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(cfs_list_empty(&rule->tr_cli_list));
92 LASSERT(cfs_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 (!cfs_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(cfs_list_empty(&cli->tc_linkage));
159 /* Rule's ref is added before called */
161 cfs_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, char *buff, int length)
168 return rule->tr_head->th_ops->o_rule_dump(rule, buff, length);
172 nrs_tbf_rule_dump_all(struct nrs_tbf_head *head, char *buff, int length)
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 cfs_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, buff + rc, length - rc);
184 spin_unlock(&head->th_rule_lock);
189 static struct nrs_tbf_rule *
190 nrs_tbf_rule_find_nolock(struct nrs_tbf_head *head,
193 struct nrs_tbf_rule *rule;
195 LASSERT(head != NULL);
196 cfs_list_for_each_entry(rule, &head->th_list, tr_linkage) {
197 LASSERT((rule->tr_flags & NTRS_STOPPING) == 0);
198 if (strcmp(rule->tr_name, name) == 0) {
199 nrs_tbf_rule_get(rule);
206 static struct nrs_tbf_rule *
207 nrs_tbf_rule_find(struct nrs_tbf_head *head,
210 struct nrs_tbf_rule *rule;
212 LASSERT(head != NULL);
213 spin_lock(&head->th_rule_lock);
214 rule = nrs_tbf_rule_find_nolock(head, name);
215 spin_unlock(&head->th_rule_lock);
219 static struct nrs_tbf_rule *
220 nrs_tbf_rule_match(struct nrs_tbf_head *head,
221 struct nrs_tbf_client *cli)
223 struct nrs_tbf_rule *rule = NULL;
224 struct nrs_tbf_rule *tmp_rule;
226 spin_lock(&head->th_rule_lock);
227 /* Match the newest rule in the list */
228 cfs_list_for_each_entry(tmp_rule, &head->th_list, tr_linkage) {
229 LASSERT((tmp_rule->tr_flags & NTRS_STOPPING) == 0);
230 if (head->th_ops->o_rule_match(tmp_rule, cli)) {
237 rule = head->th_rule;
239 nrs_tbf_rule_get(rule);
240 spin_unlock(&head->th_rule_lock);
245 nrs_tbf_cli_init(struct nrs_tbf_head *head,
246 struct nrs_tbf_client *cli,
247 struct ptlrpc_request *req)
249 struct nrs_tbf_rule *rule;
251 cli->tc_in_heap = false;
252 head->th_ops->o_cli_init(cli, req);
253 CFS_INIT_LIST_HEAD(&cli->tc_list);
254 CFS_INIT_LIST_HEAD(&cli->tc_linkage);
255 atomic_set(&cli->tc_ref, 1);
256 rule = nrs_tbf_rule_match(head, cli);
257 nrs_tbf_cli_reset(head, rule, cli);
261 nrs_tbf_cli_fini(struct nrs_tbf_client *cli)
263 LASSERT(cfs_list_empty(&cli->tc_list));
264 LASSERT(!cli->tc_in_heap);
265 LASSERT(atomic_read(&cli->tc_ref) == 0);
266 nrs_tbf_cli_rule_put(cli);
271 nrs_tbf_rule_start(struct ptlrpc_nrs_policy *policy,
272 struct nrs_tbf_head *head,
273 struct nrs_tbf_cmd *start)
275 struct nrs_tbf_rule *rule, *tmp_rule;
278 rule = nrs_tbf_rule_find(head, start->tc_name);
280 nrs_tbf_rule_put(rule);
284 OBD_CPT_ALLOC_PTR(rule, nrs_pol2cptab(policy), nrs_pol2cptid(policy));
288 memcpy(rule->tr_name, start->tc_name, strlen(start->tc_name));
289 rule->tr_rpc_rate = start->tc_rpc_rate;
290 rule->tr_nsecs = NSEC_PER_SEC / rule->tr_rpc_rate;
291 rule->tr_depth = tbf_depth;
292 atomic_set(&rule->tr_ref, 1);
293 CFS_INIT_LIST_HEAD(&rule->tr_cli_list);
294 CFS_INIT_LIST_HEAD(&rule->tr_nids);
296 rc = head->th_ops->o_rule_init(policy, rule, start);
302 /* Add as the newest rule */
303 spin_lock(&head->th_rule_lock);
304 tmp_rule = nrs_tbf_rule_find_nolock(head, start->tc_name);
306 nrs_tbf_rule_put(tmp_rule);
307 nrs_tbf_rule_put(rule);
310 cfs_list_add(&rule->tr_linkage, &head->th_list);
311 rule->tr_head = head;
312 spin_unlock(&head->th_rule_lock);
313 atomic_inc(&head->th_rule_sequence);
314 if (start->tc_rule_flags & NTRS_DEFAULT) {
315 rule->tr_flags |= NTRS_DEFAULT;
316 LASSERT(head->th_rule == NULL);
317 head->th_rule = rule;
324 nrs_tbf_rule_change(struct ptlrpc_nrs_policy *policy,
325 struct nrs_tbf_head *head,
326 struct nrs_tbf_cmd *change)
328 struct nrs_tbf_rule *rule;
330 assert_spin_locked(&policy->pol_nrs->nrs_lock);
332 rule = nrs_tbf_rule_find(head, change->tc_name);
336 rule->tr_rpc_rate = change->tc_rpc_rate;
337 rule->tr_nsecs = NSEC_PER_SEC / rule->tr_rpc_rate;
338 rule->tr_generation++;
339 nrs_tbf_rule_put(rule);
345 nrs_tbf_rule_stop(struct ptlrpc_nrs_policy *policy,
346 struct nrs_tbf_head *head,
347 struct nrs_tbf_cmd *stop)
349 struct nrs_tbf_rule *rule;
351 assert_spin_locked(&policy->pol_nrs->nrs_lock);
353 if (strcmp(stop->tc_name, NRS_TBF_DEFAULT_RULE) == 0)
356 rule = nrs_tbf_rule_find(head, stop->tc_name);
360 cfs_list_del_init(&rule->tr_linkage);
361 rule->tr_flags |= NTRS_STOPPING;
362 nrs_tbf_rule_put(rule);
363 nrs_tbf_rule_put(rule);
369 nrs_tbf_command(struct ptlrpc_nrs_policy *policy,
370 struct nrs_tbf_head *head,
371 struct nrs_tbf_cmd *cmd)
375 assert_spin_locked(&policy->pol_nrs->nrs_lock);
377 switch (cmd->tc_cmd) {
378 case NRS_CTL_TBF_START_RULE:
379 if (!(cmd->tc_valid_types & head->th_type_flag))
382 spin_unlock(&policy->pol_nrs->nrs_lock);
383 rc = nrs_tbf_rule_start(policy, head, cmd);
384 spin_lock(&policy->pol_nrs->nrs_lock);
386 case NRS_CTL_TBF_CHANGE_RATE:
387 rc = nrs_tbf_rule_change(policy, head, cmd);
389 case NRS_CTL_TBF_STOP_RULE:
390 rc = nrs_tbf_rule_stop(policy, head, cmd);
391 /* Take it as a success, if not exists at all */
392 return rc == -ENOENT ? 0 : rc;
399 * Binary heap predicate.
401 * \param[in] e1 the first binheap node to compare
402 * \param[in] e2 the second binheap node to compare
407 static int tbf_cli_compare(cfs_binheap_node_t *e1, cfs_binheap_node_t *e2)
409 struct nrs_tbf_client *cli1;
410 struct nrs_tbf_client *cli2;
412 cli1 = container_of(e1, struct nrs_tbf_client, tc_node);
413 cli2 = container_of(e2, struct nrs_tbf_client, tc_node);
415 if (cli1->tc_check_time + cli1->tc_nsecs <
416 cli2->tc_check_time + cli2->tc_nsecs)
418 else if (cli1->tc_check_time + cli1->tc_nsecs >
419 cli2->tc_check_time + cli2->tc_nsecs)
422 if (cli1->tc_check_time < cli2->tc_check_time)
424 else if (cli1->tc_check_time > cli2->tc_check_time)
427 /* Maybe need more comparasion, e.g. request number in the rules */
432 * TBF binary heap operations
434 static cfs_binheap_ops_t nrs_tbf_heap_ops = {
437 .hop_compare = tbf_cli_compare,
440 static unsigned nrs_tbf_jobid_hop_hash(cfs_hash_t *hs, const void *key,
443 return cfs_hash_djb2_hash(key, strlen(key), mask);
446 static int nrs_tbf_jobid_hop_keycmp(const void *key, cfs_hlist_node_t *hnode)
448 struct nrs_tbf_client *cli = cfs_hlist_entry(hnode,
449 struct nrs_tbf_client,
452 return (strcmp(cli->tc_jobid, key) == 0);
455 static void *nrs_tbf_jobid_hop_key(cfs_hlist_node_t *hnode)
457 struct nrs_tbf_client *cli = cfs_hlist_entry(hnode,
458 struct nrs_tbf_client,
461 return cli->tc_jobid;
464 static void *nrs_tbf_jobid_hop_object(cfs_hlist_node_t *hnode)
466 return cfs_hlist_entry(hnode, struct nrs_tbf_client, tc_hnode);
469 static void nrs_tbf_jobid_hop_get(cfs_hash_t *hs, cfs_hlist_node_t *hnode)
471 struct nrs_tbf_client *cli = cfs_hlist_entry(hnode,
472 struct nrs_tbf_client,
475 atomic_inc(&cli->tc_ref);
478 static void nrs_tbf_jobid_hop_put(cfs_hash_t *hs, cfs_hlist_node_t *hnode)
480 struct nrs_tbf_client *cli = cfs_hlist_entry(hnode,
481 struct nrs_tbf_client,
484 atomic_dec(&cli->tc_ref);
487 static void nrs_tbf_jobid_hop_exit(cfs_hash_t *hs, cfs_hlist_node_t *hnode)
490 struct nrs_tbf_client *cli = cfs_hlist_entry(hnode,
491 struct nrs_tbf_client,
494 LASSERT(atomic_read(&cli->tc_ref) == 0);
495 nrs_tbf_cli_fini(cli);
498 static cfs_hash_ops_t nrs_tbf_jobid_hash_ops = {
499 .hs_hash = nrs_tbf_jobid_hop_hash,
500 .hs_keycmp = nrs_tbf_jobid_hop_keycmp,
501 .hs_key = nrs_tbf_jobid_hop_key,
502 .hs_object = nrs_tbf_jobid_hop_object,
503 .hs_get = nrs_tbf_jobid_hop_get,
504 .hs_put = nrs_tbf_jobid_hop_put,
505 .hs_put_locked = nrs_tbf_jobid_hop_put,
506 .hs_exit = nrs_tbf_jobid_hop_exit,
509 #define NRS_TBF_JOBID_HASH_FLAGS (CFS_HASH_SPIN_BKTLOCK | \
510 CFS_HASH_NO_ITEMREF | \
513 static struct nrs_tbf_client *
514 nrs_tbf_jobid_hash_lookup(cfs_hash_t *hs,
518 cfs_hlist_node_t *hnode;
519 struct nrs_tbf_client *cli;
521 /* cfs_hash_bd_peek_locked is a somehow "internal" function
522 * of cfs_hash, it doesn't add refcount on object. */
523 hnode = cfs_hash_bd_peek_locked(hs, bd, (void *)jobid);
527 cfs_hash_get(hs, hnode);
528 cli = container_of0(hnode, struct nrs_tbf_client, tc_hnode);
529 if (!cfs_list_empty(&cli->tc_lru))
530 cfs_list_del_init(&cli->tc_lru);
534 #define NRS_TBF_JOBID_NULL ""
536 static struct nrs_tbf_client *
537 nrs_tbf_jobid_cli_find(struct nrs_tbf_head *head,
538 struct ptlrpc_request *req)
541 struct nrs_tbf_client *cli;
542 cfs_hash_t *hs = head->th_cli_hash;
545 jobid = lustre_msg_get_jobid(req->rq_reqmsg);
547 jobid = NRS_TBF_JOBID_NULL;
548 cfs_hash_bd_get_and_lock(hs, (void *)jobid, &bd, 1);
549 cli = nrs_tbf_jobid_hash_lookup(hs, &bd, jobid);
550 cfs_hash_bd_unlock(hs, &bd, 1);
555 static struct nrs_tbf_client *
556 nrs_tbf_jobid_cli_findadd(struct nrs_tbf_head *head,
557 struct nrs_tbf_client *cli)
560 struct nrs_tbf_client *ret;
561 cfs_hash_t *hs = head->th_cli_hash;
564 jobid = cli->tc_jobid;
565 cfs_hash_bd_get_and_lock(hs, (void *)jobid, &bd, 1);
566 ret = nrs_tbf_jobid_hash_lookup(hs, &bd, jobid);
568 cfs_hash_bd_add_locked(hs, &bd, &cli->tc_hnode);
571 cfs_hash_bd_unlock(hs, &bd, 1);
577 nrs_tbf_jobid_cli_put(struct nrs_tbf_head *head,
578 struct nrs_tbf_client *cli)
581 cfs_hash_t *hs = head->th_cli_hash;
582 struct nrs_tbf_bucket *bkt;
584 CFS_LIST_HEAD (zombies);
586 cfs_hash_bd_get(hs, &cli->tc_jobid, &bd);
587 bkt = cfs_hash_bd_extra_get(hs, &bd);
588 if (!cfs_hash_bd_dec_and_lock(hs, &bd, &cli->tc_ref))
590 LASSERT(cfs_list_empty(&cli->tc_lru));
591 cfs_list_add_tail(&cli->tc_lru, &bkt->ntb_lru);
594 * Check and purge the LRU, there is at least one client in the LRU.
596 hw = tbf_jobid_cache_size >>
597 (hs->hs_cur_bits - hs->hs_bkt_bits);
598 while (cfs_hash_bd_count_get(&bd) > hw) {
599 if (unlikely(cfs_list_empty(&bkt->ntb_lru)))
601 cli = cfs_list_entry(bkt->ntb_lru.next,
602 struct nrs_tbf_client,
604 LASSERT(atomic_read(&cli->tc_ref) == 0);
605 cfs_hash_bd_del_locked(hs, &bd, &cli->tc_hnode);
606 cfs_list_move(&cli->tc_lru, &zombies);
608 cfs_hash_bd_unlock(head->th_cli_hash, &bd, 1);
610 while (!cfs_list_empty(&zombies)) {
611 cli = container_of0(zombies.next,
612 struct nrs_tbf_client, tc_lru);
613 cfs_list_del_init(&cli->tc_lru);
614 nrs_tbf_cli_fini(cli);
619 nrs_tbf_jobid_cli_init(struct nrs_tbf_client *cli,
620 struct ptlrpc_request *req)
622 char *jobid = lustre_msg_get_jobid(req->rq_reqmsg);
625 jobid = NRS_TBF_JOBID_NULL;
626 LASSERT(strlen(jobid) < JOBSTATS_JOBID_SIZE);
627 CFS_INIT_LIST_HEAD(&cli->tc_lru);
628 memcpy(cli->tc_jobid, jobid, strlen(jobid));
631 static int nrs_tbf_jobid_hash_order(void)
635 for (bits = 1; (1 << bits) < tbf_jobid_cache_size; ++bits)
641 #define NRS_TBF_JOBID_BKT_BITS 10
644 nrs_tbf_jobid_startup(struct ptlrpc_nrs_policy *policy,
645 struct nrs_tbf_head *head)
647 struct nrs_tbf_cmd start;
648 struct nrs_tbf_bucket *bkt;
654 bits = nrs_tbf_jobid_hash_order();
655 if (bits < NRS_TBF_JOBID_BKT_BITS)
656 bits = NRS_TBF_JOBID_BKT_BITS;
657 head->th_cli_hash = cfs_hash_create("nrs_tbf_hash",
660 NRS_TBF_JOBID_BKT_BITS,
664 &nrs_tbf_jobid_hash_ops,
665 NRS_TBF_JOBID_HASH_FLAGS);
666 if (head->th_cli_hash == NULL)
669 cfs_hash_for_each_bucket(head->th_cli_hash, &bd, i) {
670 bkt = cfs_hash_bd_extra_get(head->th_cli_hash, &bd);
671 CFS_INIT_LIST_HEAD(&bkt->ntb_lru);
674 memset(&start, 0, sizeof(start));
675 start.tc_jobids_str = "*";
677 start.tc_rpc_rate = tbf_rate;
678 start.tc_rule_flags = NTRS_DEFAULT;
679 start.tc_name = NRS_TBF_DEFAULT_RULE;
680 CFS_INIT_LIST_HEAD(&start.tc_jobids);
681 rc = nrs_tbf_rule_start(policy, head, &start);
687 * Frees jobid of \a list.
691 nrs_tbf_jobid_list_free(cfs_list_t *jobid_list)
693 struct nrs_tbf_jobid *jobid, *n;
695 cfs_list_for_each_entry_safe(jobid, n, jobid_list, tj_linkage) {
696 OBD_FREE(jobid->tj_id, strlen(jobid->tj_id) + 1);
697 cfs_list_del(&jobid->tj_linkage);
698 OBD_FREE(jobid, sizeof(struct nrs_tbf_jobid));
703 nrs_tbf_jobid_list_add(const struct cfs_lstr *id, cfs_list_t *jobid_list)
705 struct nrs_tbf_jobid *jobid;
707 OBD_ALLOC(jobid, sizeof(struct nrs_tbf_jobid));
711 OBD_ALLOC(jobid->tj_id, id->ls_len + 1);
712 if (jobid->tj_id == NULL) {
713 OBD_FREE(jobid, sizeof(struct nrs_tbf_jobid));
717 memcpy(jobid->tj_id, id->ls_str, id->ls_len);
718 cfs_list_add_tail(&jobid->tj_linkage, jobid_list);
723 nrs_tbf_jobid_list_match(cfs_list_t *jobid_list, char *id)
725 struct nrs_tbf_jobid *jobid;
727 cfs_list_for_each_entry(jobid, jobid_list, tj_linkage) {
728 if (strcmp(id, jobid->tj_id) == 0)
735 nrs_tbf_jobid_list_parse(char *str, int len, cfs_list_t *jobid_list)
744 CFS_INIT_LIST_HEAD(jobid_list);
746 rc = cfs_gettok(&src, ' ', &res);
751 rc = nrs_tbf_jobid_list_add(&res, jobid_list);
756 nrs_tbf_jobid_list_free(jobid_list);
760 static void nrs_tbf_jobid_cmd_fini(struct nrs_tbf_cmd *cmd)
762 if (!cfs_list_empty(&cmd->tc_jobids))
763 nrs_tbf_jobid_list_free(&cmd->tc_jobids);
764 if (cmd->tc_jobids_str)
765 OBD_FREE(cmd->tc_jobids_str, strlen(cmd->tc_jobids_str) + 1);
768 static int nrs_tbf_jobid_parse(struct nrs_tbf_cmd *cmd, const char *id)
772 OBD_ALLOC(cmd->tc_jobids_str, strlen(id) + 1);
773 if (cmd->tc_jobids_str == NULL)
776 memcpy(cmd->tc_jobids_str, id, strlen(id));
778 /* parse jobid list */
779 rc = nrs_tbf_jobid_list_parse(cmd->tc_jobids_str,
780 strlen(cmd->tc_jobids_str),
783 nrs_tbf_jobid_cmd_fini(cmd);
788 static int nrs_tbf_jobid_rule_init(struct ptlrpc_nrs_policy *policy,
789 struct nrs_tbf_rule *rule,
790 struct nrs_tbf_cmd *start)
794 LASSERT(start->tc_jobids_str);
795 OBD_ALLOC(rule->tr_jobids_str,
796 strlen(start->tc_jobids_str) + 1);
797 if (rule->tr_jobids_str == NULL)
800 memcpy(rule->tr_jobids_str,
801 start->tc_jobids_str,
802 strlen(start->tc_jobids_str));
804 CFS_INIT_LIST_HEAD(&rule->tr_jobids);
805 if (!cfs_list_empty(&start->tc_jobids)) {
806 rc = nrs_tbf_jobid_list_parse(rule->tr_jobids_str,
807 strlen(rule->tr_jobids_str),
810 CERROR("jobids {%s} illegal\n", rule->tr_jobids_str);
813 OBD_FREE(rule->tr_jobids_str,
814 strlen(start->tc_jobids_str) + 1);
819 nrs_tbf_jobid_rule_dump(struct nrs_tbf_rule *rule, char *buff, int length)
821 return snprintf(buff, length, "%s {%s} %llu, ref %d\n",
825 atomic_read(&rule->tr_ref) - 1);
829 nrs_tbf_jobid_rule_match(struct nrs_tbf_rule *rule,
830 struct nrs_tbf_client *cli)
832 return nrs_tbf_jobid_list_match(&rule->tr_jobids, cli->tc_jobid);
835 static void nrs_tbf_jobid_rule_fini(struct nrs_tbf_rule *rule)
837 if (!cfs_list_empty(&rule->tr_jobids))
838 nrs_tbf_jobid_list_free(&rule->tr_jobids);
839 LASSERT(rule->tr_jobids_str != NULL);
840 OBD_FREE(rule->tr_jobids_str, strlen(rule->tr_jobids_str) + 1);
843 struct nrs_tbf_ops nrs_tbf_jobid_ops = {
844 .o_name = NRS_TBF_TYPE_JOBID,
845 .o_startup = nrs_tbf_jobid_startup,
846 .o_cli_find = nrs_tbf_jobid_cli_find,
847 .o_cli_findadd = nrs_tbf_jobid_cli_findadd,
848 .o_cli_put = nrs_tbf_jobid_cli_put,
849 .o_cli_init = nrs_tbf_jobid_cli_init,
850 .o_rule_init = nrs_tbf_jobid_rule_init,
851 .o_rule_dump = nrs_tbf_jobid_rule_dump,
852 .o_rule_match = nrs_tbf_jobid_rule_match,
853 .o_rule_fini = nrs_tbf_jobid_rule_fini,
857 * libcfs_hash operations for nrs_tbf_net::cn_cli_hash
859 * This uses ptlrpc_request::rq_peer.nid as its key, in order to hash
860 * nrs_tbf_client objects.
862 #define NRS_TBF_NID_BKT_BITS 8
863 #define NRS_TBF_NID_BITS 16
865 static unsigned nrs_tbf_nid_hop_hash(cfs_hash_t *hs, const void *key,
868 return cfs_hash_djb2_hash(key, sizeof(lnet_nid_t), mask);
871 static int nrs_tbf_nid_hop_keycmp(const void *key, cfs_hlist_node_t *hnode)
873 lnet_nid_t *nid = (lnet_nid_t *)key;
874 struct nrs_tbf_client *cli = cfs_hlist_entry(hnode,
875 struct nrs_tbf_client,
878 return *nid == cli->tc_nid;
881 static void *nrs_tbf_nid_hop_key(cfs_hlist_node_t *hnode)
883 struct nrs_tbf_client *cli = cfs_hlist_entry(hnode,
884 struct nrs_tbf_client,
890 static void *nrs_tbf_nid_hop_object(cfs_hlist_node_t *hnode)
892 return cfs_hlist_entry(hnode, struct nrs_tbf_client, tc_hnode);
895 static void nrs_tbf_nid_hop_get(cfs_hash_t *hs, cfs_hlist_node_t *hnode)
897 struct nrs_tbf_client *cli = cfs_hlist_entry(hnode,
898 struct nrs_tbf_client,
901 atomic_inc(&cli->tc_ref);
904 static void nrs_tbf_nid_hop_put(cfs_hash_t *hs, cfs_hlist_node_t *hnode)
906 struct nrs_tbf_client *cli = cfs_hlist_entry(hnode,
907 struct nrs_tbf_client,
910 atomic_dec(&cli->tc_ref);
913 static void nrs_tbf_nid_hop_exit(cfs_hash_t *hs, cfs_hlist_node_t *hnode)
915 struct nrs_tbf_client *cli = cfs_hlist_entry(hnode,
916 struct nrs_tbf_client,
919 LASSERTF(atomic_read(&cli->tc_ref) == 0,
920 "Busy TBF object from client with NID %s, with %d refs\n",
921 libcfs_nid2str(cli->tc_nid), atomic_read(&cli->tc_ref));
923 nrs_tbf_cli_fini(cli);
926 static cfs_hash_ops_t nrs_tbf_nid_hash_ops = {
927 .hs_hash = nrs_tbf_nid_hop_hash,
928 .hs_keycmp = nrs_tbf_nid_hop_keycmp,
929 .hs_key = nrs_tbf_nid_hop_key,
930 .hs_object = nrs_tbf_nid_hop_object,
931 .hs_get = nrs_tbf_nid_hop_get,
932 .hs_put = nrs_tbf_nid_hop_put,
933 .hs_put_locked = nrs_tbf_nid_hop_put,
934 .hs_exit = nrs_tbf_nid_hop_exit,
937 static struct nrs_tbf_client *
938 nrs_tbf_nid_cli_find(struct nrs_tbf_head *head,
939 struct ptlrpc_request *req)
941 return cfs_hash_lookup(head->th_cli_hash, &req->rq_peer.nid);
944 static struct nrs_tbf_client *
945 nrs_tbf_nid_cli_findadd(struct nrs_tbf_head *head,
946 struct nrs_tbf_client *cli)
948 return cfs_hash_findadd_unique(head->th_cli_hash, &cli->tc_nid,
953 nrs_tbf_nid_cli_put(struct nrs_tbf_head *head,
954 struct nrs_tbf_client *cli)
956 cfs_hash_put(head->th_cli_hash, &cli->tc_hnode);
960 nrs_tbf_nid_startup(struct ptlrpc_nrs_policy *policy,
961 struct nrs_tbf_head *head)
963 struct nrs_tbf_cmd start;
966 head->th_cli_hash = cfs_hash_create("nrs_tbf_hash",
969 NRS_TBF_NID_BKT_BITS, 0,
972 &nrs_tbf_nid_hash_ops,
973 CFS_HASH_RW_BKTLOCK);
974 if (head->th_cli_hash == NULL)
977 memset(&start, 0, sizeof(start));
978 start.tc_nids_str = "*";
980 start.tc_rpc_rate = tbf_rate;
981 start.tc_rule_flags = NTRS_DEFAULT;
982 start.tc_name = NRS_TBF_DEFAULT_RULE;
983 CFS_INIT_LIST_HEAD(&start.tc_nids);
984 rc = nrs_tbf_rule_start(policy, head, &start);
990 nrs_tbf_nid_cli_init(struct nrs_tbf_client *cli,
991 struct ptlrpc_request *req)
993 cli->tc_nid = req->rq_peer.nid;
996 static int nrs_tbf_nid_rule_init(struct ptlrpc_nrs_policy *policy,
997 struct nrs_tbf_rule *rule,
998 struct nrs_tbf_cmd *start)
1000 LASSERT(start->tc_nids_str);
1001 OBD_ALLOC(rule->tr_nids_str,
1002 strlen(start->tc_nids_str) + 1);
1003 if (rule->tr_nids_str == NULL)
1006 memcpy(rule->tr_nids_str,
1008 strlen(start->tc_nids_str));
1010 CFS_INIT_LIST_HEAD(&rule->tr_nids);
1011 if (!cfs_list_empty(&start->tc_nids)) {
1012 if (cfs_parse_nidlist(rule->tr_nids_str,
1013 strlen(rule->tr_nids_str),
1014 &rule->tr_nids) <= 0) {
1015 CERROR("nids {%s} illegal\n",
1017 OBD_FREE(rule->tr_nids_str,
1018 strlen(start->tc_nids_str) + 1);
1026 nrs_tbf_nid_rule_dump(struct nrs_tbf_rule *rule, char *buff, int length)
1028 return snprintf(buff, length, "%s {%s} %llu, ref %d\n",
1032 atomic_read(&rule->tr_ref) - 1);
1036 nrs_tbf_nid_rule_match(struct nrs_tbf_rule *rule,
1037 struct nrs_tbf_client *cli)
1039 return cfs_match_nid(cli->tc_nid, &rule->tr_nids);
1042 static void nrs_tbf_nid_rule_fini(struct nrs_tbf_rule *rule)
1044 if (!cfs_list_empty(&rule->tr_nids))
1045 cfs_free_nidlist(&rule->tr_nids);
1046 LASSERT(rule->tr_nids_str != NULL);
1047 OBD_FREE(rule->tr_nids_str, strlen(rule->tr_nids_str) + 1);
1050 static void nrs_tbf_nid_cmd_fini(struct nrs_tbf_cmd *cmd)
1052 if (!cfs_list_empty(&cmd->tc_nids))
1053 cfs_free_nidlist(&cmd->tc_nids);
1054 if (cmd->tc_nids_str)
1055 OBD_FREE(cmd->tc_nids_str, strlen(cmd->tc_nids_str) + 1);
1058 static int nrs_tbf_nid_parse(struct nrs_tbf_cmd *cmd, const char *id)
1060 OBD_ALLOC(cmd->tc_nids_str, strlen(id) + 1);
1061 if (cmd->tc_nids_str == NULL)
1064 memcpy(cmd->tc_nids_str, id, strlen(id));
1066 /* parse NID list */
1067 if (cfs_parse_nidlist(cmd->tc_nids_str,
1068 strlen(cmd->tc_nids_str),
1069 &cmd->tc_nids) <= 0) {
1070 nrs_tbf_nid_cmd_fini(cmd);
1077 struct nrs_tbf_ops nrs_tbf_nid_ops = {
1078 .o_name = NRS_TBF_TYPE_NID,
1079 .o_startup = nrs_tbf_nid_startup,
1080 .o_cli_find = nrs_tbf_nid_cli_find,
1081 .o_cli_findadd = nrs_tbf_nid_cli_findadd,
1082 .o_cli_put = nrs_tbf_nid_cli_put,
1083 .o_cli_init = nrs_tbf_nid_cli_init,
1084 .o_rule_init = nrs_tbf_nid_rule_init,
1085 .o_rule_dump = nrs_tbf_nid_rule_dump,
1086 .o_rule_match = nrs_tbf_nid_rule_match,
1087 .o_rule_fini = nrs_tbf_nid_rule_fini,
1091 * Is called before the policy transitions into
1092 * ptlrpc_nrs_pol_state::NRS_POL_STATE_STARTED; allocates and initializes a
1093 * policy-specific private data structure.
1095 * \param[in] policy The policy to start
1097 * \retval -ENOMEM OOM error
1100 * \see nrs_policy_register()
1101 * \see nrs_policy_ctl()
1103 static int nrs_tbf_start(struct ptlrpc_nrs_policy *policy, char *arg)
1105 struct nrs_tbf_head *head;
1106 struct nrs_tbf_ops *ops;
1110 if (arg == NULL || strlen(arg) > NRS_TBF_TYPE_MAX_LEN)
1111 GOTO(out, rc = -EINVAL);
1113 if (strcmp(arg, NRS_TBF_TYPE_NID) == 0) {
1114 ops = &nrs_tbf_nid_ops;
1115 type = NRS_TBF_FLAG_NID;
1116 } else if (strcmp(arg, NRS_TBF_TYPE_JOBID) == 0) {
1117 ops = &nrs_tbf_jobid_ops;
1118 type = NRS_TBF_FLAG_JOBID;
1120 GOTO(out, rc = -ENOTSUPP);
1122 OBD_CPT_ALLOC_PTR(head, nrs_pol2cptab(policy), nrs_pol2cptid(policy));
1124 GOTO(out, rc = -ENOMEM);
1126 memcpy(head->th_type, arg, strlen(arg));
1127 head->th_type[strlen(arg)] = '\0';
1129 head->th_type_flag = type;
1131 head->th_binheap = cfs_binheap_create(&nrs_tbf_heap_ops,
1132 CBH_FLAG_ATOMIC_GROW, 4096, NULL,
1133 nrs_pol2cptab(policy),
1134 nrs_pol2cptid(policy));
1135 if (head->th_binheap == NULL)
1136 GOTO(out_free_head, rc = -ENOMEM);
1138 atomic_set(&head->th_rule_sequence, 0);
1139 spin_lock_init(&head->th_rule_lock);
1140 CFS_INIT_LIST_HEAD(&head->th_list);
1141 hrtimer_init(&head->th_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
1142 head->th_timer.function = nrs_tbf_timer_cb;
1143 rc = head->th_ops->o_startup(policy, head);
1145 GOTO(out_free_heap, rc);
1147 policy->pol_private = head;
1150 cfs_binheap_destroy(head->th_binheap);
1158 * Is called before the policy transitions into
1159 * ptlrpc_nrs_pol_state::NRS_POL_STATE_STOPPED; deallocates the policy-specific
1160 * private data structure.
1162 * \param[in] policy The policy to stop
1164 * \see nrs_policy_stop0()
1166 static void nrs_tbf_stop(struct ptlrpc_nrs_policy *policy)
1168 struct nrs_tbf_head *head = policy->pol_private;
1169 struct ptlrpc_nrs *nrs = policy->pol_nrs;
1170 struct nrs_tbf_rule *rule, *n;
1172 LASSERT(head != NULL);
1173 LASSERT(head->th_cli_hash != NULL);
1174 hrtimer_cancel(&head->th_timer);
1175 /* Should cleanup hash first before free rules */
1176 cfs_hash_putref(head->th_cli_hash);
1177 cfs_list_for_each_entry_safe(rule, n, &head->th_list, tr_linkage) {
1178 cfs_list_del_init(&rule->tr_linkage);
1179 nrs_tbf_rule_put(rule);
1181 LASSERT(cfs_list_empty(&head->th_list));
1182 LASSERT(head->th_binheap != NULL);
1183 LASSERT(cfs_binheap_is_empty(head->th_binheap));
1184 cfs_binheap_destroy(head->th_binheap);
1186 spin_lock(&nrs->nrs_lock);
1187 nrs->nrs_throttling = 0;
1188 spin_unlock(&nrs->nrs_lock);
1189 wake_up(&policy->pol_nrs->nrs_svcpt->scp_waitq);
1193 * Performs a policy-specific ctl function on TBF policy instances; similar
1196 * \param[in] policy the policy instance
1197 * \param[in] opc the opcode
1198 * \param[in,out] arg used for passing parameters and information
1200 * \pre assert_spin_locked(&policy->pol_nrs->->nrs_lock)
1201 * \post assert_spin_locked(&policy->pol_nrs->->nrs_lock)
1203 * \retval 0 operation carried out successfully
1206 int nrs_tbf_ctl(struct ptlrpc_nrs_policy *policy, enum ptlrpc_nrs_ctl opc,
1212 assert_spin_locked(&policy->pol_nrs->nrs_lock);
1214 switch ((enum nrs_ctl_tbf)opc) {
1219 * Read RPC rate size of a policy instance.
1221 case NRS_CTL_TBF_RD_RULE: {
1222 struct nrs_tbf_head *head = policy->pol_private;
1223 struct ptlrpc_service_part *svcpt;
1224 struct nrs_tbf_dump *dump;
1227 dump = (struct nrs_tbf_dump *)arg;
1229 svcpt = policy->pol_nrs->nrs_svcpt;
1230 length = snprintf(dump->td_buff, dump->td_size,
1233 dump->td_length += length;
1234 dump->td_buff += length;
1235 dump->td_size -= length;
1237 length = nrs_tbf_rule_dump_all(head,
1240 dump->td_length += length;
1241 dump->td_buff += length;
1242 dump->td_size -= length;
1247 * Write RPC rate of a policy instance.
1249 case NRS_CTL_TBF_WR_RULE: {
1250 struct nrs_tbf_head *head = policy->pol_private;
1251 struct nrs_tbf_cmd *cmd;
1253 cmd = (struct nrs_tbf_cmd *)arg;
1254 rc = nrs_tbf_command(policy,
1265 * Is called for obtaining a TBF policy resource.
1267 * \param[in] policy The policy on which the request is being asked for
1268 * \param[in] nrq The request for which resources are being taken
1269 * \param[in] parent Parent resource, unused in this policy
1270 * \param[out] resp Resources references are placed in this array
1271 * \param[in] moving_req Signifies limited caller context; unused in this
1275 * \see nrs_resource_get_safe()
1277 static int nrs_tbf_res_get(struct ptlrpc_nrs_policy *policy,
1278 struct ptlrpc_nrs_request *nrq,
1279 const struct ptlrpc_nrs_resource *parent,
1280 struct ptlrpc_nrs_resource **resp,
1283 struct nrs_tbf_head *head;
1284 struct nrs_tbf_client *cli;
1285 struct nrs_tbf_client *tmp;
1286 struct ptlrpc_request *req;
1288 if (parent == NULL) {
1289 *resp = &((struct nrs_tbf_head *)policy->pol_private)->th_res;
1293 head = container_of(parent, struct nrs_tbf_head, th_res);
1294 req = container_of(nrq, struct ptlrpc_request, rq_nrq);
1295 cli = head->th_ops->o_cli_find(head, req);
1297 spin_lock(&policy->pol_nrs->nrs_svcpt->scp_req_lock);
1298 LASSERT(cli->tc_rule);
1299 if (cli->tc_rule_sequence !=
1300 atomic_read(&head->th_rule_sequence) ||
1301 cli->tc_rule->tr_flags & NTRS_STOPPING) {
1302 struct nrs_tbf_rule *rule;
1304 rule = nrs_tbf_rule_match(head, cli);
1305 if (rule != cli->tc_rule)
1306 nrs_tbf_cli_reset(head, rule, cli);
1308 nrs_tbf_rule_put(rule);
1309 } else if (cli->tc_rule_generation !=
1310 cli->tc_rule->tr_generation) {
1311 nrs_tbf_cli_reset_value(head, cli);
1313 spin_unlock(&policy->pol_nrs->nrs_svcpt->scp_req_lock);
1317 OBD_CPT_ALLOC_GFP(cli, nrs_pol2cptab(policy), nrs_pol2cptid(policy),
1318 sizeof(*cli), moving_req ? GFP_ATOMIC : __GFP_IO);
1321 nrs_tbf_cli_init(head, cli, req);
1322 tmp = head->th_ops->o_cli_findadd(head, cli);
1324 atomic_dec(&cli->tc_ref);
1325 nrs_tbf_cli_fini(cli);
1329 *resp = &cli->tc_res;
1335 * Called when releasing references to the resource hierachy obtained for a
1336 * request for scheduling using the TBF policy.
1338 * \param[in] policy the policy the resource belongs to
1339 * \param[in] res the resource to be released
1341 static void nrs_tbf_res_put(struct ptlrpc_nrs_policy *policy,
1342 const struct ptlrpc_nrs_resource *res)
1344 struct nrs_tbf_head *head;
1345 struct nrs_tbf_client *cli;
1348 * Do nothing for freeing parent, nrs_tbf_net resources
1350 if (res->res_parent == NULL)
1353 cli = container_of(res, struct nrs_tbf_client, tc_res);
1354 head = container_of(res->res_parent, struct nrs_tbf_head, th_res);
1356 head->th_ops->o_cli_put(head, cli);
1360 * Called when getting a request from the TBF policy for handling, or just
1361 * peeking; removes the request from the policy when it is to be handled.
1363 * \param[in] policy The policy
1364 * \param[in] peek When set, signifies that we just want to examine the
1365 * request, and not handle it, so the request is not removed
1367 * \param[in] force Force the policy to return a request; unused in this
1370 * \retval The request to be handled; this is the next request in the TBF
1373 * \see ptlrpc_nrs_req_get_nolock()
1374 * \see nrs_request_get()
1377 struct ptlrpc_nrs_request *nrs_tbf_req_get(struct ptlrpc_nrs_policy *policy,
1378 bool peek, bool force)
1380 struct nrs_tbf_head *head = policy->pol_private;
1381 struct ptlrpc_nrs_request *nrq = NULL;
1382 struct nrs_tbf_client *cli;
1383 cfs_binheap_node_t *node;
1385 assert_spin_locked(&policy->pol_nrs->nrs_svcpt->scp_req_lock);
1387 if (!peek && policy->pol_nrs->nrs_throttling)
1390 node = cfs_binheap_root(head->th_binheap);
1391 if (unlikely(node == NULL))
1394 cli = container_of(node, struct nrs_tbf_client, tc_node);
1395 LASSERT(cli->tc_in_heap);
1397 nrq = cfs_list_entry(cli->tc_list.next,
1398 struct ptlrpc_nrs_request,
1401 __u64 now = ktime_to_ns(ktime_get());
1406 deadline = cli->tc_check_time +
1408 LASSERT(now >= cli->tc_check_time);
1409 passed = now - cli->tc_check_time;
1410 ntoken = (passed * cli->tc_rpc_rate) / NSEC_PER_SEC;
1411 ntoken += cli->tc_ntoken;
1412 if (ntoken > cli->tc_depth)
1413 ntoken = cli->tc_depth;
1415 struct ptlrpc_request *req;
1416 nrq = cfs_list_entry(cli->tc_list.next,
1417 struct ptlrpc_nrs_request,
1419 req = container_of(nrq,
1420 struct ptlrpc_request,
1423 cli->tc_ntoken = ntoken;
1424 cli->tc_check_time = now;
1425 cfs_list_del_init(&nrq->nr_u.tbf.tr_list);
1426 if (cfs_list_empty(&cli->tc_list)) {
1427 cfs_binheap_remove(head->th_binheap,
1429 cli->tc_in_heap = false;
1431 cfs_binheap_relocate(head->th_binheap,
1435 "NRS start %s request from %s, "
1437 policy->pol_desc->pd_name,
1438 libcfs_id2str(req->rq_peer),
1439 nrq->nr_u.tbf.tr_sequence);
1443 spin_lock(&policy->pol_nrs->nrs_lock);
1444 policy->pol_nrs->nrs_throttling = 1;
1445 spin_unlock(&policy->pol_nrs->nrs_lock);
1446 head->th_deadline = deadline;
1447 time = ktime_set(0, 0);
1448 time = ktime_add_ns(time, deadline);
1449 hrtimer_start(&head->th_timer, time, HRTIMER_MODE_ABS);
1457 * Adds request \a nrq to \a policy's list of queued requests
1459 * \param[in] policy The policy
1460 * \param[in] nrq The request to add
1462 * \retval 0 success; nrs_request_enqueue() assumes this function will always
1465 static int nrs_tbf_req_add(struct ptlrpc_nrs_policy *policy,
1466 struct ptlrpc_nrs_request *nrq)
1468 struct nrs_tbf_head *head;
1469 struct nrs_tbf_client *cli;
1472 assert_spin_locked(&policy->pol_nrs->nrs_svcpt->scp_req_lock);
1474 cli = container_of(nrs_request_resource(nrq),
1475 struct nrs_tbf_client, tc_res);
1476 head = container_of(nrs_request_resource(nrq)->res_parent,
1477 struct nrs_tbf_head, th_res);
1478 if (cfs_list_empty(&cli->tc_list)) {
1479 LASSERT(!cli->tc_in_heap);
1480 rc = cfs_binheap_insert(head->th_binheap, &cli->tc_node);
1482 cli->tc_in_heap = true;
1483 nrq->nr_u.tbf.tr_sequence = head->th_sequence++;
1484 cfs_list_add_tail(&nrq->nr_u.tbf.tr_list,
1486 if (policy->pol_nrs->nrs_throttling) {
1487 __u64 deadline = cli->tc_check_time +
1489 if ((head->th_deadline > deadline) &&
1490 (hrtimer_try_to_cancel(&head->th_timer)
1493 head->th_deadline = deadline;
1494 time = ktime_set(0, 0);
1495 time = ktime_add_ns(time, deadline);
1496 hrtimer_start(&head->th_timer, time,
1502 LASSERT(cli->tc_in_heap);
1503 nrq->nr_u.tbf.tr_sequence = head->th_sequence++;
1504 cfs_list_add_tail(&nrq->nr_u.tbf.tr_list,
1511 * Removes request \a nrq from \a policy's list of queued requests.
1513 * \param[in] policy The policy
1514 * \param[in] nrq The request to remove
1516 static void nrs_tbf_req_del(struct ptlrpc_nrs_policy *policy,
1517 struct ptlrpc_nrs_request *nrq)
1519 struct nrs_tbf_head *head;
1520 struct nrs_tbf_client *cli;
1522 assert_spin_locked(&policy->pol_nrs->nrs_svcpt->scp_req_lock);
1524 cli = container_of(nrs_request_resource(nrq),
1525 struct nrs_tbf_client, tc_res);
1526 head = container_of(nrs_request_resource(nrq)->res_parent,
1527 struct nrs_tbf_head, th_res);
1529 LASSERT(!cfs_list_empty(&nrq->nr_u.tbf.tr_list));
1530 cfs_list_del_init(&nrq->nr_u.tbf.tr_list);
1531 if (cfs_list_empty(&cli->tc_list)) {
1532 cfs_binheap_remove(head->th_binheap,
1534 cli->tc_in_heap = false;
1536 cfs_binheap_relocate(head->th_binheap,
1542 * Prints a debug statement right before the request \a nrq stops being
1545 * \param[in] policy The policy handling the request
1546 * \param[in] nrq The request being handled
1548 * \see ptlrpc_server_finish_request()
1549 * \see ptlrpc_nrs_req_stop_nolock()
1551 static void nrs_tbf_req_stop(struct ptlrpc_nrs_policy *policy,
1552 struct ptlrpc_nrs_request *nrq)
1554 struct ptlrpc_request *req = container_of(nrq, struct ptlrpc_request,
1557 assert_spin_locked(&policy->pol_nrs->nrs_svcpt->scp_req_lock);
1559 CDEBUG(D_RPCTRACE, "NRS stop %s request from %s, seq: "LPU64"\n",
1560 policy->pol_desc->pd_name, libcfs_id2str(req->rq_peer),
1561 nrq->nr_u.tbf.tr_sequence);
1571 * The maximum RPC rate.
1573 #define LPROCFS_NRS_RATE_MAX 65535
1575 static int ptlrpc_lprocfs_rd_nrs_tbf_rule(char *page, char **start,
1576 off_t off, int count, int *eof,
1579 struct ptlrpc_service *svc = data;
1582 struct nrs_tbf_dump dump;
1584 rc2 = snprintf(page, count, "regular_requests:\n");
1586 * Perform two separate calls to this as only one of the NRS heads'
1587 * policies may be in the ptlrpc_nrs_pol_state::NRS_POL_STATE_STARTED or
1588 * ptlrpc_nrs_pol_state::NRS_POL_STATE_STOPPING state.
1591 dump.td_buff = page + rc2;
1592 dump.td_size = count - rc2;
1593 rc = ptlrpc_nrs_policy_control(svc, PTLRPC_NRS_QUEUE_REG,
1595 NRS_CTL_TBF_RD_RULE,
1599 rc2 += dump.td_length;
1601 * Ignore -ENODEV as the regular NRS head's policy may be in the
1602 * ptlrpc_nrs_pol_state::NRS_POL_STATE_STOPPED state.
1604 } else if (rc != -ENODEV) {
1608 if (!nrs_svc_has_hp(svc))
1611 rc2 += snprintf(page + rc2, count - rc2, "high_priority_requests:\n");
1613 dump.td_buff = page + rc2;
1614 dump.td_size = count - rc2;
1615 rc = ptlrpc_nrs_policy_control(svc, PTLRPC_NRS_QUEUE_HP,
1617 NRS_CTL_TBF_RD_RULE,
1621 rc2 += dump.td_length;
1623 * Ignore -ENODEV as the high priority NRS head's policy may be
1624 * in the ptlrpc_nrs_pol_state::NRS_POL_STATE_STOPPED state.
1626 } else if (rc != -ENODEV) {
1635 static int nrs_tbf_id_parse(struct nrs_tbf_cmd *cmd, char **val)
1640 token = strsep(val, "}");
1642 GOTO(out, rc = -EINVAL);
1644 if (strlen(token) <= 1 ||
1646 GOTO(out, rc = -EINVAL);
1650 /* Should be followed by ' ' or nothing */
1651 if ((*val)[0] == '\0')
1653 else if ((*val)[0] == ' ')
1656 GOTO(out, rc = -EINVAL);
1658 rc = nrs_tbf_jobid_parse(cmd, token);
1660 cmd->tc_valid_types |= NRS_TBF_FLAG_JOBID;
1662 rc = nrs_tbf_nid_parse(cmd, token);
1664 cmd->tc_valid_types |= NRS_TBF_FLAG_NID;
1666 if (!cmd->tc_valid_types)
1675 static void nrs_tbf_cmd_fini(struct nrs_tbf_cmd *cmd)
1677 if (cmd->tc_valid_types & NRS_TBF_FLAG_JOBID)
1678 nrs_tbf_jobid_cmd_fini(cmd);
1679 if (cmd->tc_valid_types & NRS_TBF_FLAG_NID)
1680 nrs_tbf_nid_cmd_fini(cmd);
1683 static struct nrs_tbf_cmd *
1684 nrs_tbf_parse_cmd(char *buffer, unsigned long count)
1686 static struct nrs_tbf_cmd *cmd;
1694 GOTO(out, rc = -ENOMEM);
1697 token = strsep(&val, " ");
1698 if (val == NULL || strlen(val) == 0)
1699 GOTO(out_free_cmd, rc = -EINVAL);
1701 /* Type of the command */
1702 if (strcmp(token, "start") == 0)
1703 cmd->tc_cmd = NRS_CTL_TBF_START_RULE;
1704 else if (strcmp(token, "stop") == 0)
1705 cmd->tc_cmd = NRS_CTL_TBF_STOP_RULE;
1706 else if (strcmp(token, "change") == 0)
1707 cmd->tc_cmd = NRS_CTL_TBF_CHANGE_RATE;
1709 GOTO(out_free_cmd, rc = -EINVAL);
1711 /* Name of the rule */
1712 token = strsep(&val, " ");
1715 * Stop comand only need name argument,
1716 * But other commands need ID or rate argument.
1718 if (cmd->tc_cmd != NRS_CTL_TBF_STOP_RULE)
1719 GOTO(out_free_cmd, rc = -EINVAL);
1722 for (i = 0; i < strlen(token); i++) {
1723 if ((!isalnum(token[i])) &&
1725 GOTO(out_free_cmd, rc = -EINVAL);
1727 cmd->tc_name = token;
1729 if (cmd->tc_cmd == NRS_CTL_TBF_START_RULE) {
1732 rc = nrs_tbf_id_parse(cmd, &val);
1734 GOTO(out_free_cmd, rc);
1738 if (cmd->tc_cmd == NRS_CTL_TBF_STOP_RULE ||
1739 strlen(val) == 0 || !isdigit(val[0]))
1740 GOTO(out_free_nid, rc = -EINVAL);
1742 cmd->tc_rpc_rate = simple_strtoull(val, NULL, 10);
1743 if (cmd->tc_rpc_rate <= 0 ||
1744 cmd->tc_rpc_rate >= LPROCFS_NRS_RATE_MAX)
1745 GOTO(out_free_nid, rc = -EINVAL);
1747 if (cmd->tc_cmd == NRS_CTL_TBF_CHANGE_RATE)
1748 GOTO(out_free_nid, rc = -EINVAL);
1749 /* No RPC rate given */
1750 cmd->tc_rpc_rate = tbf_rate;
1754 nrs_tbf_cmd_fini(cmd);
1763 extern struct nrs_core nrs_core;
1764 #define LPROCFS_WR_NRS_TBF_MAX_CMD (4096)
1765 static int ptlrpc_lprocfs_wr_nrs_tbf_rule(struct file *file,
1767 unsigned long count, void *data)
1769 struct ptlrpc_service *svc = data;
1773 static struct nrs_tbf_cmd *cmd;
1774 enum ptlrpc_nrs_queue_type queue = PTLRPC_NRS_QUEUE_BOTH;
1775 unsigned long length;
1778 OBD_ALLOC(kernbuf, LPROCFS_WR_NRS_TBF_MAX_CMD);
1779 if (kernbuf == NULL)
1780 GOTO(out, rc = -ENOMEM);
1782 if (count > LPROCFS_WR_NRS_TBF_MAX_CMD - 1)
1783 GOTO(out_free_kernbuff, rc = -EINVAL);
1785 if (copy_from_user(kernbuf, buffer, count))
1786 GOTO(out_free_kernbuff, rc = -EFAULT);
1789 token = strsep(&val, " ");
1791 GOTO(out_free_kernbuff, rc = -EINVAL);
1793 if (strcmp(token, "reg") == 0) {
1794 queue = PTLRPC_NRS_QUEUE_REG;
1795 } else if (strcmp(token, "hp") == 0) {
1796 queue = PTLRPC_NRS_QUEUE_HP;
1798 kernbuf[strlen(token)] = ' ';
1801 length = strlen(val);
1804 GOTO(out_free_kernbuff, rc = -EINVAL);
1806 if (queue == PTLRPC_NRS_QUEUE_HP && !nrs_svc_has_hp(svc))
1807 GOTO(out_free_kernbuff, rc = -ENODEV);
1808 else if (queue == PTLRPC_NRS_QUEUE_BOTH && !nrs_svc_has_hp(svc))
1809 queue = PTLRPC_NRS_QUEUE_REG;
1811 cmd = nrs_tbf_parse_cmd(val, length);
1813 GOTO(out_free_kernbuff, rc = PTR_ERR(cmd));
1816 * Serialize NRS core lprocfs operations with policy registration/
1819 mutex_lock(&nrs_core.nrs_mutex);
1820 rc = ptlrpc_nrs_policy_control(svc, queue,
1822 NRS_CTL_TBF_WR_RULE,
1824 mutex_unlock(&nrs_core.nrs_mutex);
1826 nrs_tbf_cmd_fini(cmd);
1829 OBD_FREE(kernbuf, LPROCFS_WR_NRS_TBF_MAX_CMD);
1831 return rc ? rc : count;
1836 * Initializes a TBF policy's lprocfs interface for service \a svc
1838 * \param[in] svc the service
1841 * \retval != 0 error
1843 int nrs_tbf_lprocfs_init(struct ptlrpc_service *svc)
1846 struct lprocfs_vars nrs_tbf_lprocfs_vars[] = {
1847 { .name = "nrs_tbf_rule",
1848 .read_fptr = ptlrpc_lprocfs_rd_nrs_tbf_rule,
1849 .write_fptr = ptlrpc_lprocfs_wr_nrs_tbf_rule,
1854 if (svc->srv_procroot == NULL)
1857 rc = lprocfs_add_vars(svc->srv_procroot, nrs_tbf_lprocfs_vars, NULL);
1863 * Cleans up a TBF policy's lprocfs interface for service \a svc
1865 * \param[in] svc the service
1867 void nrs_tbf_lprocfs_fini(struct ptlrpc_service *svc)
1869 if (svc->srv_procroot == NULL)
1872 lprocfs_remove_proc_entry("nrs_tbf_quantum", svc->srv_procroot);
1875 #endif /* LPROCFS */
1878 * TBF policy operations
1880 static const struct ptlrpc_nrs_pol_ops nrs_tbf_ops = {
1881 .op_policy_start = nrs_tbf_start,
1882 .op_policy_stop = nrs_tbf_stop,
1883 .op_policy_ctl = nrs_tbf_ctl,
1884 .op_res_get = nrs_tbf_res_get,
1885 .op_res_put = nrs_tbf_res_put,
1886 .op_req_get = nrs_tbf_req_get,
1887 .op_req_enqueue = nrs_tbf_req_add,
1888 .op_req_dequeue = nrs_tbf_req_del,
1889 .op_req_stop = nrs_tbf_req_stop,
1891 .op_lprocfs_init = nrs_tbf_lprocfs_init,
1892 .op_lprocfs_fini = nrs_tbf_lprocfs_fini,
1897 * TBF policy configuration
1899 struct ptlrpc_nrs_pol_conf nrs_conf_tbf = {
1900 .nc_name = NRS_POL_NAME_TBF,
1901 .nc_ops = &nrs_tbf_ops,
1902 .nc_compat = nrs_policy_compat_all,
1909 #endif /* HAVE_SERVER_SUPPORT */