Whamcloud - gitweb
63c7b3852429e0d8539f9d3307ba7d80537dd021
[fs/lustre-release.git] / lustre / ptlrpc / nrs_tbf.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
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.
9
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.
15
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
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (C) 2013 DataDirect Networks, Inc.
24  *
25  */
26 /*
27  * lustre/ptlrpc/nrs_tbf.c
28  *
29  * Network Request Scheduler (NRS) Token Bucket Filter(TBF) policy
30  *
31  */
32
33 #ifdef HAVE_SERVER_SUPPORT
34
35 /**
36  * \addtogoup nrs
37  * @{
38  */
39
40 #define DEBUG_SUBSYSTEM S_RPC
41 #ifndef __KERNEL__
42 #include <liblustre.h>
43 #endif
44 #include <obd_support.h>
45 #include <obd_class.h>
46 #include <libcfs/libcfs.h>
47 #include "ptlrpc_internal.h"
48
49 /**
50  * \name tbf
51  *
52  * Token Bucket Filter over client NIDs
53  *
54  * @{
55  */
56
57 #define NRS_POL_NAME_TBF        "tbf"
58
59 int tbf_jobid_cache_size = 8192;
60 CFS_MODULE_PARM(tbf_jobid_cache_size, "i", int, 0644,
61                 "The size of jobid cache");
62
63 int tbf_rate = 10000;
64 CFS_MODULE_PARM(tbf_rate, "i", int, 0644,
65                 "Default rate limit in RPCs/s");
66
67 int tbf_depth = 3;
68 CFS_MODULE_PARM(tbf_depth, "i", int, 0644,
69                 "How many tokens that a client can save up");
70
71 static enum hrtimer_restart nrs_tbf_timer_cb(struct hrtimer *timer)
72 {
73         struct nrs_tbf_head *head = container_of(timer, struct nrs_tbf_head,
74                                                  th_timer);
75         struct ptlrpc_nrs   *nrs = head->th_res.res_policy->pol_nrs;
76         struct ptlrpc_service_part *svcpt = nrs->nrs_svcpt;
77
78         spin_lock(&nrs->nrs_lock);
79         nrs->nrs_throttling = 0;
80         spin_unlock(&nrs->nrs_lock);
81         wake_up(&svcpt->scp_waitq);
82
83         return HRTIMER_NORESTART;
84 }
85
86 #define NRS_TBF_DEFAULT_RULE "default"
87
88 static void nrs_tbf_rule_fini(struct nrs_tbf_rule *rule)
89 {
90         LASSERT(atomic_read(&rule->tr_ref) == 0);
91         LASSERT(list_empty(&rule->tr_cli_list));
92         LASSERT(list_empty(&rule->tr_linkage));
93
94         rule->tr_head->th_ops->o_rule_fini(rule);
95         OBD_FREE_PTR(rule);
96 }
97
98 /**
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
102  * stopped).
103  */
104 static void nrs_tbf_rule_put(struct nrs_tbf_rule *rule)
105 {
106         if (atomic_dec_and_test(&rule->tr_ref))
107                 nrs_tbf_rule_fini(rule);
108 }
109
110 /**
111  * Increases the rule's usage reference count.
112  */
113 static inline void nrs_tbf_rule_get(struct nrs_tbf_rule *rule)
114 {
115         atomic_inc(&rule->tr_ref);
116 }
117
118 static void
119 nrs_tbf_cli_rule_put(struct nrs_tbf_client *cli)
120 {
121         LASSERT(!list_empty(&cli->tc_linkage));
122         LASSERT(cli->tc_rule);
123         list_del_init(&cli->tc_linkage);
124         nrs_tbf_rule_put(cli->tc_rule);
125         cli->tc_rule = NULL;
126 }
127
128 static void
129 nrs_tbf_cli_reset_value(struct nrs_tbf_head *head,
130                         struct nrs_tbf_client *cli)
131
132 {
133         struct nrs_tbf_rule *rule = cli->tc_rule;
134
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;
142
143         if (cli->tc_in_heap)
144                 cfs_binheap_relocate(head->th_binheap,
145                                      &cli->tc_node);
146 }
147
148 static void
149 nrs_tbf_cli_reset(struct nrs_tbf_head *head,
150                   struct nrs_tbf_rule *rule,
151                   struct nrs_tbf_client *cli)
152 {
153         if (!list_empty(&cli->tc_linkage)) {
154                 LASSERT(rule != cli->tc_rule);
155                 nrs_tbf_cli_rule_put(cli);
156         }
157         LASSERT(cli->tc_rule == NULL);
158         LASSERT(list_empty(&cli->tc_linkage));
159         /* Rule's ref is added before called */
160         cli->tc_rule = rule;
161         list_add_tail(&cli->tc_linkage, &rule->tr_cli_list);
162         nrs_tbf_cli_reset_value(head, cli);
163 }
164
165 static int
166 nrs_tbf_rule_dump(struct nrs_tbf_rule *rule, struct seq_file *m)
167 {
168         return rule->tr_head->th_ops->o_rule_dump(rule, m);
169 }
170
171 static int
172 nrs_tbf_rule_dump_all(struct nrs_tbf_head *head, struct seq_file *m)
173 {
174         struct nrs_tbf_rule *rule;
175         int rc = 0;
176
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);
183                 if (rc) {
184                         rc = -ENOSPC;
185                         break;
186                 }
187         }
188         spin_unlock(&head->th_rule_lock);
189
190         return rc;
191 }
192
193 static struct nrs_tbf_rule *
194 nrs_tbf_rule_find_nolock(struct nrs_tbf_head *head,
195                          const char *name)
196 {
197         struct nrs_tbf_rule *rule;
198
199         LASSERT(head != NULL);
200         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);
204                         return rule;
205                 }
206         }
207         return NULL;
208 }
209
210 static struct nrs_tbf_rule *
211 nrs_tbf_rule_find(struct nrs_tbf_head *head,
212                   const char *name)
213 {
214         struct nrs_tbf_rule *rule;
215
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);
220         return rule;
221 }
222
223 static struct nrs_tbf_rule *
224 nrs_tbf_rule_match(struct nrs_tbf_head *head,
225                    struct nrs_tbf_client *cli)
226 {
227         struct nrs_tbf_rule *rule = NULL;
228         struct nrs_tbf_rule *tmp_rule;
229
230         spin_lock(&head->th_rule_lock);
231         /* Match the newest rule in the list */
232         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)) {
235                         rule = tmp_rule;
236                         break;
237                 }
238         }
239
240         if (rule == NULL)
241                 rule = head->th_rule;
242
243         nrs_tbf_rule_get(rule);
244         spin_unlock(&head->th_rule_lock);
245         return rule;
246 }
247
248 static void
249 nrs_tbf_cli_init(struct nrs_tbf_head *head,
250                  struct nrs_tbf_client *cli,
251                  struct ptlrpc_request *req)
252 {
253         struct nrs_tbf_rule *rule;
254
255         cli->tc_in_heap = false;
256         head->th_ops->o_cli_init(cli, req);
257         INIT_LIST_HEAD(&cli->tc_list);
258         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);
262 }
263
264 static void
265 nrs_tbf_cli_fini(struct nrs_tbf_client *cli)
266 {
267         LASSERT(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);
271         OBD_FREE_PTR(cli);
272 }
273
274 static int
275 nrs_tbf_rule_start(struct ptlrpc_nrs_policy *policy,
276                    struct nrs_tbf_head *head,
277                    struct nrs_tbf_cmd *start)
278 {
279         struct nrs_tbf_rule *rule, *tmp_rule;
280         int rc;
281
282         rule = nrs_tbf_rule_find(head, start->tc_name);
283         if (rule) {
284                 nrs_tbf_rule_put(rule);
285                 return -EEXIST;
286         }
287
288         OBD_CPT_ALLOC_PTR(rule, nrs_pol2cptab(policy), nrs_pol2cptid(policy));
289         if (rule == NULL)
290                 return -ENOMEM;
291
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         INIT_LIST_HEAD(&rule->tr_cli_list);
298         INIT_LIST_HEAD(&rule->tr_nids);
299
300         rc = head->th_ops->o_rule_init(policy, rule, start);
301         if (rc) {
302                 OBD_FREE_PTR(rule);
303                 return rc;
304         }
305
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);
309         if (tmp_rule) {
310                 spin_unlock(&head->th_rule_lock);
311                 nrs_tbf_rule_put(tmp_rule);
312                 nrs_tbf_rule_put(rule);
313                 return -EEXIST;
314         }
315         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;
323         }
324
325         return 0;
326 }
327
328 static int
329 nrs_tbf_rule_change(struct ptlrpc_nrs_policy *policy,
330                     struct nrs_tbf_head *head,
331                     struct nrs_tbf_cmd *change)
332 {
333         struct nrs_tbf_rule *rule;
334
335         assert_spin_locked(&policy->pol_nrs->nrs_lock);
336
337         rule = nrs_tbf_rule_find(head, change->tc_name);
338         if (rule == NULL)
339                 return -ENOENT;
340
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);
345
346         return 0;
347 }
348
349 static int
350 nrs_tbf_rule_stop(struct ptlrpc_nrs_policy *policy,
351                   struct nrs_tbf_head *head,
352                   struct nrs_tbf_cmd *stop)
353 {
354         struct nrs_tbf_rule *rule;
355
356         assert_spin_locked(&policy->pol_nrs->nrs_lock);
357
358         if (strcmp(stop->tc_name, NRS_TBF_DEFAULT_RULE) == 0)
359                 return -EPERM;
360
361         rule = nrs_tbf_rule_find(head, stop->tc_name);
362         if (rule == NULL)
363                 return -ENOENT;
364
365         list_del_init(&rule->tr_linkage);
366         rule->tr_flags |= NTRS_STOPPING;
367         nrs_tbf_rule_put(rule);
368         nrs_tbf_rule_put(rule);
369
370         return 0;
371 }
372
373 static int
374 nrs_tbf_command(struct ptlrpc_nrs_policy *policy,
375                 struct nrs_tbf_head *head,
376                 struct nrs_tbf_cmd *cmd)
377 {
378         int rc;
379
380         assert_spin_locked(&policy->pol_nrs->nrs_lock);
381
382         switch (cmd->tc_cmd) {
383         case NRS_CTL_TBF_START_RULE:
384                 if (!(cmd->tc_valid_types & head->th_type_flag))
385                         return -EINVAL;
386
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);
390                 return rc;
391         case NRS_CTL_TBF_CHANGE_RATE:
392                 rc = nrs_tbf_rule_change(policy, head, cmd);
393                 return rc;
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;
398         default:
399                 return -EFAULT;
400         }
401 }
402
403 /**
404  * Binary heap predicate.
405  *
406  * \param[in] e1 the first binheap node to compare
407  * \param[in] e2 the second binheap node to compare
408  *
409  * \retval 0 e1 > e2
410  * \retval 1 e1 < e2
411  */
412 static int tbf_cli_compare(cfs_binheap_node_t *e1, cfs_binheap_node_t *e2)
413 {
414         struct nrs_tbf_client *cli1;
415         struct nrs_tbf_client *cli2;
416
417         cli1 = container_of(e1, struct nrs_tbf_client, tc_node);
418         cli2 = container_of(e2, struct nrs_tbf_client, tc_node);
419
420         if (cli1->tc_check_time + cli1->tc_nsecs <
421             cli2->tc_check_time + cli2->tc_nsecs)
422                 return 1;
423         else if (cli1->tc_check_time + cli1->tc_nsecs >
424                  cli2->tc_check_time + cli2->tc_nsecs)
425                 return 0;
426
427         if (cli1->tc_check_time < cli2->tc_check_time)
428                 return 1;
429         else if (cli1->tc_check_time > cli2->tc_check_time)
430                 return 0;
431
432         /* Maybe need more comparasion, e.g. request number in the rules */
433         return 1;
434 }
435
436 /**
437  * TBF binary heap operations
438  */
439 static cfs_binheap_ops_t nrs_tbf_heap_ops = {
440         .hop_enter      = NULL,
441         .hop_exit       = NULL,
442         .hop_compare    = tbf_cli_compare,
443 };
444
445 static unsigned nrs_tbf_jobid_hop_hash(cfs_hash_t *hs, const void *key,
446                                   unsigned mask)
447 {
448         return cfs_hash_djb2_hash(key, strlen(key), mask);
449 }
450
451 static int nrs_tbf_jobid_hop_keycmp(const void *key, struct hlist_node *hnode)
452 {
453         struct nrs_tbf_client *cli = hlist_entry(hnode,
454                                                      struct nrs_tbf_client,
455                                                      tc_hnode);
456
457         return (strcmp(cli->tc_jobid, key) == 0);
458 }
459
460 static void *nrs_tbf_jobid_hop_key(struct hlist_node *hnode)
461 {
462         struct nrs_tbf_client *cli = hlist_entry(hnode,
463                                                      struct nrs_tbf_client,
464                                                      tc_hnode);
465
466         return cli->tc_jobid;
467 }
468
469 static void *nrs_tbf_jobid_hop_object(struct hlist_node *hnode)
470 {
471         return hlist_entry(hnode, struct nrs_tbf_client, tc_hnode);
472 }
473
474 static void nrs_tbf_jobid_hop_get(cfs_hash_t *hs, struct hlist_node *hnode)
475 {
476         struct nrs_tbf_client *cli = hlist_entry(hnode,
477                                                      struct nrs_tbf_client,
478                                                      tc_hnode);
479
480         atomic_inc(&cli->tc_ref);
481 }
482
483 static void nrs_tbf_jobid_hop_put(cfs_hash_t *hs, struct hlist_node *hnode)
484 {
485         struct nrs_tbf_client *cli = hlist_entry(hnode,
486                                                      struct nrs_tbf_client,
487                                                      tc_hnode);
488
489         atomic_dec(&cli->tc_ref);
490 }
491
492 static void nrs_tbf_jobid_hop_exit(cfs_hash_t *hs, struct hlist_node *hnode)
493
494 {
495         struct nrs_tbf_client *cli = hlist_entry(hnode,
496                                                      struct nrs_tbf_client,
497                                                      tc_hnode);
498
499         LASSERT(atomic_read(&cli->tc_ref) == 0);
500         nrs_tbf_cli_fini(cli);
501 }
502
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,
512 };
513
514 #define NRS_TBF_JOBID_HASH_FLAGS (CFS_HASH_SPIN_BKTLOCK | \
515                                   CFS_HASH_NO_ITEMREF | \
516                                   CFS_HASH_DEPTH)
517
518 static struct nrs_tbf_client *
519 nrs_tbf_jobid_hash_lookup(cfs_hash_t *hs,
520                           cfs_hash_bd_t *bd,
521                           const char *jobid)
522 {
523         struct hlist_node *hnode;
524         struct nrs_tbf_client *cli;
525
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);
529         if (hnode == NULL)
530                 return NULL;
531
532         cfs_hash_get(hs, hnode);
533         cli = container_of0(hnode, struct nrs_tbf_client, tc_hnode);
534         if (!list_empty(&cli->tc_lru))
535                 list_del_init(&cli->tc_lru);
536         return cli;
537 }
538
539 #define NRS_TBF_JOBID_NULL ""
540
541 static struct nrs_tbf_client *
542 nrs_tbf_jobid_cli_find(struct nrs_tbf_head *head,
543                        struct ptlrpc_request *req)
544 {
545         const char              *jobid;
546         struct nrs_tbf_client   *cli;
547         cfs_hash_t              *hs = head->th_cli_hash;
548         cfs_hash_bd_t            bd;
549
550         jobid = lustre_msg_get_jobid(req->rq_reqmsg);
551         if (jobid == NULL)
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);
556
557         return cli;
558 }
559
560 static struct nrs_tbf_client *
561 nrs_tbf_jobid_cli_findadd(struct nrs_tbf_head *head,
562                           struct nrs_tbf_client *cli)
563 {
564         const char              *jobid;
565         struct nrs_tbf_client   *ret;
566         cfs_hash_t              *hs = head->th_cli_hash;
567         cfs_hash_bd_t            bd;
568
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);
572         if (ret == NULL) {
573                 cfs_hash_bd_add_locked(hs, &bd, &cli->tc_hnode);
574                 ret = cli;
575         }
576         cfs_hash_bd_unlock(hs, &bd, 1);
577
578         return ret;
579 }
580
581 static void
582 nrs_tbf_jobid_cli_put(struct nrs_tbf_head *head,
583                       struct nrs_tbf_client *cli)
584 {
585         cfs_hash_bd_t            bd;
586         cfs_hash_t              *hs = head->th_cli_hash;
587         struct nrs_tbf_bucket   *bkt;
588         int                      hw;
589         struct list_head        zombies;
590
591         INIT_LIST_HEAD(&zombies);
592         cfs_hash_bd_get(hs, &cli->tc_jobid, &bd);
593         bkt = cfs_hash_bd_extra_get(hs, &bd);
594         if (!cfs_hash_bd_dec_and_lock(hs, &bd, &cli->tc_ref))
595                 return;
596         LASSERT(list_empty(&cli->tc_lru));
597         list_add_tail(&cli->tc_lru, &bkt->ntb_lru);
598
599         /*
600          * Check and purge the LRU, there is at least one client in the LRU.
601          */
602         hw = tbf_jobid_cache_size >>
603              (hs->hs_cur_bits - hs->hs_bkt_bits);
604         while (cfs_hash_bd_count_get(&bd) > hw) {
605                 if (unlikely(list_empty(&bkt->ntb_lru)))
606                         break;
607                 cli = list_entry(bkt->ntb_lru.next,
608                                      struct nrs_tbf_client,
609                                      tc_lru);
610                 LASSERT(atomic_read(&cli->tc_ref) == 0);
611                 cfs_hash_bd_del_locked(hs, &bd, &cli->tc_hnode);
612                 list_move(&cli->tc_lru, &zombies);
613         }
614         cfs_hash_bd_unlock(head->th_cli_hash, &bd, 1);
615
616         while (!list_empty(&zombies)) {
617                 cli = container_of0(zombies.next,
618                                     struct nrs_tbf_client, tc_lru);
619                 list_del_init(&cli->tc_lru);
620                 nrs_tbf_cli_fini(cli);
621         }
622 }
623
624 static void
625 nrs_tbf_jobid_cli_init(struct nrs_tbf_client *cli,
626                        struct ptlrpc_request *req)
627 {
628         char *jobid = lustre_msg_get_jobid(req->rq_reqmsg);
629
630         if (jobid == NULL)
631                 jobid = NRS_TBF_JOBID_NULL;
632         LASSERT(strlen(jobid) < JOBSTATS_JOBID_SIZE);
633         INIT_LIST_HEAD(&cli->tc_lru);
634         memcpy(cli->tc_jobid, jobid, strlen(jobid));
635 }
636
637 static int nrs_tbf_jobid_hash_order(void)
638 {
639         int bits;
640
641         for (bits = 1; (1 << bits) < tbf_jobid_cache_size; ++bits)
642                 ;
643
644         return bits;
645 }
646
647 #define NRS_TBF_JOBID_BKT_BITS 10
648
649 static int
650 nrs_tbf_jobid_startup(struct ptlrpc_nrs_policy *policy,
651                       struct nrs_tbf_head *head)
652 {
653         struct nrs_tbf_cmd       start;
654         struct nrs_tbf_bucket   *bkt;
655         int                      bits;
656         int                      i;
657         int                      rc;
658         cfs_hash_bd_t            bd;
659
660         bits = nrs_tbf_jobid_hash_order();
661         if (bits < NRS_TBF_JOBID_BKT_BITS)
662                 bits = NRS_TBF_JOBID_BKT_BITS;
663         head->th_cli_hash = cfs_hash_create("nrs_tbf_hash",
664                                             bits,
665                                             bits,
666                                             NRS_TBF_JOBID_BKT_BITS,
667                                             sizeof(*bkt),
668                                             0,
669                                             0,
670                                             &nrs_tbf_jobid_hash_ops,
671                                             NRS_TBF_JOBID_HASH_FLAGS);
672         if (head->th_cli_hash == NULL)
673                 return -ENOMEM;
674
675         cfs_hash_for_each_bucket(head->th_cli_hash, &bd, i) {
676                 bkt = cfs_hash_bd_extra_get(head->th_cli_hash, &bd);
677                 INIT_LIST_HEAD(&bkt->ntb_lru);
678         }
679
680         memset(&start, 0, sizeof(start));
681         start.tc_jobids_str = "*";
682
683         start.tc_rpc_rate = tbf_rate;
684         start.tc_rule_flags = NTRS_DEFAULT;
685         start.tc_name = NRS_TBF_DEFAULT_RULE;
686         INIT_LIST_HEAD(&start.tc_jobids);
687         rc = nrs_tbf_rule_start(policy, head, &start);
688
689         return rc;
690 }
691
692 /**
693  * Frees jobid of \a list.
694  *
695  */
696 static void
697 nrs_tbf_jobid_list_free(struct list_head *jobid_list)
698 {
699         struct nrs_tbf_jobid *jobid, *n;
700
701         list_for_each_entry_safe(jobid, n, jobid_list, tj_linkage) {
702                 OBD_FREE(jobid->tj_id, strlen(jobid->tj_id) + 1);
703                 list_del(&jobid->tj_linkage);
704                 OBD_FREE(jobid, sizeof(struct nrs_tbf_jobid));
705         }
706 }
707
708 static int
709 nrs_tbf_jobid_list_add(const struct cfs_lstr *id, struct list_head *jobid_list)
710 {
711         struct nrs_tbf_jobid *jobid;
712
713         OBD_ALLOC(jobid, sizeof(struct nrs_tbf_jobid));
714         if (jobid == NULL)
715                 return -ENOMEM;
716
717         OBD_ALLOC(jobid->tj_id, id->ls_len + 1);
718         if (jobid->tj_id == NULL) {
719                 OBD_FREE(jobid, sizeof(struct nrs_tbf_jobid));
720                 return -ENOMEM;
721         }
722
723         memcpy(jobid->tj_id, id->ls_str, id->ls_len);
724         list_add_tail(&jobid->tj_linkage, jobid_list);
725         return 0;
726 }
727
728 static int
729 nrs_tbf_jobid_list_match(struct list_head *jobid_list, char *id)
730 {
731         struct nrs_tbf_jobid *jobid;
732
733         list_for_each_entry(jobid, jobid_list, tj_linkage) {
734                 if (strcmp(id, jobid->tj_id) == 0)
735                         return 1;
736         }
737         return 0;
738 }
739
740 static int
741 nrs_tbf_jobid_list_parse(char *str, int len, struct list_head *jobid_list)
742 {
743         struct cfs_lstr src;
744         struct cfs_lstr res;
745         int rc = 0;
746         ENTRY;
747
748         src.ls_str = str;
749         src.ls_len = len;
750         INIT_LIST_HEAD(jobid_list);
751         while (src.ls_str) {
752                 rc = cfs_gettok(&src, ' ', &res);
753                 if (rc == 0) {
754                         rc = -EINVAL;
755                         break;
756                 }
757                 rc = nrs_tbf_jobid_list_add(&res, jobid_list);
758                 if (rc)
759                         break;
760         }
761         if (rc)
762                 nrs_tbf_jobid_list_free(jobid_list);
763         RETURN(rc);
764 }
765
766 static void nrs_tbf_jobid_cmd_fini(struct nrs_tbf_cmd *cmd)
767 {
768         if (!list_empty(&cmd->tc_jobids))
769                 nrs_tbf_jobid_list_free(&cmd->tc_jobids);
770         if (cmd->tc_jobids_str)
771                 OBD_FREE(cmd->tc_jobids_str, strlen(cmd->tc_jobids_str) + 1);
772 }
773
774 static int nrs_tbf_jobid_parse(struct nrs_tbf_cmd *cmd, const char *id)
775 {
776         int rc;
777
778         OBD_ALLOC(cmd->tc_jobids_str, strlen(id) + 1);
779         if (cmd->tc_jobids_str == NULL)
780                 return -ENOMEM;
781
782         memcpy(cmd->tc_jobids_str, id, strlen(id));
783
784         /* parse jobid list */
785         rc = nrs_tbf_jobid_list_parse(cmd->tc_jobids_str,
786                                       strlen(cmd->tc_jobids_str),
787                                       &cmd->tc_jobids);
788         if (rc)
789                 nrs_tbf_jobid_cmd_fini(cmd);
790
791         return rc;
792 }
793
794 static int nrs_tbf_jobid_rule_init(struct ptlrpc_nrs_policy *policy,
795                                    struct nrs_tbf_rule *rule,
796                                    struct nrs_tbf_cmd *start)
797 {
798         int rc = 0;
799
800         LASSERT(start->tc_jobids_str);
801         OBD_ALLOC(rule->tr_jobids_str,
802                   strlen(start->tc_jobids_str) + 1);
803         if (rule->tr_jobids_str == NULL)
804                 return -ENOMEM;
805
806         memcpy(rule->tr_jobids_str,
807                start->tc_jobids_str,
808                strlen(start->tc_jobids_str));
809
810         INIT_LIST_HEAD(&rule->tr_jobids);
811         if (!list_empty(&start->tc_jobids)) {
812                 rc = nrs_tbf_jobid_list_parse(rule->tr_jobids_str,
813                                               strlen(rule->tr_jobids_str),
814                                               &rule->tr_jobids);
815                 if (rc)
816                         CERROR("jobids {%s} illegal\n", rule->tr_jobids_str);
817         }
818         if (rc)
819                 OBD_FREE(rule->tr_jobids_str,
820                          strlen(start->tc_jobids_str) + 1);
821         return rc;
822 }
823
824 static int
825 nrs_tbf_jobid_rule_dump(struct nrs_tbf_rule *rule, struct seq_file *m)
826 {
827         return seq_printf(m, "%s {%s} %llu, ref %d\n", rule->tr_name,
828                           rule->tr_jobids_str, rule->tr_rpc_rate,
829                           atomic_read(&rule->tr_ref) - 1);
830 }
831
832 static int
833 nrs_tbf_jobid_rule_match(struct nrs_tbf_rule *rule,
834                          struct nrs_tbf_client *cli)
835 {
836         return nrs_tbf_jobid_list_match(&rule->tr_jobids, cli->tc_jobid);
837 }
838
839 static void nrs_tbf_jobid_rule_fini(struct nrs_tbf_rule *rule)
840 {
841         if (!list_empty(&rule->tr_jobids))
842                 nrs_tbf_jobid_list_free(&rule->tr_jobids);
843         LASSERT(rule->tr_jobids_str != NULL);
844         OBD_FREE(rule->tr_jobids_str, strlen(rule->tr_jobids_str) + 1);
845 }
846
847 struct nrs_tbf_ops nrs_tbf_jobid_ops = {
848         .o_name = NRS_TBF_TYPE_JOBID,
849         .o_startup = nrs_tbf_jobid_startup,
850         .o_cli_find = nrs_tbf_jobid_cli_find,
851         .o_cli_findadd = nrs_tbf_jobid_cli_findadd,
852         .o_cli_put = nrs_tbf_jobid_cli_put,
853         .o_cli_init = nrs_tbf_jobid_cli_init,
854         .o_rule_init = nrs_tbf_jobid_rule_init,
855         .o_rule_dump = nrs_tbf_jobid_rule_dump,
856         .o_rule_match = nrs_tbf_jobid_rule_match,
857         .o_rule_fini = nrs_tbf_jobid_rule_fini,
858 };
859
860 /**
861  * libcfs_hash operations for nrs_tbf_net::cn_cli_hash
862  *
863  * This uses ptlrpc_request::rq_peer.nid as its key, in order to hash
864  * nrs_tbf_client objects.
865  */
866 #define NRS_TBF_NID_BKT_BITS    8
867 #define NRS_TBF_NID_BITS        16
868
869 static unsigned nrs_tbf_nid_hop_hash(cfs_hash_t *hs, const void *key,
870                                   unsigned mask)
871 {
872         return cfs_hash_djb2_hash(key, sizeof(lnet_nid_t), mask);
873 }
874
875 static int nrs_tbf_nid_hop_keycmp(const void *key, struct hlist_node *hnode)
876 {
877         lnet_nid_t            *nid = (lnet_nid_t *)key;
878         struct nrs_tbf_client *cli = hlist_entry(hnode,
879                                                      struct nrs_tbf_client,
880                                                      tc_hnode);
881
882         return *nid == cli->tc_nid;
883 }
884
885 static void *nrs_tbf_nid_hop_key(struct hlist_node *hnode)
886 {
887         struct nrs_tbf_client *cli = hlist_entry(hnode,
888                                                      struct nrs_tbf_client,
889                                                      tc_hnode);
890
891         return &cli->tc_nid;
892 }
893
894 static void *nrs_tbf_nid_hop_object(struct hlist_node *hnode)
895 {
896         return hlist_entry(hnode, struct nrs_tbf_client, tc_hnode);
897 }
898
899 static void nrs_tbf_nid_hop_get(cfs_hash_t *hs, struct hlist_node *hnode)
900 {
901         struct nrs_tbf_client *cli = hlist_entry(hnode,
902                                                      struct nrs_tbf_client,
903                                                      tc_hnode);
904
905         atomic_inc(&cli->tc_ref);
906 }
907
908 static void nrs_tbf_nid_hop_put(cfs_hash_t *hs, struct hlist_node *hnode)
909 {
910         struct nrs_tbf_client *cli = hlist_entry(hnode,
911                                                      struct nrs_tbf_client,
912                                                      tc_hnode);
913
914         atomic_dec(&cli->tc_ref);
915 }
916
917 static void nrs_tbf_nid_hop_exit(cfs_hash_t *hs, struct hlist_node *hnode)
918 {
919         struct nrs_tbf_client *cli = hlist_entry(hnode,
920                                                      struct nrs_tbf_client,
921                                                      tc_hnode);
922
923         LASSERTF(atomic_read(&cli->tc_ref) == 0,
924                  "Busy TBF object from client with NID %s, with %d refs\n",
925                  libcfs_nid2str(cli->tc_nid), atomic_read(&cli->tc_ref));
926
927         nrs_tbf_cli_fini(cli);
928 }
929
930 static cfs_hash_ops_t nrs_tbf_nid_hash_ops = {
931         .hs_hash        = nrs_tbf_nid_hop_hash,
932         .hs_keycmp      = nrs_tbf_nid_hop_keycmp,
933         .hs_key         = nrs_tbf_nid_hop_key,
934         .hs_object      = nrs_tbf_nid_hop_object,
935         .hs_get         = nrs_tbf_nid_hop_get,
936         .hs_put         = nrs_tbf_nid_hop_put,
937         .hs_put_locked  = nrs_tbf_nid_hop_put,
938         .hs_exit        = nrs_tbf_nid_hop_exit,
939 };
940
941 static struct nrs_tbf_client *
942 nrs_tbf_nid_cli_find(struct nrs_tbf_head *head,
943                      struct ptlrpc_request *req)
944 {
945         return cfs_hash_lookup(head->th_cli_hash, &req->rq_peer.nid);
946 }
947
948 static struct nrs_tbf_client *
949 nrs_tbf_nid_cli_findadd(struct nrs_tbf_head *head,
950                         struct nrs_tbf_client *cli)
951 {
952         return cfs_hash_findadd_unique(head->th_cli_hash, &cli->tc_nid,
953                                        &cli->tc_hnode);
954 }
955
956 static void
957 nrs_tbf_nid_cli_put(struct nrs_tbf_head *head,
958                       struct nrs_tbf_client *cli)
959 {
960         cfs_hash_put(head->th_cli_hash, &cli->tc_hnode);
961 }
962
963 static int
964 nrs_tbf_nid_startup(struct ptlrpc_nrs_policy *policy,
965                     struct nrs_tbf_head *head)
966 {
967         struct nrs_tbf_cmd      start;
968         int rc;
969
970         head->th_cli_hash = cfs_hash_create("nrs_tbf_hash",
971                                             NRS_TBF_NID_BITS,
972                                             NRS_TBF_NID_BITS,
973                                             NRS_TBF_NID_BKT_BITS, 0,
974                                             CFS_HASH_MIN_THETA,
975                                             CFS_HASH_MAX_THETA,
976                                             &nrs_tbf_nid_hash_ops,
977                                             CFS_HASH_RW_BKTLOCK);
978         if (head->th_cli_hash == NULL)
979                 return -ENOMEM;
980
981         memset(&start, 0, sizeof(start));
982         start.tc_nids_str = "*";
983
984         start.tc_rpc_rate = tbf_rate;
985         start.tc_rule_flags = NTRS_DEFAULT;
986         start.tc_name = NRS_TBF_DEFAULT_RULE;
987         INIT_LIST_HEAD(&start.tc_nids);
988         rc = nrs_tbf_rule_start(policy, head, &start);
989
990         return rc;
991 }
992
993 static void
994 nrs_tbf_nid_cli_init(struct nrs_tbf_client *cli,
995                              struct ptlrpc_request *req)
996 {
997         cli->tc_nid = req->rq_peer.nid;
998 }
999
1000 static int nrs_tbf_nid_rule_init(struct ptlrpc_nrs_policy *policy,
1001                                  struct nrs_tbf_rule *rule,
1002                                  struct nrs_tbf_cmd *start)
1003 {
1004         LASSERT(start->tc_nids_str);
1005         OBD_ALLOC(rule->tr_nids_str,
1006                   strlen(start->tc_nids_str) + 1);
1007         if (rule->tr_nids_str == NULL)
1008                 return -ENOMEM;
1009
1010         memcpy(rule->tr_nids_str,
1011                start->tc_nids_str,
1012                strlen(start->tc_nids_str));
1013
1014         INIT_LIST_HEAD(&rule->tr_nids);
1015         if (!list_empty(&start->tc_nids)) {
1016                 if (cfs_parse_nidlist(rule->tr_nids_str,
1017                                       strlen(rule->tr_nids_str),
1018                                       &rule->tr_nids) <= 0) {
1019                         CERROR("nids {%s} illegal\n",
1020                                rule->tr_nids_str);
1021                         OBD_FREE(rule->tr_nids_str,
1022                                  strlen(start->tc_nids_str) + 1);
1023                         return -EINVAL;
1024                 }
1025         }
1026         return 0;
1027 }
1028
1029 static int
1030 nrs_tbf_nid_rule_dump(struct nrs_tbf_rule *rule, struct seq_file *m)
1031 {
1032         return seq_printf(m, "%s {%s} %llu, ref %d\n", rule->tr_name,
1033                           rule->tr_nids_str, rule->tr_rpc_rate,
1034                           atomic_read(&rule->tr_ref) - 1);
1035 }
1036
1037 static int
1038 nrs_tbf_nid_rule_match(struct nrs_tbf_rule *rule,
1039                        struct nrs_tbf_client *cli)
1040 {
1041         return cfs_match_nid(cli->tc_nid, &rule->tr_nids);
1042 }
1043
1044 static void nrs_tbf_nid_rule_fini(struct nrs_tbf_rule *rule)
1045 {
1046         if (!list_empty(&rule->tr_nids))
1047                 cfs_free_nidlist(&rule->tr_nids);
1048         LASSERT(rule->tr_nids_str != NULL);
1049         OBD_FREE(rule->tr_nids_str, strlen(rule->tr_nids_str) + 1);
1050 }
1051
1052 static void nrs_tbf_nid_cmd_fini(struct nrs_tbf_cmd *cmd)
1053 {
1054         if (!list_empty(&cmd->tc_nids))
1055                 cfs_free_nidlist(&cmd->tc_nids);
1056         if (cmd->tc_nids_str)
1057                 OBD_FREE(cmd->tc_nids_str, strlen(cmd->tc_nids_str) + 1);
1058 }
1059
1060 static int nrs_tbf_nid_parse(struct nrs_tbf_cmd *cmd, const char *id)
1061 {
1062         OBD_ALLOC(cmd->tc_nids_str, strlen(id) + 1);
1063         if (cmd->tc_nids_str == NULL)
1064                 return -ENOMEM;
1065
1066         memcpy(cmd->tc_nids_str, id, strlen(id));
1067
1068         /* parse NID list */
1069         if (cfs_parse_nidlist(cmd->tc_nids_str,
1070                               strlen(cmd->tc_nids_str),
1071                               &cmd->tc_nids) <= 0) {
1072                 nrs_tbf_nid_cmd_fini(cmd);
1073                 return -EINVAL;
1074         }
1075
1076         return 0;
1077 }
1078
1079 struct nrs_tbf_ops nrs_tbf_nid_ops = {
1080         .o_name = NRS_TBF_TYPE_NID,
1081         .o_startup = nrs_tbf_nid_startup,
1082         .o_cli_find = nrs_tbf_nid_cli_find,
1083         .o_cli_findadd = nrs_tbf_nid_cli_findadd,
1084         .o_cli_put = nrs_tbf_nid_cli_put,
1085         .o_cli_init = nrs_tbf_nid_cli_init,
1086         .o_rule_init = nrs_tbf_nid_rule_init,
1087         .o_rule_dump = nrs_tbf_nid_rule_dump,
1088         .o_rule_match = nrs_tbf_nid_rule_match,
1089         .o_rule_fini = nrs_tbf_nid_rule_fini,
1090 };
1091
1092 /**
1093  * Is called before the policy transitions into
1094  * ptlrpc_nrs_pol_state::NRS_POL_STATE_STARTED; allocates and initializes a
1095  * policy-specific private data structure.
1096  *
1097  * \param[in] policy The policy to start
1098  *
1099  * \retval -ENOMEM OOM error
1100  * \retval  0      success
1101  *
1102  * \see nrs_policy_register()
1103  * \see nrs_policy_ctl()
1104  */
1105 static int nrs_tbf_start(struct ptlrpc_nrs_policy *policy, char *arg)
1106 {
1107         struct nrs_tbf_head     *head;
1108         struct nrs_tbf_ops      *ops;
1109         __u32                    type;
1110         int rc = 0;
1111
1112         if (arg == NULL || strlen(arg) > NRS_TBF_TYPE_MAX_LEN)
1113                 GOTO(out, rc = -EINVAL);
1114
1115         if (strcmp(arg, NRS_TBF_TYPE_NID) == 0) {
1116                 ops = &nrs_tbf_nid_ops;
1117                 type = NRS_TBF_FLAG_NID;
1118         } else if (strcmp(arg, NRS_TBF_TYPE_JOBID) == 0) {
1119                 ops = &nrs_tbf_jobid_ops;
1120                 type = NRS_TBF_FLAG_JOBID;
1121         } else
1122                 GOTO(out, rc = -ENOTSUPP);
1123
1124         OBD_CPT_ALLOC_PTR(head, nrs_pol2cptab(policy), nrs_pol2cptid(policy));
1125         if (head == NULL)
1126                 GOTO(out, rc = -ENOMEM);
1127
1128         memcpy(head->th_type, arg, strlen(arg));
1129         head->th_type[strlen(arg)] = '\0';
1130         head->th_ops = ops;
1131         head->th_type_flag = type;
1132
1133         head->th_binheap = cfs_binheap_create(&nrs_tbf_heap_ops,
1134                                               CBH_FLAG_ATOMIC_GROW, 4096, NULL,
1135                                               nrs_pol2cptab(policy),
1136                                               nrs_pol2cptid(policy));
1137         if (head->th_binheap == NULL)
1138                 GOTO(out_free_head, rc = -ENOMEM);
1139
1140         atomic_set(&head->th_rule_sequence, 0);
1141         spin_lock_init(&head->th_rule_lock);
1142         INIT_LIST_HEAD(&head->th_list);
1143         hrtimer_init(&head->th_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
1144         head->th_timer.function = nrs_tbf_timer_cb;
1145         rc = head->th_ops->o_startup(policy, head);
1146         if (rc)
1147                 GOTO(out_free_heap, rc);
1148
1149         policy->pol_private = head;
1150         return 0;
1151 out_free_heap:
1152         cfs_binheap_destroy(head->th_binheap);
1153 out_free_head:
1154         OBD_FREE_PTR(head);
1155 out:
1156         return rc;
1157 }
1158
1159 /**
1160  * Is called before the policy transitions into
1161  * ptlrpc_nrs_pol_state::NRS_POL_STATE_STOPPED; deallocates the policy-specific
1162  * private data structure.
1163  *
1164  * \param[in] policy The policy to stop
1165  *
1166  * \see nrs_policy_stop0()
1167  */
1168 static void nrs_tbf_stop(struct ptlrpc_nrs_policy *policy)
1169 {
1170         struct nrs_tbf_head *head = policy->pol_private;
1171         struct ptlrpc_nrs *nrs = policy->pol_nrs;
1172         struct nrs_tbf_rule *rule, *n;
1173
1174         LASSERT(head != NULL);
1175         LASSERT(head->th_cli_hash != NULL);
1176         hrtimer_cancel(&head->th_timer);
1177         /* Should cleanup hash first before free rules */
1178         cfs_hash_putref(head->th_cli_hash);
1179         list_for_each_entry_safe(rule, n, &head->th_list, tr_linkage) {
1180                 list_del_init(&rule->tr_linkage);
1181                 nrs_tbf_rule_put(rule);
1182         }
1183         LASSERT(list_empty(&head->th_list));
1184         LASSERT(head->th_binheap != NULL);
1185         LASSERT(cfs_binheap_is_empty(head->th_binheap));
1186         cfs_binheap_destroy(head->th_binheap);
1187         OBD_FREE_PTR(head);
1188         spin_lock(&nrs->nrs_lock);
1189         nrs->nrs_throttling = 0;
1190         spin_unlock(&nrs->nrs_lock);
1191         wake_up(&policy->pol_nrs->nrs_svcpt->scp_waitq);
1192 }
1193
1194 /**
1195  * Performs a policy-specific ctl function on TBF policy instances; similar
1196  * to ioctl.
1197  *
1198  * \param[in]     policy the policy instance
1199  * \param[in]     opc    the opcode
1200  * \param[in,out] arg    used for passing parameters and information
1201  *
1202  * \pre assert_spin_locked(&policy->pol_nrs->->nrs_lock)
1203  * \post assert_spin_locked(&policy->pol_nrs->->nrs_lock)
1204  *
1205  * \retval 0   operation carried out successfully
1206  * \retval -ve error
1207  */
1208 int nrs_tbf_ctl(struct ptlrpc_nrs_policy *policy, enum ptlrpc_nrs_ctl opc,
1209                 void *arg)
1210 {
1211         int rc = 0;
1212         ENTRY;
1213
1214         assert_spin_locked(&policy->pol_nrs->nrs_lock);
1215
1216         switch ((enum nrs_ctl_tbf)opc) {
1217         default:
1218                 RETURN(-EINVAL);
1219
1220         /**
1221          * Read RPC rate size of a policy instance.
1222          */
1223         case NRS_CTL_TBF_RD_RULE: {
1224                 struct nrs_tbf_head *head = policy->pol_private;
1225                 struct seq_file *m = (struct seq_file *) arg;
1226                 struct ptlrpc_service_part *svcpt;
1227
1228                 svcpt = policy->pol_nrs->nrs_svcpt;
1229                 seq_printf(m, "CPT %d:\n", svcpt->scp_cpt);
1230
1231                 rc = nrs_tbf_rule_dump_all(head, m);
1232                 }
1233                 break;
1234
1235         /**
1236          * Write RPC rate of a policy instance.
1237          */
1238         case NRS_CTL_TBF_WR_RULE: {
1239                 struct nrs_tbf_head *head = policy->pol_private;
1240                 struct nrs_tbf_cmd *cmd;
1241
1242                 cmd = (struct nrs_tbf_cmd *)arg;
1243                 rc = nrs_tbf_command(policy,
1244                                      head,
1245                                      cmd);
1246                 }
1247                 break;
1248         }
1249
1250         RETURN(rc);
1251 }
1252
1253 /**
1254  * Is called for obtaining a TBF policy resource.
1255  *
1256  * \param[in]  policy     The policy on which the request is being asked for
1257  * \param[in]  nrq        The request for which resources are being taken
1258  * \param[in]  parent     Parent resource, unused in this policy
1259  * \param[out] resp       Resources references are placed in this array
1260  * \param[in]  moving_req Signifies limited caller context; unused in this
1261  *                        policy
1262  *
1263  *
1264  * \see nrs_resource_get_safe()
1265  */
1266 static int nrs_tbf_res_get(struct ptlrpc_nrs_policy *policy,
1267                            struct ptlrpc_nrs_request *nrq,
1268                            const struct ptlrpc_nrs_resource *parent,
1269                            struct ptlrpc_nrs_resource **resp,
1270                            bool moving_req)
1271 {
1272         struct nrs_tbf_head   *head;
1273         struct nrs_tbf_client *cli;
1274         struct nrs_tbf_client *tmp;
1275         struct ptlrpc_request *req;
1276
1277         if (parent == NULL) {
1278                 *resp = &((struct nrs_tbf_head *)policy->pol_private)->th_res;
1279                 return 0;
1280         }
1281
1282         head = container_of(parent, struct nrs_tbf_head, th_res);
1283         req = container_of(nrq, struct ptlrpc_request, rq_nrq);
1284         cli = head->th_ops->o_cli_find(head, req);
1285         if (cli != NULL) {
1286                 spin_lock(&policy->pol_nrs->nrs_svcpt->scp_req_lock);
1287                 LASSERT(cli->tc_rule);
1288                 if (cli->tc_rule_sequence !=
1289                     atomic_read(&head->th_rule_sequence) ||
1290                     cli->tc_rule->tr_flags & NTRS_STOPPING) {
1291                         struct nrs_tbf_rule *rule;
1292
1293                         rule = nrs_tbf_rule_match(head, cli);
1294                         if (rule != cli->tc_rule)
1295                                 nrs_tbf_cli_reset(head, rule, cli);
1296                         else
1297                                 nrs_tbf_rule_put(rule);
1298                 } else if (cli->tc_rule_generation !=
1299                            cli->tc_rule->tr_generation) {
1300                         nrs_tbf_cli_reset_value(head, cli);
1301                 }
1302                 spin_unlock(&policy->pol_nrs->nrs_svcpt->scp_req_lock);
1303                 goto out;
1304         }
1305
1306         OBD_CPT_ALLOC_GFP(cli, nrs_pol2cptab(policy), nrs_pol2cptid(policy),
1307                           sizeof(*cli), moving_req ? GFP_ATOMIC : __GFP_IO);
1308         if (cli == NULL)
1309                 return -ENOMEM;
1310         nrs_tbf_cli_init(head, cli, req);
1311         tmp = head->th_ops->o_cli_findadd(head, cli);
1312         if (tmp != cli) {
1313                 atomic_dec(&cli->tc_ref);
1314                 nrs_tbf_cli_fini(cli);
1315                 cli = tmp;
1316         }
1317 out:
1318         *resp = &cli->tc_res;
1319
1320         return 1;
1321 }
1322
1323 /**
1324  * Called when releasing references to the resource hierachy obtained for a
1325  * request for scheduling using the TBF policy.
1326  *
1327  * \param[in] policy   the policy the resource belongs to
1328  * \param[in] res      the resource to be released
1329  */
1330 static void nrs_tbf_res_put(struct ptlrpc_nrs_policy *policy,
1331                             const struct ptlrpc_nrs_resource *res)
1332 {
1333         struct nrs_tbf_head   *head;
1334         struct nrs_tbf_client *cli;
1335
1336         /**
1337          * Do nothing for freeing parent, nrs_tbf_net resources
1338          */
1339         if (res->res_parent == NULL)
1340                 return;
1341
1342         cli = container_of(res, struct nrs_tbf_client, tc_res);
1343         head = container_of(res->res_parent, struct nrs_tbf_head, th_res);
1344
1345         head->th_ops->o_cli_put(head, cli);
1346 }
1347
1348 /**
1349  * Called when getting a request from the TBF policy for handling, or just
1350  * peeking; removes the request from the policy when it is to be handled.
1351  *
1352  * \param[in] policy The policy
1353  * \param[in] peek   When set, signifies that we just want to examine the
1354  *                   request, and not handle it, so the request is not removed
1355  *                   from the policy.
1356  * \param[in] force  Force the policy to return a request; unused in this
1357  *                   policy
1358  *
1359  * \retval The request to be handled; this is the next request in the TBF
1360  *         rule
1361  *
1362  * \see ptlrpc_nrs_req_get_nolock()
1363  * \see nrs_request_get()
1364  */
1365 static
1366 struct ptlrpc_nrs_request *nrs_tbf_req_get(struct ptlrpc_nrs_policy *policy,
1367                                            bool peek, bool force)
1368 {
1369         struct nrs_tbf_head       *head = policy->pol_private;
1370         struct ptlrpc_nrs_request *nrq = NULL;
1371         struct nrs_tbf_client     *cli;
1372         cfs_binheap_node_t        *node;
1373
1374         assert_spin_locked(&policy->pol_nrs->nrs_svcpt->scp_req_lock);
1375
1376         if (!peek && policy->pol_nrs->nrs_throttling)
1377                 return NULL;
1378
1379         node = cfs_binheap_root(head->th_binheap);
1380         if (unlikely(node == NULL))
1381                 return NULL;
1382
1383         cli = container_of(node, struct nrs_tbf_client, tc_node);
1384         LASSERT(cli->tc_in_heap);
1385         if (peek) {
1386                 nrq = list_entry(cli->tc_list.next,
1387                                      struct ptlrpc_nrs_request,
1388                                      nr_u.tbf.tr_list);
1389         } else {
1390                 __u64 now = ktime_to_ns(ktime_get());
1391                 __u64 passed;
1392                 long  ntoken;
1393                 __u64 deadline;
1394
1395                 deadline = cli->tc_check_time +
1396                           cli->tc_nsecs;
1397                 LASSERT(now >= cli->tc_check_time);
1398                 passed = now - cli->tc_check_time;
1399                 ntoken = (passed * cli->tc_rpc_rate) / NSEC_PER_SEC;
1400                 ntoken += cli->tc_ntoken;
1401                 if (ntoken > cli->tc_depth)
1402                         ntoken = cli->tc_depth;
1403                 if (ntoken > 0) {
1404                         struct ptlrpc_request *req;
1405                         nrq = list_entry(cli->tc_list.next,
1406                                              struct ptlrpc_nrs_request,
1407                                              nr_u.tbf.tr_list);
1408                         req = container_of(nrq,
1409                                            struct ptlrpc_request,
1410                                            rq_nrq);
1411                         ntoken--;
1412                         cli->tc_ntoken = ntoken;
1413                         cli->tc_check_time = now;
1414                         list_del_init(&nrq->nr_u.tbf.tr_list);
1415                         if (list_empty(&cli->tc_list)) {
1416                                 cfs_binheap_remove(head->th_binheap,
1417                                                    &cli->tc_node);
1418                                 cli->tc_in_heap = false;
1419                         } else {
1420                                 cfs_binheap_relocate(head->th_binheap,
1421                                                      &cli->tc_node);
1422                         }
1423                         CDEBUG(D_RPCTRACE,
1424                                "NRS start %s request from %s, "
1425                                "seq: "LPU64"\n",
1426                                policy->pol_desc->pd_name,
1427                                libcfs_id2str(req->rq_peer),
1428                                nrq->nr_u.tbf.tr_sequence);
1429                 } else {
1430                         ktime_t time;
1431
1432                         spin_lock(&policy->pol_nrs->nrs_lock);
1433                         policy->pol_nrs->nrs_throttling = 1;
1434                         spin_unlock(&policy->pol_nrs->nrs_lock);
1435                         head->th_deadline = deadline;
1436                         time = ktime_set(0, 0);
1437                         time = ktime_add_ns(time, deadline);
1438                         hrtimer_start(&head->th_timer, time, HRTIMER_MODE_ABS);
1439                 }
1440         }
1441
1442         return nrq;
1443 }
1444
1445 /**
1446  * Adds request \a nrq to \a policy's list of queued requests
1447  *
1448  * \param[in] policy The policy
1449  * \param[in] nrq    The request to add
1450  *
1451  * \retval 0 success; nrs_request_enqueue() assumes this function will always
1452  *                    succeed
1453  */
1454 static int nrs_tbf_req_add(struct ptlrpc_nrs_policy *policy,
1455                            struct ptlrpc_nrs_request *nrq)
1456 {
1457         struct nrs_tbf_head   *head;
1458         struct nrs_tbf_client *cli;
1459         int                    rc = 0;
1460
1461         assert_spin_locked(&policy->pol_nrs->nrs_svcpt->scp_req_lock);
1462
1463         cli = container_of(nrs_request_resource(nrq),
1464                            struct nrs_tbf_client, tc_res);
1465         head = container_of(nrs_request_resource(nrq)->res_parent,
1466                             struct nrs_tbf_head, th_res);
1467         if (list_empty(&cli->tc_list)) {
1468                 LASSERT(!cli->tc_in_heap);
1469                 rc = cfs_binheap_insert(head->th_binheap, &cli->tc_node);
1470                 if (rc == 0) {
1471                         cli->tc_in_heap = true;
1472                         nrq->nr_u.tbf.tr_sequence = head->th_sequence++;
1473                         list_add_tail(&nrq->nr_u.tbf.tr_list,
1474                                           &cli->tc_list);
1475                         if (policy->pol_nrs->nrs_throttling) {
1476                                 __u64 deadline = cli->tc_check_time +
1477                                                  cli->tc_nsecs;
1478                                 if ((head->th_deadline > deadline) &&
1479                                     (hrtimer_try_to_cancel(&head->th_timer)
1480                                      >= 0)) {
1481                                         ktime_t time;
1482                                         head->th_deadline = deadline;
1483                                         time = ktime_set(0, 0);
1484                                         time = ktime_add_ns(time, deadline);
1485                                         hrtimer_start(&head->th_timer, time,
1486                                                       HRTIMER_MODE_ABS);
1487                                 }
1488                         }
1489                 }
1490         } else {
1491                 LASSERT(cli->tc_in_heap);
1492                 nrq->nr_u.tbf.tr_sequence = head->th_sequence++;
1493                 list_add_tail(&nrq->nr_u.tbf.tr_list,
1494                                   &cli->tc_list);
1495         }
1496         return rc;
1497 }
1498
1499 /**
1500  * Removes request \a nrq from \a policy's list of queued requests.
1501  *
1502  * \param[in] policy The policy
1503  * \param[in] nrq    The request to remove
1504  */
1505 static void nrs_tbf_req_del(struct ptlrpc_nrs_policy *policy,
1506                              struct ptlrpc_nrs_request *nrq)
1507 {
1508         struct nrs_tbf_head   *head;
1509         struct nrs_tbf_client *cli;
1510
1511         assert_spin_locked(&policy->pol_nrs->nrs_svcpt->scp_req_lock);
1512
1513         cli = container_of(nrs_request_resource(nrq),
1514                            struct nrs_tbf_client, tc_res);
1515         head = container_of(nrs_request_resource(nrq)->res_parent,
1516                             struct nrs_tbf_head, th_res);
1517
1518         LASSERT(!list_empty(&nrq->nr_u.tbf.tr_list));
1519         list_del_init(&nrq->nr_u.tbf.tr_list);
1520         if (list_empty(&cli->tc_list)) {
1521                 cfs_binheap_remove(head->th_binheap,
1522                                    &cli->tc_node);
1523                 cli->tc_in_heap = false;
1524         } else {
1525                 cfs_binheap_relocate(head->th_binheap,
1526                                      &cli->tc_node);
1527         }
1528 }
1529
1530 /**
1531  * Prints a debug statement right before the request \a nrq stops being
1532  * handled.
1533  *
1534  * \param[in] policy The policy handling the request
1535  * \param[in] nrq    The request being handled
1536  *
1537  * \see ptlrpc_server_finish_request()
1538  * \see ptlrpc_nrs_req_stop_nolock()
1539  */
1540 static void nrs_tbf_req_stop(struct ptlrpc_nrs_policy *policy,
1541                               struct ptlrpc_nrs_request *nrq)
1542 {
1543         struct ptlrpc_request *req = container_of(nrq, struct ptlrpc_request,
1544                                                   rq_nrq);
1545
1546         assert_spin_locked(&policy->pol_nrs->nrs_svcpt->scp_req_lock);
1547
1548         CDEBUG(D_RPCTRACE, "NRS stop %s request from %s, seq: "LPU64"\n",
1549                policy->pol_desc->pd_name, libcfs_id2str(req->rq_peer),
1550                nrq->nr_u.tbf.tr_sequence);
1551 }
1552
1553 #ifdef LPROCFS
1554
1555 /**
1556  * lprocfs interface
1557  */
1558
1559 /**
1560  * The maximum RPC rate.
1561  */
1562 #define LPROCFS_NRS_RATE_MAX            65535
1563
1564 static int
1565 ptlrpc_lprocfs_nrs_tbf_rule_seq_show(struct seq_file *m, void *data)
1566 {
1567         struct ptlrpc_service       *svc = m->private;
1568         int                          rc;
1569
1570         seq_printf(m, "regular_requests:\n");
1571         /**
1572          * Perform two separate calls to this as only one of the NRS heads'
1573          * policies may be in the ptlrpc_nrs_pol_state::NRS_POL_STATE_STARTED or
1574          * ptlrpc_nrs_pol_state::NRS_POL_STATE_STOPPING state.
1575          */
1576         rc = ptlrpc_nrs_policy_control(svc, PTLRPC_NRS_QUEUE_REG,
1577                                        NRS_POL_NAME_TBF,
1578                                        NRS_CTL_TBF_RD_RULE,
1579                                        false, m);
1580         if (rc == 0) {
1581                 /**
1582                  * -ENOSPC means buf in the parameter m is overflow, return 0
1583                  * here to let upper layer function seq_read alloc a larger
1584                  * memory area and do this process again.
1585                  */
1586         } else if (rc == -ENOSPC) {
1587                 return 0;
1588
1589                 /**
1590                  * Ignore -ENODEV as the regular NRS head's policy may be in the
1591                  * ptlrpc_nrs_pol_state::NRS_POL_STATE_STOPPED state.
1592                  */
1593         } else if (rc != -ENODEV) {
1594                 return rc;
1595         }
1596
1597         if (!nrs_svc_has_hp(svc))
1598                 goto no_hp;
1599
1600         seq_printf(m, "high_priority_requests:\n");
1601         rc = ptlrpc_nrs_policy_control(svc, PTLRPC_NRS_QUEUE_HP,
1602                                        NRS_POL_NAME_TBF,
1603                                        NRS_CTL_TBF_RD_RULE,
1604                                        false, m);
1605         if (rc == 0) {
1606                 /**
1607                  * -ENOSPC means buf in the parameter m is overflow, return 0
1608                  * here to let upper layer function seq_read alloc a larger
1609                  * memory area and do this process again.
1610                  */
1611         } else if (rc == -ENOSPC) {
1612                 return 0;
1613         }
1614
1615 no_hp:
1616
1617         return rc;
1618 }
1619
1620 static int nrs_tbf_id_parse(struct nrs_tbf_cmd *cmd, char **val)
1621 {
1622         int rc;
1623         char *token;
1624
1625         token = strsep(val, "}");
1626         if (*val == NULL)
1627                 GOTO(out, rc = -EINVAL);
1628
1629         if (strlen(token) <= 1 ||
1630             token[0] != '{')
1631                 GOTO(out, rc = -EINVAL);
1632         /* Skip '{' */
1633         token++;
1634
1635         /* Should be followed by ' ' or nothing */
1636         if ((*val)[0] == '\0')
1637                 *val = NULL;
1638         else if ((*val)[0] == ' ')
1639                 (*val)++;
1640         else
1641                 GOTO(out, rc = -EINVAL);
1642
1643         rc = nrs_tbf_jobid_parse(cmd, token);
1644         if (!rc)
1645                 cmd->tc_valid_types |= NRS_TBF_FLAG_JOBID;
1646
1647         rc = nrs_tbf_nid_parse(cmd, token);
1648         if (!rc)
1649                 cmd->tc_valid_types |= NRS_TBF_FLAG_NID;
1650
1651         if (!cmd->tc_valid_types)
1652                 rc = -EINVAL;
1653         else
1654                 rc = 0;
1655 out:
1656         return rc;
1657 }
1658
1659
1660 static void nrs_tbf_cmd_fini(struct nrs_tbf_cmd *cmd)
1661 {
1662         if (cmd->tc_valid_types & NRS_TBF_FLAG_JOBID)
1663                 nrs_tbf_jobid_cmd_fini(cmd);
1664         if (cmd->tc_valid_types & NRS_TBF_FLAG_NID)
1665                 nrs_tbf_nid_cmd_fini(cmd);
1666 }
1667
1668 static struct nrs_tbf_cmd *
1669 nrs_tbf_parse_cmd(char *buffer, unsigned long count)
1670 {
1671         static struct nrs_tbf_cmd *cmd;
1672         char                      *token;
1673         char                      *val;
1674         int                        i;
1675         int                        rc = 0;
1676
1677         OBD_ALLOC_PTR(cmd);
1678         if (cmd == NULL)
1679                 GOTO(out, rc = -ENOMEM);
1680
1681         val = buffer;
1682         token = strsep(&val, " ");
1683         if (val == NULL || strlen(val) == 0)
1684                 GOTO(out_free_cmd, rc = -EINVAL);
1685
1686         /* Type of the command */
1687         if (strcmp(token, "start") == 0)
1688                 cmd->tc_cmd = NRS_CTL_TBF_START_RULE;
1689         else if (strcmp(token, "stop") == 0)
1690                 cmd->tc_cmd = NRS_CTL_TBF_STOP_RULE;
1691         else if (strcmp(token, "change") == 0)
1692                 cmd->tc_cmd = NRS_CTL_TBF_CHANGE_RATE;
1693         else
1694                 GOTO(out_free_cmd, rc = -EINVAL);
1695
1696         /* Name of the rule */
1697         token = strsep(&val, " ");
1698         if (val == NULL) {
1699                 /**
1700                  * Stop comand only need name argument,
1701                  * But other commands need ID or rate argument.
1702                  */
1703                 if (cmd->tc_cmd != NRS_CTL_TBF_STOP_RULE)
1704                         GOTO(out_free_cmd, rc = -EINVAL);
1705         }
1706
1707         for (i = 0; i < strlen(token); i++) {
1708                 if ((!isalnum(token[i])) &&
1709                     (token[i] != '_'))
1710                         GOTO(out_free_cmd, rc = -EINVAL);
1711         }
1712         cmd->tc_name = token;
1713
1714         if (cmd->tc_cmd == NRS_CTL_TBF_START_RULE) {
1715                 /* List of ID */
1716                 LASSERT(val);
1717                 rc = nrs_tbf_id_parse(cmd, &val);
1718                 if (rc)
1719                         GOTO(out_free_cmd, rc);
1720         }
1721
1722         if (val != NULL) {
1723                 if (cmd->tc_cmd == NRS_CTL_TBF_STOP_RULE ||
1724                     strlen(val) == 0 || !isdigit(val[0]))
1725                         GOTO(out_free_nid, rc = -EINVAL);
1726
1727                 cmd->tc_rpc_rate = simple_strtoull(val, NULL, 10);
1728                 if (cmd->tc_rpc_rate <= 0 ||
1729                     cmd->tc_rpc_rate >= LPROCFS_NRS_RATE_MAX)
1730                         GOTO(out_free_nid, rc = -EINVAL);
1731         } else {
1732                 if (cmd->tc_cmd == NRS_CTL_TBF_CHANGE_RATE)
1733                         GOTO(out_free_nid, rc = -EINVAL);
1734                 /* No RPC rate given */
1735                 cmd->tc_rpc_rate = tbf_rate;
1736         }
1737         goto out;
1738 out_free_nid:
1739         nrs_tbf_cmd_fini(cmd);
1740 out_free_cmd:
1741         OBD_FREE_PTR(cmd);
1742 out:
1743         if (rc)
1744                 cmd = ERR_PTR(rc);
1745         return cmd;
1746 }
1747
1748 extern struct nrs_core nrs_core;
1749 #define LPROCFS_WR_NRS_TBF_MAX_CMD (4096)
1750 static ssize_t
1751 ptlrpc_lprocfs_nrs_tbf_rule_seq_write(struct file *file, const char *buffer,
1752                                       size_t count, loff_t *off)
1753 {
1754         struct seq_file           *m = file->private_data;
1755         struct ptlrpc_service     *svc = m->private;
1756         char                      *kernbuf;
1757         char                      *val;
1758         int                        rc;
1759         static struct nrs_tbf_cmd *cmd;
1760         enum ptlrpc_nrs_queue_type queue = PTLRPC_NRS_QUEUE_BOTH;
1761         unsigned long              length;
1762         char                      *token;
1763
1764         OBD_ALLOC(kernbuf, LPROCFS_WR_NRS_TBF_MAX_CMD);
1765         if (kernbuf == NULL)
1766                 GOTO(out, rc = -ENOMEM);
1767
1768         if (count > LPROCFS_WR_NRS_TBF_MAX_CMD - 1)
1769                 GOTO(out_free_kernbuff, rc = -EINVAL);
1770
1771         if (copy_from_user(kernbuf, buffer, count))
1772                 GOTO(out_free_kernbuff, rc = -EFAULT);
1773
1774         val = kernbuf;
1775         token = strsep(&val, " ");
1776         if (val == NULL)
1777                 GOTO(out_free_kernbuff, rc = -EINVAL);
1778
1779         if (strcmp(token, "reg") == 0) {
1780                 queue = PTLRPC_NRS_QUEUE_REG;
1781         } else if (strcmp(token, "hp") == 0) {
1782                 queue = PTLRPC_NRS_QUEUE_HP;
1783         } else {
1784                 kernbuf[strlen(token)] = ' ';
1785                 val = kernbuf;
1786         }
1787         length = strlen(val);
1788
1789         if (length == 0)
1790                 GOTO(out_free_kernbuff, rc = -EINVAL);
1791
1792         if (queue == PTLRPC_NRS_QUEUE_HP && !nrs_svc_has_hp(svc))
1793                 GOTO(out_free_kernbuff, rc = -ENODEV);
1794         else if (queue == PTLRPC_NRS_QUEUE_BOTH && !nrs_svc_has_hp(svc))
1795                 queue = PTLRPC_NRS_QUEUE_REG;
1796
1797         cmd = nrs_tbf_parse_cmd(val, length);
1798         if (IS_ERR(cmd))
1799                 GOTO(out_free_kernbuff, rc = PTR_ERR(cmd));
1800
1801         /**
1802          * Serialize NRS core lprocfs operations with policy registration/
1803          * unregistration.
1804          */
1805         mutex_lock(&nrs_core.nrs_mutex);
1806         rc = ptlrpc_nrs_policy_control(svc, queue,
1807                                        NRS_POL_NAME_TBF,
1808                                        NRS_CTL_TBF_WR_RULE,
1809                                        false, cmd);
1810         mutex_unlock(&nrs_core.nrs_mutex);
1811
1812         nrs_tbf_cmd_fini(cmd);
1813         OBD_FREE_PTR(cmd);
1814 out_free_kernbuff:
1815         OBD_FREE(kernbuf, LPROCFS_WR_NRS_TBF_MAX_CMD);
1816 out:
1817         return rc ? rc : count;
1818 }
1819 LPROC_SEQ_FOPS(ptlrpc_lprocfs_nrs_tbf_rule);
1820
1821 /**
1822  * Initializes a TBF policy's lprocfs interface for service \a svc
1823  *
1824  * \param[in] svc the service
1825  *
1826  * \retval 0    success
1827  * \retval != 0 error
1828  */
1829 int nrs_tbf_lprocfs_init(struct ptlrpc_service *svc)
1830 {
1831         struct lprocfs_seq_vars nrs_tbf_lprocfs_vars[] = {
1832                 { .name         = "nrs_tbf_rule",
1833                   .fops         = &ptlrpc_lprocfs_nrs_tbf_rule_fops,
1834                   .data = svc },
1835                 { NULL }
1836         };
1837
1838         if (svc->srv_procroot == NULL)
1839                 return 0;
1840
1841         return lprocfs_seq_add_vars(svc->srv_procroot, nrs_tbf_lprocfs_vars,
1842                                     NULL);
1843 }
1844
1845 /**
1846  * Cleans up a TBF policy's lprocfs interface for service \a svc
1847  *
1848  * \param[in] svc the service
1849  */
1850 void nrs_tbf_lprocfs_fini(struct ptlrpc_service *svc)
1851 {
1852         if (svc->srv_procroot == NULL)
1853                 return;
1854
1855         lprocfs_remove_proc_entry("nrs_tbf_rule", svc->srv_procroot);
1856 }
1857
1858 #endif /* LPROCFS */
1859
1860 /**
1861  * TBF policy operations
1862  */
1863 static const struct ptlrpc_nrs_pol_ops nrs_tbf_ops = {
1864         .op_policy_start        = nrs_tbf_start,
1865         .op_policy_stop         = nrs_tbf_stop,
1866         .op_policy_ctl          = nrs_tbf_ctl,
1867         .op_res_get             = nrs_tbf_res_get,
1868         .op_res_put             = nrs_tbf_res_put,
1869         .op_req_get             = nrs_tbf_req_get,
1870         .op_req_enqueue         = nrs_tbf_req_add,
1871         .op_req_dequeue         = nrs_tbf_req_del,
1872         .op_req_stop            = nrs_tbf_req_stop,
1873 #ifdef LPROCFS
1874         .op_lprocfs_init        = nrs_tbf_lprocfs_init,
1875         .op_lprocfs_fini        = nrs_tbf_lprocfs_fini,
1876 #endif
1877 };
1878
1879 /**
1880  * TBF policy configuration
1881  */
1882 struct ptlrpc_nrs_pol_conf nrs_conf_tbf = {
1883         .nc_name                = NRS_POL_NAME_TBF,
1884         .nc_ops                 = &nrs_tbf_ops,
1885         .nc_compat              = nrs_policy_compat_all,
1886 };
1887
1888 /** @} tbf */
1889
1890 /** @} nrs */
1891
1892 #endif /* HAVE_SERVER_SUPPORT */