Whamcloud - gitweb
LU-9227 nrs: Rate change of a TBF rule loses control
[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  * Copyright (c) 2014, 2016, Intel Corporation.
26  */
27 /*
28  * lustre/ptlrpc/nrs_tbf.c
29  *
30  * Network Request Scheduler (NRS) Token Bucket Filter(TBF) policy
31  *
32  */
33
34 #ifdef HAVE_SERVER_SUPPORT
35
36 /**
37  * \addtogoup nrs
38  * @{
39  */
40
41 #define DEBUG_SUBSYSTEM S_RPC
42 #include <obd_support.h>
43 #include <obd_class.h>
44 #include <libcfs/libcfs.h>
45 #include "ptlrpc_internal.h"
46
47 /**
48  * \name tbf
49  *
50  * Token Bucket Filter over client NIDs
51  *
52  * @{
53  */
54
55 #define NRS_POL_NAME_TBF        "tbf"
56
57 static int tbf_jobid_cache_size = 8192;
58 module_param(tbf_jobid_cache_size, int, 0644);
59 MODULE_PARM_DESC(tbf_jobid_cache_size, "The size of jobid cache");
60
61 static int tbf_rate = 10000;
62 module_param(tbf_rate, int, 0644);
63 MODULE_PARM_DESC(tbf_rate, "Default rate limit in RPCs/s");
64
65 static int tbf_depth = 3;
66 module_param(tbf_depth, int, 0644);
67 MODULE_PARM_DESC(tbf_depth, "How many tokens that a client can save up");
68
69 static enum hrtimer_restart nrs_tbf_timer_cb(struct hrtimer *timer)
70 {
71         struct nrs_tbf_head *head = container_of(timer, struct nrs_tbf_head,
72                                                  th_timer);
73         struct ptlrpc_nrs   *nrs = head->th_res.res_policy->pol_nrs;
74         struct ptlrpc_service_part *svcpt = nrs->nrs_svcpt;
75
76         nrs->nrs_throttling = 0;
77         wake_up(&svcpt->scp_waitq);
78
79         return HRTIMER_NORESTART;
80 }
81
82 #define NRS_TBF_DEFAULT_RULE "default"
83
84 static void nrs_tbf_rule_fini(struct nrs_tbf_rule *rule)
85 {
86         LASSERT(atomic_read(&rule->tr_ref) == 0);
87         LASSERT(list_empty(&rule->tr_cli_list));
88         LASSERT(list_empty(&rule->tr_linkage));
89
90         rule->tr_head->th_ops->o_rule_fini(rule);
91         OBD_FREE_PTR(rule);
92 }
93
94 /**
95  * Decreases the rule's usage reference count, and stops the rule in case it
96  * was already stopping and have no more outstanding usage references (which
97  * indicates it has no more queued or started requests, and can be safely
98  * stopped).
99  */
100 static void nrs_tbf_rule_put(struct nrs_tbf_rule *rule)
101 {
102         if (atomic_dec_and_test(&rule->tr_ref))
103                 nrs_tbf_rule_fini(rule);
104 }
105
106 /**
107  * Increases the rule's usage reference count.
108  */
109 static inline void nrs_tbf_rule_get(struct nrs_tbf_rule *rule)
110 {
111         atomic_inc(&rule->tr_ref);
112 }
113
114 static void
115 nrs_tbf_cli_rule_put(struct nrs_tbf_client *cli)
116 {
117         LASSERT(!list_empty(&cli->tc_linkage));
118         LASSERT(cli->tc_rule);
119         spin_lock(&cli->tc_rule->tr_rule_lock);
120         list_del_init(&cli->tc_linkage);
121         spin_unlock(&cli->tc_rule->tr_rule_lock);
122         nrs_tbf_rule_put(cli->tc_rule);
123         cli->tc_rule = NULL;
124 }
125
126 static void
127 nrs_tbf_cli_reset_value(struct nrs_tbf_head *head,
128                         struct nrs_tbf_client *cli)
129
130 {
131         struct nrs_tbf_rule *rule = cli->tc_rule;
132
133         cli->tc_rpc_rate = rule->tr_rpc_rate;
134         cli->tc_nsecs = rule->tr_nsecs;
135         cli->tc_depth = rule->tr_depth;
136         cli->tc_ntoken = rule->tr_depth;
137         cli->tc_check_time = ktime_to_ns(ktime_get());
138         cli->tc_rule_sequence = atomic_read(&head->th_rule_sequence);
139         cli->tc_rule_generation = rule->tr_generation;
140
141         if (cli->tc_in_heap)
142                 cfs_binheap_relocate(head->th_binheap,
143                                      &cli->tc_node);
144 }
145
146 static void
147 nrs_tbf_cli_reset(struct nrs_tbf_head *head,
148                   struct nrs_tbf_rule *rule,
149                   struct nrs_tbf_client *cli)
150 {
151         spin_lock(&cli->tc_rule_lock);
152         if (cli->tc_rule != NULL && !list_empty(&cli->tc_linkage)) {
153                 LASSERT(rule != cli->tc_rule);
154                 nrs_tbf_cli_rule_put(cli);
155         }
156         LASSERT(cli->tc_rule == NULL);
157         LASSERT(list_empty(&cli->tc_linkage));
158         /* Rule's ref is added before called */
159         cli->tc_rule = rule;
160         spin_lock(&rule->tr_rule_lock);
161         list_add_tail(&cli->tc_linkage, &rule->tr_cli_list);
162         spin_unlock(&rule->tr_rule_lock);
163         spin_unlock(&cli->tc_rule_lock);
164         nrs_tbf_cli_reset_value(head, cli);
165 }
166
167 static int
168 nrs_tbf_rule_dump(struct nrs_tbf_rule *rule, struct seq_file *m)
169 {
170         return rule->tr_head->th_ops->o_rule_dump(rule, m);
171 }
172
173 static int
174 nrs_tbf_rule_dump_all(struct nrs_tbf_head *head, struct seq_file *m)
175 {
176         struct nrs_tbf_rule *rule;
177         int rc = 0;
178
179         LASSERT(head != NULL);
180         spin_lock(&head->th_rule_lock);
181         /* List the rules from newest to oldest */
182         list_for_each_entry(rule, &head->th_list, tr_linkage) {
183                 LASSERT((rule->tr_flags & NTRS_STOPPING) == 0);
184                 rc = nrs_tbf_rule_dump(rule, m);
185                 if (rc) {
186                         rc = -ENOSPC;
187                         break;
188                 }
189         }
190         spin_unlock(&head->th_rule_lock);
191
192         return rc;
193 }
194
195 static struct nrs_tbf_rule *
196 nrs_tbf_rule_find_nolock(struct nrs_tbf_head *head,
197                          const char *name)
198 {
199         struct nrs_tbf_rule *rule;
200
201         LASSERT(head != NULL);
202         list_for_each_entry(rule, &head->th_list, tr_linkage) {
203                 LASSERT((rule->tr_flags & NTRS_STOPPING) == 0);
204                 if (strcmp(rule->tr_name, name) == 0) {
205                         nrs_tbf_rule_get(rule);
206                         return rule;
207                 }
208         }
209         return NULL;
210 }
211
212 static struct nrs_tbf_rule *
213 nrs_tbf_rule_find(struct nrs_tbf_head *head,
214                   const char *name)
215 {
216         struct nrs_tbf_rule *rule;
217
218         LASSERT(head != NULL);
219         spin_lock(&head->th_rule_lock);
220         rule = nrs_tbf_rule_find_nolock(head, name);
221         spin_unlock(&head->th_rule_lock);
222         return rule;
223 }
224
225 static struct nrs_tbf_rule *
226 nrs_tbf_rule_match(struct nrs_tbf_head *head,
227                    struct nrs_tbf_client *cli)
228 {
229         struct nrs_tbf_rule *rule = NULL;
230         struct nrs_tbf_rule *tmp_rule;
231
232         spin_lock(&head->th_rule_lock);
233         /* Match the newest rule in the list */
234         list_for_each_entry(tmp_rule, &head->th_list, tr_linkage) {
235                 LASSERT((tmp_rule->tr_flags & NTRS_STOPPING) == 0);
236                 if (head->th_ops->o_rule_match(tmp_rule, cli)) {
237                         rule = tmp_rule;
238                         break;
239                 }
240         }
241
242         if (rule == NULL)
243                 rule = head->th_rule;
244
245         nrs_tbf_rule_get(rule);
246         spin_unlock(&head->th_rule_lock);
247         return rule;
248 }
249
250 static void
251 nrs_tbf_cli_init(struct nrs_tbf_head *head,
252                  struct nrs_tbf_client *cli,
253                  struct ptlrpc_request *req)
254 {
255         struct nrs_tbf_rule *rule;
256
257         memset(cli, 0, sizeof(*cli));
258         cli->tc_in_heap = false;
259         head->th_ops->o_cli_init(cli, req);
260         INIT_LIST_HEAD(&cli->tc_list);
261         INIT_LIST_HEAD(&cli->tc_linkage);
262         spin_lock_init(&cli->tc_rule_lock);
263         atomic_set(&cli->tc_ref, 1);
264         rule = nrs_tbf_rule_match(head, cli);
265         nrs_tbf_cli_reset(head, rule, cli);
266 }
267
268 static void
269 nrs_tbf_cli_fini(struct nrs_tbf_client *cli)
270 {
271         LASSERT(list_empty(&cli->tc_list));
272         LASSERT(!cli->tc_in_heap);
273         LASSERT(atomic_read(&cli->tc_ref) == 0);
274         spin_lock(&cli->tc_rule_lock);
275         nrs_tbf_cli_rule_put(cli);
276         spin_unlock(&cli->tc_rule_lock);
277         OBD_FREE_PTR(cli);
278 }
279
280 static int
281 nrs_tbf_rule_start(struct ptlrpc_nrs_policy *policy,
282                    struct nrs_tbf_head *head,
283                    struct nrs_tbf_cmd *start)
284 {
285         struct nrs_tbf_rule     *rule;
286         struct nrs_tbf_rule     *tmp_rule;
287         struct nrs_tbf_rule     *next_rule;
288         char                    *next_name = start->u.tc_start.ts_next_name;
289         int                      rc;
290
291         rule = nrs_tbf_rule_find(head, start->tc_name);
292         if (rule) {
293                 nrs_tbf_rule_put(rule);
294                 return -EEXIST;
295         }
296
297         OBD_CPT_ALLOC_PTR(rule, nrs_pol2cptab(policy), nrs_pol2cptid(policy));
298         if (rule == NULL)
299                 return -ENOMEM;
300
301         memcpy(rule->tr_name, start->tc_name, strlen(start->tc_name));
302         rule->tr_rpc_rate = start->u.tc_start.ts_rpc_rate;
303         rule->tr_nsecs = NSEC_PER_SEC;
304         do_div(rule->tr_nsecs, rule->tr_rpc_rate);
305         rule->tr_depth = tbf_depth;
306         atomic_set(&rule->tr_ref, 1);
307         INIT_LIST_HEAD(&rule->tr_cli_list);
308         INIT_LIST_HEAD(&rule->tr_nids);
309         INIT_LIST_HEAD(&rule->tr_linkage);
310         spin_lock_init(&rule->tr_rule_lock);
311         rule->tr_head = head;
312
313         rc = head->th_ops->o_rule_init(policy, rule, start);
314         if (rc) {
315                 OBD_FREE_PTR(rule);
316                 return rc;
317         }
318
319         /* Add as the newest rule */
320         spin_lock(&head->th_rule_lock);
321         tmp_rule = nrs_tbf_rule_find_nolock(head, start->tc_name);
322         if (tmp_rule) {
323                 spin_unlock(&head->th_rule_lock);
324                 nrs_tbf_rule_put(tmp_rule);
325                 nrs_tbf_rule_put(rule);
326                 return -EEXIST;
327         }
328
329         if (next_name) {
330                 next_rule = nrs_tbf_rule_find_nolock(head, next_name);
331                 if (!next_rule) {
332                         spin_unlock(&head->th_rule_lock);
333                         nrs_tbf_rule_put(rule);
334                         return -ENOENT;
335                 }
336
337                 list_add(&rule->tr_linkage, next_rule->tr_linkage.prev);
338                 nrs_tbf_rule_put(next_rule);
339         } else {
340                 /* Add on the top of the rule list */
341                 list_add(&rule->tr_linkage, &head->th_list);
342         }
343         spin_unlock(&head->th_rule_lock);
344         atomic_inc(&head->th_rule_sequence);
345         if (start->u.tc_start.ts_rule_flags & NTRS_DEFAULT) {
346                 rule->tr_flags |= NTRS_DEFAULT;
347                 LASSERT(head->th_rule == NULL);
348                 head->th_rule = rule;
349         }
350
351         return 0;
352 }
353
354 /**
355  * Change the rank of a rule in the rule list
356  *
357  * The matched rule will be moved to the position right before another
358  * given rule.
359  *
360  * \param[in] policy    the policy instance
361  * \param[in] head      the TBF policy instance
362  * \param[in] name      the rule name to be moved
363  * \param[in] next_name the rule name before which the matched rule will be
364  *                      moved
365  *
366  */
367 static int
368 nrs_tbf_rule_change_rank(struct ptlrpc_nrs_policy *policy,
369                          struct nrs_tbf_head *head,
370                          char *name,
371                          char *next_name)
372 {
373         struct nrs_tbf_rule     *rule = NULL;
374         struct nrs_tbf_rule     *next_rule = NULL;
375         int                      rc = 0;
376
377         LASSERT(head != NULL);
378
379         spin_lock(&head->th_rule_lock);
380         rule = nrs_tbf_rule_find_nolock(head, name);
381         if (!rule)
382                 GOTO(out, rc = -ENOENT);
383
384         if (strcmp(name, next_name) == 0)
385                 GOTO(out_put, rc);
386
387         next_rule = nrs_tbf_rule_find_nolock(head, next_name);
388         if (!next_rule)
389                 GOTO(out_put, rc = -ENOENT);
390
391         list_move(&rule->tr_linkage, next_rule->tr_linkage.prev);
392         nrs_tbf_rule_put(next_rule);
393 out_put:
394         nrs_tbf_rule_put(rule);
395 out:
396         spin_unlock(&head->th_rule_lock);
397         return rc;
398 }
399
400 static int
401 nrs_tbf_rule_change_rate(struct ptlrpc_nrs_policy *policy,
402                          struct nrs_tbf_head *head,
403                          char *name,
404                          __u64 rate)
405 {
406         struct nrs_tbf_rule *rule;
407
408         assert_spin_locked(&policy->pol_nrs->nrs_lock);
409
410         rule = nrs_tbf_rule_find(head, name);
411         if (rule == NULL)
412                 return -ENOENT;
413
414         rule->tr_rpc_rate = rate;
415         rule->tr_nsecs = NSEC_PER_SEC;
416         do_div(rule->tr_nsecs, rule->tr_rpc_rate);
417         rule->tr_generation++;
418         nrs_tbf_rule_put(rule);
419
420         return 0;
421 }
422
423 static int
424 nrs_tbf_rule_change(struct ptlrpc_nrs_policy *policy,
425                     struct nrs_tbf_head *head,
426                     struct nrs_tbf_cmd *change)
427 {
428         __u64    rate = change->u.tc_change.tc_rpc_rate;
429         char    *next_name = change->u.tc_change.tc_next_name;
430         int      rc;
431
432         if (rate != 0) {
433                 rc = nrs_tbf_rule_change_rate(policy, head, change->tc_name,
434                                               rate);
435                 if (rc)
436                         return rc;
437         }
438
439         if (next_name) {
440                 rc = nrs_tbf_rule_change_rank(policy, head, change->tc_name,
441                                               next_name);
442                 if (rc)
443                         return rc;
444         }
445
446         return 0;
447 }
448
449 static int
450 nrs_tbf_rule_stop(struct ptlrpc_nrs_policy *policy,
451                   struct nrs_tbf_head *head,
452                   struct nrs_tbf_cmd *stop)
453 {
454         struct nrs_tbf_rule *rule;
455
456         assert_spin_locked(&policy->pol_nrs->nrs_lock);
457
458         if (strcmp(stop->tc_name, NRS_TBF_DEFAULT_RULE) == 0)
459                 return -EPERM;
460
461         rule = nrs_tbf_rule_find(head, stop->tc_name);
462         if (rule == NULL)
463                 return -ENOENT;
464
465         list_del_init(&rule->tr_linkage);
466         rule->tr_flags |= NTRS_STOPPING;
467         nrs_tbf_rule_put(rule);
468         nrs_tbf_rule_put(rule);
469
470         return 0;
471 }
472
473 static int
474 nrs_tbf_command(struct ptlrpc_nrs_policy *policy,
475                 struct nrs_tbf_head *head,
476                 struct nrs_tbf_cmd *cmd)
477 {
478         int rc;
479
480         assert_spin_locked(&policy->pol_nrs->nrs_lock);
481
482         switch (cmd->tc_cmd) {
483         case NRS_CTL_TBF_START_RULE:
484                 if (cmd->u.tc_start.ts_valid_type != head->th_type_flag)
485                         return -EINVAL;
486
487                 spin_unlock(&policy->pol_nrs->nrs_lock);
488                 rc = nrs_tbf_rule_start(policy, head, cmd);
489                 spin_lock(&policy->pol_nrs->nrs_lock);
490                 return rc;
491         case NRS_CTL_TBF_CHANGE_RULE:
492                 rc = nrs_tbf_rule_change(policy, head, cmd);
493                 return rc;
494         case NRS_CTL_TBF_STOP_RULE:
495                 rc = nrs_tbf_rule_stop(policy, head, cmd);
496                 /* Take it as a success, if not exists at all */
497                 return rc == -ENOENT ? 0 : rc;
498         default:
499                 return -EFAULT;
500         }
501 }
502
503 /**
504  * Binary heap predicate.
505  *
506  * \param[in] e1 the first binheap node to compare
507  * \param[in] e2 the second binheap node to compare
508  *
509  * \retval 0 e1 > e2
510  * \retval 1 e1 < e2
511  */
512 static int
513 tbf_cli_compare(struct cfs_binheap_node *e1, struct cfs_binheap_node *e2)
514 {
515         struct nrs_tbf_client *cli1;
516         struct nrs_tbf_client *cli2;
517
518         cli1 = container_of(e1, struct nrs_tbf_client, tc_node);
519         cli2 = container_of(e2, struct nrs_tbf_client, tc_node);
520
521         if (cli1->tc_check_time + cli1->tc_nsecs <
522             cli2->tc_check_time + cli2->tc_nsecs)
523                 return 1;
524         else if (cli1->tc_check_time + cli1->tc_nsecs >
525                  cli2->tc_check_time + cli2->tc_nsecs)
526                 return 0;
527
528         if (cli1->tc_check_time < cli2->tc_check_time)
529                 return 1;
530         else if (cli1->tc_check_time > cli2->tc_check_time)
531                 return 0;
532
533         /* Maybe need more comparasion, e.g. request number in the rules */
534         return 1;
535 }
536
537 /**
538  * TBF binary heap operations
539  */
540 static struct cfs_binheap_ops nrs_tbf_heap_ops = {
541         .hop_enter      = NULL,
542         .hop_exit       = NULL,
543         .hop_compare    = tbf_cli_compare,
544 };
545
546 static unsigned nrs_tbf_jobid_hop_hash(struct cfs_hash *hs, const void *key,
547                                   unsigned mask)
548 {
549         return cfs_hash_djb2_hash(key, strlen(key), mask);
550 }
551
552 static int nrs_tbf_jobid_hop_keycmp(const void *key, struct hlist_node *hnode)
553 {
554         struct nrs_tbf_client *cli = hlist_entry(hnode,
555                                                      struct nrs_tbf_client,
556                                                      tc_hnode);
557
558         return (strcmp(cli->tc_jobid, key) == 0);
559 }
560
561 static void *nrs_tbf_jobid_hop_key(struct hlist_node *hnode)
562 {
563         struct nrs_tbf_client *cli = hlist_entry(hnode,
564                                                      struct nrs_tbf_client,
565                                                      tc_hnode);
566
567         return cli->tc_jobid;
568 }
569
570 static void *nrs_tbf_jobid_hop_object(struct hlist_node *hnode)
571 {
572         return hlist_entry(hnode, struct nrs_tbf_client, tc_hnode);
573 }
574
575 static void nrs_tbf_jobid_hop_get(struct cfs_hash *hs, struct hlist_node *hnode)
576 {
577         struct nrs_tbf_client *cli = hlist_entry(hnode,
578                                                      struct nrs_tbf_client,
579                                                      tc_hnode);
580
581         atomic_inc(&cli->tc_ref);
582 }
583
584 static void nrs_tbf_jobid_hop_put(struct cfs_hash *hs, struct hlist_node *hnode)
585 {
586         struct nrs_tbf_client *cli = hlist_entry(hnode,
587                                                      struct nrs_tbf_client,
588                                                      tc_hnode);
589
590         atomic_dec(&cli->tc_ref);
591 }
592
593 static void
594 nrs_tbf_jobid_hop_exit(struct cfs_hash *hs, struct hlist_node *hnode)
595
596 {
597         struct nrs_tbf_client *cli = hlist_entry(hnode,
598                                                  struct nrs_tbf_client,
599                                                  tc_hnode);
600
601         LASSERT(atomic_read(&cli->tc_ref) == 0);
602         nrs_tbf_cli_fini(cli);
603 }
604
605 static struct cfs_hash_ops nrs_tbf_jobid_hash_ops = {
606         .hs_hash        = nrs_tbf_jobid_hop_hash,
607         .hs_keycmp      = nrs_tbf_jobid_hop_keycmp,
608         .hs_key         = nrs_tbf_jobid_hop_key,
609         .hs_object      = nrs_tbf_jobid_hop_object,
610         .hs_get         = nrs_tbf_jobid_hop_get,
611         .hs_put         = nrs_tbf_jobid_hop_put,
612         .hs_put_locked  = nrs_tbf_jobid_hop_put,
613         .hs_exit        = nrs_tbf_jobid_hop_exit,
614 };
615
616 #define NRS_TBF_JOBID_HASH_FLAGS (CFS_HASH_SPIN_BKTLOCK | \
617                                   CFS_HASH_NO_ITEMREF | \
618                                   CFS_HASH_DEPTH)
619
620 static struct nrs_tbf_client *
621 nrs_tbf_jobid_hash_lookup(struct cfs_hash *hs,
622                           struct cfs_hash_bd *bd,
623                           const char *jobid)
624 {
625         struct hlist_node *hnode;
626         struct nrs_tbf_client *cli;
627
628         hnode = cfs_hash_bd_lookup_locked(hs, bd, (void *)jobid);
629         if (hnode == NULL)
630                 return NULL;
631
632         cli = container_of0(hnode, struct nrs_tbf_client, tc_hnode);
633         if (!list_empty(&cli->tc_lru))
634                 list_del_init(&cli->tc_lru);
635         return cli;
636 }
637
638 #define NRS_TBF_JOBID_NULL ""
639
640 static struct nrs_tbf_client *
641 nrs_tbf_jobid_cli_find(struct nrs_tbf_head *head,
642                        struct ptlrpc_request *req)
643 {
644         const char              *jobid;
645         struct nrs_tbf_client   *cli;
646         struct cfs_hash         *hs = head->th_cli_hash;
647         struct cfs_hash_bd               bd;
648
649         jobid = lustre_msg_get_jobid(req->rq_reqmsg);
650         if (jobid == NULL)
651                 jobid = NRS_TBF_JOBID_NULL;
652         cfs_hash_bd_get_and_lock(hs, (void *)jobid, &bd, 1);
653         cli = nrs_tbf_jobid_hash_lookup(hs, &bd, jobid);
654         cfs_hash_bd_unlock(hs, &bd, 1);
655
656         return cli;
657 }
658
659 static struct nrs_tbf_client *
660 nrs_tbf_jobid_cli_findadd(struct nrs_tbf_head *head,
661                           struct nrs_tbf_client *cli)
662 {
663         const char              *jobid;
664         struct nrs_tbf_client   *ret;
665         struct cfs_hash         *hs = head->th_cli_hash;
666         struct cfs_hash_bd               bd;
667
668         jobid = cli->tc_jobid;
669         cfs_hash_bd_get_and_lock(hs, (void *)jobid, &bd, 1);
670         ret = nrs_tbf_jobid_hash_lookup(hs, &bd, jobid);
671         if (ret == NULL) {
672                 cfs_hash_bd_add_locked(hs, &bd, &cli->tc_hnode);
673                 ret = cli;
674         }
675         cfs_hash_bd_unlock(hs, &bd, 1);
676
677         return ret;
678 }
679
680 static void
681 nrs_tbf_jobid_cli_put(struct nrs_tbf_head *head,
682                       struct nrs_tbf_client *cli)
683 {
684         struct cfs_hash_bd               bd;
685         struct cfs_hash         *hs = head->th_cli_hash;
686         struct nrs_tbf_bucket   *bkt;
687         int                      hw;
688         struct list_head        zombies;
689
690         INIT_LIST_HEAD(&zombies);
691         cfs_hash_bd_get(hs, &cli->tc_jobid, &bd);
692         bkt = cfs_hash_bd_extra_get(hs, &bd);
693         if (!cfs_hash_bd_dec_and_lock(hs, &bd, &cli->tc_ref))
694                 return;
695         LASSERT(list_empty(&cli->tc_lru));
696         list_add_tail(&cli->tc_lru, &bkt->ntb_lru);
697
698         /*
699          * Check and purge the LRU, there is at least one client in the LRU.
700          */
701         hw = tbf_jobid_cache_size >>
702              (hs->hs_cur_bits - hs->hs_bkt_bits);
703         while (cfs_hash_bd_count_get(&bd) > hw) {
704                 if (unlikely(list_empty(&bkt->ntb_lru)))
705                         break;
706                 cli = list_entry(bkt->ntb_lru.next,
707                                      struct nrs_tbf_client,
708                                      tc_lru);
709                 LASSERT(atomic_read(&cli->tc_ref) == 0);
710                 cfs_hash_bd_del_locked(hs, &bd, &cli->tc_hnode);
711                 list_move(&cli->tc_lru, &zombies);
712         }
713         cfs_hash_bd_unlock(head->th_cli_hash, &bd, 1);
714
715         while (!list_empty(&zombies)) {
716                 cli = container_of0(zombies.next,
717                                     struct nrs_tbf_client, tc_lru);
718                 list_del_init(&cli->tc_lru);
719                 nrs_tbf_cli_fini(cli);
720         }
721 }
722
723 static void
724 nrs_tbf_jobid_cli_init(struct nrs_tbf_client *cli,
725                        struct ptlrpc_request *req)
726 {
727         char *jobid = lustre_msg_get_jobid(req->rq_reqmsg);
728
729         if (jobid == NULL)
730                 jobid = NRS_TBF_JOBID_NULL;
731         LASSERT(strlen(jobid) < LUSTRE_JOBID_SIZE);
732         INIT_LIST_HEAD(&cli->tc_lru);
733         memcpy(cli->tc_jobid, jobid, strlen(jobid));
734 }
735
736 static int nrs_tbf_jobid_hash_order(void)
737 {
738         int bits;
739
740         for (bits = 1; (1 << bits) < tbf_jobid_cache_size; ++bits)
741                 ;
742
743         return bits;
744 }
745
746 #define NRS_TBF_JOBID_BKT_BITS 10
747
748 static int
749 nrs_tbf_jobid_startup(struct ptlrpc_nrs_policy *policy,
750                       struct nrs_tbf_head *head)
751 {
752         struct nrs_tbf_cmd       start;
753         struct nrs_tbf_bucket   *bkt;
754         int                      bits;
755         int                      i;
756         int                      rc;
757         struct cfs_hash_bd       bd;
758
759         bits = nrs_tbf_jobid_hash_order();
760         if (bits < NRS_TBF_JOBID_BKT_BITS)
761                 bits = NRS_TBF_JOBID_BKT_BITS;
762         head->th_cli_hash = cfs_hash_create("nrs_tbf_hash",
763                                             bits,
764                                             bits,
765                                             NRS_TBF_JOBID_BKT_BITS,
766                                             sizeof(*bkt),
767                                             0,
768                                             0,
769                                             &nrs_tbf_jobid_hash_ops,
770                                             NRS_TBF_JOBID_HASH_FLAGS);
771         if (head->th_cli_hash == NULL)
772                 return -ENOMEM;
773
774         cfs_hash_for_each_bucket(head->th_cli_hash, &bd, i) {
775                 bkt = cfs_hash_bd_extra_get(head->th_cli_hash, &bd);
776                 INIT_LIST_HEAD(&bkt->ntb_lru);
777         }
778
779         memset(&start, 0, sizeof(start));
780         start.u.tc_start.ts_jobids_str = "*";
781
782         start.u.tc_start.ts_rpc_rate = tbf_rate;
783         start.u.tc_start.ts_rule_flags = NTRS_DEFAULT;
784         start.tc_name = NRS_TBF_DEFAULT_RULE;
785         INIT_LIST_HEAD(&start.u.tc_start.ts_jobids);
786         rc = nrs_tbf_rule_start(policy, head, &start);
787         if (rc) {
788                 cfs_hash_putref(head->th_cli_hash);
789                 head->th_cli_hash = NULL;
790         }
791
792         return rc;
793 }
794
795 /**
796  * Frees jobid of \a list.
797  *
798  */
799 static void
800 nrs_tbf_jobid_list_free(struct list_head *jobid_list)
801 {
802         struct nrs_tbf_jobid *jobid, *n;
803
804         list_for_each_entry_safe(jobid, n, jobid_list, tj_linkage) {
805                 OBD_FREE(jobid->tj_id, strlen(jobid->tj_id) + 1);
806                 list_del(&jobid->tj_linkage);
807                 OBD_FREE(jobid, sizeof(struct nrs_tbf_jobid));
808         }
809 }
810
811 static int
812 nrs_tbf_jobid_list_add(struct cfs_lstr *id, struct list_head *jobid_list)
813 {
814         struct nrs_tbf_jobid *jobid;
815         struct cfs_lstr res;
816         int rc;
817
818         OBD_ALLOC(jobid, sizeof(struct nrs_tbf_jobid));
819         if (jobid == NULL)
820                 return -ENOMEM;
821
822         OBD_ALLOC(jobid->tj_id, id->ls_len + 1);
823         if (jobid->tj_id == NULL) {
824                 OBD_FREE(jobid, sizeof(struct nrs_tbf_jobid));
825                 return -ENOMEM;
826         }
827
828         memcpy(jobid->tj_id, id->ls_str, id->ls_len);
829         rc = cfs_gettok(id, '*', &res);
830         if (rc == 0)
831                 jobid->tj_match_flag = NRS_TBF_MATCH_FULL;
832         else
833                 jobid->tj_match_flag = NRS_TBF_MATCH_WILDCARD;
834
835         list_add_tail(&jobid->tj_linkage, jobid_list);
836         return 0;
837 }
838
839 static bool
840 cfs_match_wildcard(const char *pattern, const char *content)
841 {
842         if (*pattern == '\0' && *content == '\0')
843                 return true;
844
845         if (*pattern == '*' && *(pattern + 1) != '\0' && *content == '\0')
846                 return false;
847
848         while (*pattern == *content) {
849                 pattern++;
850                 content++;
851                 if (*pattern == '\0' && *content == '\0')
852                         return true;
853
854                 if (*pattern == '*' && *(pattern + 1) != '\0' &&
855                     *content == '\0')
856                         return false;
857         }
858
859         if (*pattern == '*')
860                 return (cfs_match_wildcard(pattern + 1, content) ||
861                         cfs_match_wildcard(pattern, content + 1));
862
863         return false;
864 }
865
866 static inline bool
867 nrs_tbf_jobid_match(const struct nrs_tbf_jobid *jobid, const char *id)
868 {
869         if (jobid->tj_match_flag == NRS_TBF_MATCH_FULL)
870                 return strcmp(jobid->tj_id, id) == 0;
871
872         if (jobid->tj_match_flag == NRS_TBF_MATCH_WILDCARD)
873                 return cfs_match_wildcard(jobid->tj_id, id);
874
875         return false;
876 }
877
878 static int
879 nrs_tbf_jobid_list_match(struct list_head *jobid_list, char *id)
880 {
881         struct nrs_tbf_jobid *jobid;
882
883         list_for_each_entry(jobid, jobid_list, tj_linkage) {
884                 if (nrs_tbf_jobid_match(jobid, id))
885                         return 1;
886         }
887         return 0;
888 }
889
890 static int
891 nrs_tbf_jobid_list_parse(char *str, int len, struct list_head *jobid_list)
892 {
893         struct cfs_lstr src;
894         struct cfs_lstr res;
895         int rc = 0;
896         ENTRY;
897
898         src.ls_str = str;
899         src.ls_len = len;
900         INIT_LIST_HEAD(jobid_list);
901         while (src.ls_str) {
902                 rc = cfs_gettok(&src, ' ', &res);
903                 if (rc == 0) {
904                         rc = -EINVAL;
905                         break;
906                 }
907                 rc = nrs_tbf_jobid_list_add(&res, jobid_list);
908                 if (rc)
909                         break;
910         }
911         if (rc)
912                 nrs_tbf_jobid_list_free(jobid_list);
913         RETURN(rc);
914 }
915
916 static void nrs_tbf_jobid_cmd_fini(struct nrs_tbf_cmd *cmd)
917 {
918         if (!list_empty(&cmd->u.tc_start.ts_jobids))
919                 nrs_tbf_jobid_list_free(&cmd->u.tc_start.ts_jobids);
920         if (cmd->u.tc_start.ts_jobids_str)
921                 OBD_FREE(cmd->u.tc_start.ts_jobids_str,
922                          strlen(cmd->u.tc_start.ts_jobids_str) + 1);
923 }
924
925 static int nrs_tbf_check_id_value(struct cfs_lstr *src, char *key)
926 {
927         struct cfs_lstr res;
928         int keylen = strlen(key);
929         int rc;
930
931         rc = cfs_gettok(src, '=', &res);
932         if (rc == 0 || res.ls_len != keylen ||
933             strncmp(res.ls_str, key, keylen) != 0 ||
934             src->ls_len <= 2 || src->ls_str[0] != '{' ||
935             src->ls_str[src->ls_len - 1] != '}')
936                 return -EINVAL;
937
938         /* Skip '{' and '}' */
939         src->ls_str++;
940         src->ls_len -= 2;
941         return 0;
942 }
943
944 static int nrs_tbf_jobid_parse(struct nrs_tbf_cmd *cmd, char *id)
945 {
946         struct cfs_lstr src;
947         int rc;
948
949         src.ls_str = id;
950         src.ls_len = strlen(id);
951         rc = nrs_tbf_check_id_value(&src, "jobid");
952         if (rc)
953                 return rc;
954
955         OBD_ALLOC(cmd->u.tc_start.ts_jobids_str, src.ls_len + 1);
956         if (cmd->u.tc_start.ts_jobids_str == NULL)
957                 return -ENOMEM;
958
959         memcpy(cmd->u.tc_start.ts_jobids_str, src.ls_str, src.ls_len);
960
961         /* parse jobid list */
962         rc = nrs_tbf_jobid_list_parse(cmd->u.tc_start.ts_jobids_str,
963                                       strlen(cmd->u.tc_start.ts_jobids_str),
964                                       &cmd->u.tc_start.ts_jobids);
965         if (rc)
966                 nrs_tbf_jobid_cmd_fini(cmd);
967
968         return rc;
969 }
970
971 static int nrs_tbf_jobid_rule_init(struct ptlrpc_nrs_policy *policy,
972                                    struct nrs_tbf_rule *rule,
973                                    struct nrs_tbf_cmd *start)
974 {
975         int rc = 0;
976
977         LASSERT(start->u.tc_start.ts_jobids_str);
978         OBD_ALLOC(rule->tr_jobids_str,
979                   strlen(start->u.tc_start.ts_jobids_str) + 1);
980         if (rule->tr_jobids_str == NULL)
981                 return -ENOMEM;
982
983         memcpy(rule->tr_jobids_str,
984                start->u.tc_start.ts_jobids_str,
985                strlen(start->u.tc_start.ts_jobids_str));
986
987         INIT_LIST_HEAD(&rule->tr_jobids);
988         if (!list_empty(&start->u.tc_start.ts_jobids)) {
989                 rc = nrs_tbf_jobid_list_parse(rule->tr_jobids_str,
990                                               strlen(rule->tr_jobids_str),
991                                               &rule->tr_jobids);
992                 if (rc)
993                         CERROR("jobids {%s} illegal\n", rule->tr_jobids_str);
994         }
995         if (rc)
996                 OBD_FREE(rule->tr_jobids_str,
997                          strlen(start->u.tc_start.ts_jobids_str) + 1);
998         return rc;
999 }
1000
1001 static int
1002 nrs_tbf_jobid_rule_dump(struct nrs_tbf_rule *rule, struct seq_file *m)
1003 {
1004         seq_printf(m, "%s {%s} %llu, ref %d\n", rule->tr_name,
1005                    rule->tr_jobids_str, rule->tr_rpc_rate,
1006                    atomic_read(&rule->tr_ref) - 1);
1007         return 0;
1008 }
1009
1010 static int
1011 nrs_tbf_jobid_rule_match(struct nrs_tbf_rule *rule,
1012                          struct nrs_tbf_client *cli)
1013 {
1014         return nrs_tbf_jobid_list_match(&rule->tr_jobids, cli->tc_jobid);
1015 }
1016
1017 static void nrs_tbf_jobid_rule_fini(struct nrs_tbf_rule *rule)
1018 {
1019         if (!list_empty(&rule->tr_jobids))
1020                 nrs_tbf_jobid_list_free(&rule->tr_jobids);
1021         LASSERT(rule->tr_jobids_str != NULL);
1022         OBD_FREE(rule->tr_jobids_str, strlen(rule->tr_jobids_str) + 1);
1023 }
1024
1025 static struct nrs_tbf_ops nrs_tbf_jobid_ops = {
1026         .o_name = NRS_TBF_TYPE_JOBID,
1027         .o_startup = nrs_tbf_jobid_startup,
1028         .o_cli_find = nrs_tbf_jobid_cli_find,
1029         .o_cli_findadd = nrs_tbf_jobid_cli_findadd,
1030         .o_cli_put = nrs_tbf_jobid_cli_put,
1031         .o_cli_init = nrs_tbf_jobid_cli_init,
1032         .o_rule_init = nrs_tbf_jobid_rule_init,
1033         .o_rule_dump = nrs_tbf_jobid_rule_dump,
1034         .o_rule_match = nrs_tbf_jobid_rule_match,
1035         .o_rule_fini = nrs_tbf_jobid_rule_fini,
1036 };
1037
1038 /**
1039  * libcfs_hash operations for nrs_tbf_net::cn_cli_hash
1040  *
1041  * This uses ptlrpc_request::rq_peer.nid as its key, in order to hash
1042  * nrs_tbf_client objects.
1043  */
1044 #define NRS_TBF_NID_BKT_BITS    8
1045 #define NRS_TBF_NID_BITS        16
1046
1047 static unsigned nrs_tbf_nid_hop_hash(struct cfs_hash *hs, const void *key,
1048                                   unsigned mask)
1049 {
1050         return cfs_hash_djb2_hash(key, sizeof(lnet_nid_t), mask);
1051 }
1052
1053 static int nrs_tbf_nid_hop_keycmp(const void *key, struct hlist_node *hnode)
1054 {
1055         lnet_nid_t            *nid = (lnet_nid_t *)key;
1056         struct nrs_tbf_client *cli = hlist_entry(hnode,
1057                                                      struct nrs_tbf_client,
1058                                                      tc_hnode);
1059
1060         return *nid == cli->tc_nid;
1061 }
1062
1063 static void *nrs_tbf_nid_hop_key(struct hlist_node *hnode)
1064 {
1065         struct nrs_tbf_client *cli = hlist_entry(hnode,
1066                                                      struct nrs_tbf_client,
1067                                                      tc_hnode);
1068
1069         return &cli->tc_nid;
1070 }
1071
1072 static void *nrs_tbf_nid_hop_object(struct hlist_node *hnode)
1073 {
1074         return hlist_entry(hnode, struct nrs_tbf_client, tc_hnode);
1075 }
1076
1077 static void nrs_tbf_nid_hop_get(struct cfs_hash *hs, struct hlist_node *hnode)
1078 {
1079         struct nrs_tbf_client *cli = hlist_entry(hnode,
1080                                                      struct nrs_tbf_client,
1081                                                      tc_hnode);
1082
1083         atomic_inc(&cli->tc_ref);
1084 }
1085
1086 static void nrs_tbf_nid_hop_put(struct cfs_hash *hs, struct hlist_node *hnode)
1087 {
1088         struct nrs_tbf_client *cli = hlist_entry(hnode,
1089                                                      struct nrs_tbf_client,
1090                                                      tc_hnode);
1091
1092         atomic_dec(&cli->tc_ref);
1093 }
1094
1095 static void nrs_tbf_nid_hop_exit(struct cfs_hash *hs, struct hlist_node *hnode)
1096 {
1097         struct nrs_tbf_client *cli = hlist_entry(hnode,
1098                                                      struct nrs_tbf_client,
1099                                                      tc_hnode);
1100
1101         LASSERTF(atomic_read(&cli->tc_ref) == 0,
1102                  "Busy TBF object from client with NID %s, with %d refs\n",
1103                  libcfs_nid2str(cli->tc_nid), atomic_read(&cli->tc_ref));
1104
1105         nrs_tbf_cli_fini(cli);
1106 }
1107
1108 static struct cfs_hash_ops nrs_tbf_nid_hash_ops = {
1109         .hs_hash        = nrs_tbf_nid_hop_hash,
1110         .hs_keycmp      = nrs_tbf_nid_hop_keycmp,
1111         .hs_key         = nrs_tbf_nid_hop_key,
1112         .hs_object      = nrs_tbf_nid_hop_object,
1113         .hs_get         = nrs_tbf_nid_hop_get,
1114         .hs_put         = nrs_tbf_nid_hop_put,
1115         .hs_put_locked  = nrs_tbf_nid_hop_put,
1116         .hs_exit        = nrs_tbf_nid_hop_exit,
1117 };
1118
1119 static struct nrs_tbf_client *
1120 nrs_tbf_nid_cli_find(struct nrs_tbf_head *head,
1121                      struct ptlrpc_request *req)
1122 {
1123         return cfs_hash_lookup(head->th_cli_hash, &req->rq_peer.nid);
1124 }
1125
1126 static struct nrs_tbf_client *
1127 nrs_tbf_nid_cli_findadd(struct nrs_tbf_head *head,
1128                         struct nrs_tbf_client *cli)
1129 {
1130         return cfs_hash_findadd_unique(head->th_cli_hash, &cli->tc_nid,
1131                                        &cli->tc_hnode);
1132 }
1133
1134 static void
1135 nrs_tbf_nid_cli_put(struct nrs_tbf_head *head,
1136                       struct nrs_tbf_client *cli)
1137 {
1138         cfs_hash_put(head->th_cli_hash, &cli->tc_hnode);
1139 }
1140
1141 static int
1142 nrs_tbf_nid_startup(struct ptlrpc_nrs_policy *policy,
1143                     struct nrs_tbf_head *head)
1144 {
1145         struct nrs_tbf_cmd      start;
1146         int rc;
1147
1148         head->th_cli_hash = cfs_hash_create("nrs_tbf_hash",
1149                                             NRS_TBF_NID_BITS,
1150                                             NRS_TBF_NID_BITS,
1151                                             NRS_TBF_NID_BKT_BITS, 0,
1152                                             CFS_HASH_MIN_THETA,
1153                                             CFS_HASH_MAX_THETA,
1154                                             &nrs_tbf_nid_hash_ops,
1155                                             CFS_HASH_RW_BKTLOCK);
1156         if (head->th_cli_hash == NULL)
1157                 return -ENOMEM;
1158
1159         memset(&start, 0, sizeof(start));
1160         start.u.tc_start.ts_nids_str = "*";
1161
1162         start.u.tc_start.ts_rpc_rate = tbf_rate;
1163         start.u.tc_start.ts_rule_flags = NTRS_DEFAULT;
1164         start.tc_name = NRS_TBF_DEFAULT_RULE;
1165         INIT_LIST_HEAD(&start.u.tc_start.ts_nids);
1166         rc = nrs_tbf_rule_start(policy, head, &start);
1167         if (rc) {
1168                 cfs_hash_putref(head->th_cli_hash);
1169                 head->th_cli_hash = NULL;
1170         }
1171
1172         return rc;
1173 }
1174
1175 static void
1176 nrs_tbf_nid_cli_init(struct nrs_tbf_client *cli,
1177                              struct ptlrpc_request *req)
1178 {
1179         cli->tc_nid = req->rq_peer.nid;
1180 }
1181
1182 static int nrs_tbf_nid_rule_init(struct ptlrpc_nrs_policy *policy,
1183                                  struct nrs_tbf_rule *rule,
1184                                  struct nrs_tbf_cmd *start)
1185 {
1186         LASSERT(start->u.tc_start.ts_nids_str);
1187         OBD_ALLOC(rule->tr_nids_str,
1188                   strlen(start->u.tc_start.ts_nids_str) + 1);
1189         if (rule->tr_nids_str == NULL)
1190                 return -ENOMEM;
1191
1192         memcpy(rule->tr_nids_str,
1193                start->u.tc_start.ts_nids_str,
1194                strlen(start->u.tc_start.ts_nids_str));
1195
1196         INIT_LIST_HEAD(&rule->tr_nids);
1197         if (!list_empty(&start->u.tc_start.ts_nids)) {
1198                 if (cfs_parse_nidlist(rule->tr_nids_str,
1199                                       strlen(rule->tr_nids_str),
1200                                       &rule->tr_nids) <= 0) {
1201                         CERROR("nids {%s} illegal\n",
1202                                rule->tr_nids_str);
1203                         OBD_FREE(rule->tr_nids_str,
1204                                  strlen(start->u.tc_start.ts_nids_str) + 1);
1205                         return -EINVAL;
1206                 }
1207         }
1208         return 0;
1209 }
1210
1211 static int
1212 nrs_tbf_nid_rule_dump(struct nrs_tbf_rule *rule, struct seq_file *m)
1213 {
1214         seq_printf(m, "%s {%s} %llu, ref %d\n", rule->tr_name,
1215                    rule->tr_nids_str, rule->tr_rpc_rate,
1216                    atomic_read(&rule->tr_ref) - 1);
1217         return 0;
1218 }
1219
1220 static int
1221 nrs_tbf_nid_rule_match(struct nrs_tbf_rule *rule,
1222                        struct nrs_tbf_client *cli)
1223 {
1224         return cfs_match_nid(cli->tc_nid, &rule->tr_nids);
1225 }
1226
1227 static void nrs_tbf_nid_rule_fini(struct nrs_tbf_rule *rule)
1228 {
1229         if (!list_empty(&rule->tr_nids))
1230                 cfs_free_nidlist(&rule->tr_nids);
1231         LASSERT(rule->tr_nids_str != NULL);
1232         OBD_FREE(rule->tr_nids_str, strlen(rule->tr_nids_str) + 1);
1233 }
1234
1235 static void nrs_tbf_nid_cmd_fini(struct nrs_tbf_cmd *cmd)
1236 {
1237         if (!list_empty(&cmd->u.tc_start.ts_nids))
1238                 cfs_free_nidlist(&cmd->u.tc_start.ts_nids);
1239         if (cmd->u.tc_start.ts_nids_str)
1240                 OBD_FREE(cmd->u.tc_start.ts_nids_str,
1241                          strlen(cmd->u.tc_start.ts_nids_str) + 1);
1242 }
1243
1244 static int nrs_tbf_nid_parse(struct nrs_tbf_cmd *cmd, char *id)
1245 {
1246         struct cfs_lstr src;
1247         int rc;
1248
1249         src.ls_str = id;
1250         src.ls_len = strlen(id);
1251         rc = nrs_tbf_check_id_value(&src, "nid");
1252         if (rc)
1253                 return rc;
1254
1255         OBD_ALLOC(cmd->u.tc_start.ts_nids_str, src.ls_len + 1);
1256         if (cmd->u.tc_start.ts_nids_str == NULL)
1257                 return -ENOMEM;
1258
1259         memcpy(cmd->u.tc_start.ts_nids_str, src.ls_str, src.ls_len);
1260
1261         /* parse NID list */
1262         if (cfs_parse_nidlist(cmd->u.tc_start.ts_nids_str,
1263                               strlen(cmd->u.tc_start.ts_nids_str),
1264                               &cmd->u.tc_start.ts_nids) <= 0) {
1265                 nrs_tbf_nid_cmd_fini(cmd);
1266                 return -EINVAL;
1267         }
1268
1269         return 0;
1270 }
1271
1272 static struct nrs_tbf_ops nrs_tbf_nid_ops = {
1273         .o_name = NRS_TBF_TYPE_NID,
1274         .o_startup = nrs_tbf_nid_startup,
1275         .o_cli_find = nrs_tbf_nid_cli_find,
1276         .o_cli_findadd = nrs_tbf_nid_cli_findadd,
1277         .o_cli_put = nrs_tbf_nid_cli_put,
1278         .o_cli_init = nrs_tbf_nid_cli_init,
1279         .o_rule_init = nrs_tbf_nid_rule_init,
1280         .o_rule_dump = nrs_tbf_nid_rule_dump,
1281         .o_rule_match = nrs_tbf_nid_rule_match,
1282         .o_rule_fini = nrs_tbf_nid_rule_fini,
1283 };
1284
1285 static unsigned nrs_tbf_hop_hash(struct cfs_hash *hs, const void *key,
1286                                  unsigned mask)
1287 {
1288         return cfs_hash_djb2_hash(key, strlen(key), mask);
1289 }
1290
1291 static int nrs_tbf_hop_keycmp(const void *key, struct hlist_node *hnode)
1292 {
1293         struct nrs_tbf_client *cli = hlist_entry(hnode,
1294                                                  struct nrs_tbf_client,
1295                                                  tc_hnode);
1296
1297         return (strcmp(cli->tc_key, key) == 0);
1298 }
1299
1300 static void *nrs_tbf_hop_key(struct hlist_node *hnode)
1301 {
1302         struct nrs_tbf_client *cli = hlist_entry(hnode,
1303                                                  struct nrs_tbf_client,
1304                                                  tc_hnode);
1305         return cli->tc_key;
1306 }
1307
1308 static void *nrs_tbf_hop_object(struct hlist_node *hnode)
1309 {
1310         return hlist_entry(hnode, struct nrs_tbf_client, tc_hnode);
1311 }
1312
1313 static void nrs_tbf_hop_get(struct cfs_hash *hs, struct hlist_node *hnode)
1314 {
1315         struct nrs_tbf_client *cli = hlist_entry(hnode,
1316                                                  struct nrs_tbf_client,
1317                                                  tc_hnode);
1318
1319         atomic_inc(&cli->tc_ref);
1320 }
1321
1322 static void nrs_tbf_hop_put(struct cfs_hash *hs, struct hlist_node *hnode)
1323 {
1324         struct nrs_tbf_client *cli = hlist_entry(hnode,
1325                                                  struct nrs_tbf_client,
1326                                                  tc_hnode);
1327
1328         atomic_dec(&cli->tc_ref);
1329 }
1330
1331 static void nrs_tbf_hop_exit(struct cfs_hash *hs, struct hlist_node *hnode)
1332
1333 {
1334         struct nrs_tbf_client *cli = hlist_entry(hnode,
1335                                                  struct nrs_tbf_client,
1336                                                  tc_hnode);
1337
1338         LASSERT(atomic_read(&cli->tc_ref) == 0);
1339         nrs_tbf_cli_fini(cli);
1340 }
1341
1342 static struct cfs_hash_ops nrs_tbf_hash_ops = {
1343         .hs_hash        = nrs_tbf_hop_hash,
1344         .hs_keycmp      = nrs_tbf_hop_keycmp,
1345         .hs_key         = nrs_tbf_hop_key,
1346         .hs_object      = nrs_tbf_hop_object,
1347         .hs_get         = nrs_tbf_hop_get,
1348         .hs_put         = nrs_tbf_hop_put,
1349         .hs_put_locked  = nrs_tbf_hop_put,
1350         .hs_exit        = nrs_tbf_hop_exit,
1351 };
1352
1353 #define NRS_TBF_GENERIC_BKT_BITS        10
1354 #define NRS_TBF_GENERIC_HASH_FLAGS      (CFS_HASH_SPIN_BKTLOCK | \
1355                                         CFS_HASH_NO_ITEMREF | \
1356                                         CFS_HASH_DEPTH)
1357
1358 static int
1359 nrs_tbf_startup(struct ptlrpc_nrs_policy *policy, struct nrs_tbf_head *head)
1360 {
1361         struct nrs_tbf_cmd       start;
1362         struct nrs_tbf_bucket   *bkt;
1363         int                      bits;
1364         int                      i;
1365         int                      rc;
1366         struct cfs_hash_bd       bd;
1367
1368         bits = nrs_tbf_jobid_hash_order();
1369         if (bits < NRS_TBF_GENERIC_BKT_BITS)
1370                 bits = NRS_TBF_GENERIC_BKT_BITS;
1371         head->th_cli_hash = cfs_hash_create("nrs_tbf_hash",
1372                                             bits, bits,
1373                                             NRS_TBF_GENERIC_BKT_BITS,
1374                                             sizeof(*bkt), 0, 0,
1375                                             &nrs_tbf_hash_ops,
1376                                             NRS_TBF_GENERIC_HASH_FLAGS);
1377         if (head->th_cli_hash == NULL)
1378                 return -ENOMEM;
1379
1380         cfs_hash_for_each_bucket(head->th_cli_hash, &bd, i) {
1381                 bkt = cfs_hash_bd_extra_get(head->th_cli_hash, &bd);
1382                 INIT_LIST_HEAD(&bkt->ntb_lru);
1383         }
1384
1385         memset(&start, 0, sizeof(start));
1386         start.u.tc_start.ts_conds_str = "*";
1387
1388         start.u.tc_start.ts_rpc_rate = tbf_rate;
1389         start.u.tc_start.ts_rule_flags = NTRS_DEFAULT;
1390         start.tc_name = NRS_TBF_DEFAULT_RULE;
1391         INIT_LIST_HEAD(&start.u.tc_start.ts_conds);
1392         rc = nrs_tbf_rule_start(policy, head, &start);
1393         if (rc)
1394                 cfs_hash_putref(head->th_cli_hash);
1395
1396         return rc;
1397 }
1398
1399 static struct nrs_tbf_client *
1400 nrs_tbf_cli_hash_lookup(struct cfs_hash *hs, struct cfs_hash_bd *bd,
1401                         const char *key)
1402 {
1403         struct hlist_node *hnode;
1404         struct nrs_tbf_client *cli;
1405
1406         hnode = cfs_hash_bd_lookup_locked(hs, bd, (void *)key);
1407         if (hnode == NULL)
1408                 return NULL;
1409
1410         cli = container_of0(hnode, struct nrs_tbf_client, tc_hnode);
1411         if (!list_empty(&cli->tc_lru))
1412                 list_del_init(&cli->tc_lru);
1413         return cli;
1414 }
1415
1416 static struct nrs_tbf_client *
1417 nrs_tbf_cli_find(struct nrs_tbf_head *head, struct ptlrpc_request *req)
1418 {
1419         struct nrs_tbf_client *cli;
1420         struct cfs_hash *hs = head->th_cli_hash;
1421         struct cfs_hash_bd bd;
1422         char keystr[NRS_TBF_KEY_LEN] = { '\0' };
1423         const char *jobid;
1424         __u32 opc;
1425
1426         jobid = lustre_msg_get_jobid(req->rq_reqmsg);
1427         if (jobid == NULL)
1428                 jobid = NRS_TBF_JOBID_NULL;
1429         opc = lustre_msg_get_opc(req->rq_reqmsg);
1430         snprintf(keystr, sizeof(keystr), "%s_%s_%d", jobid,
1431                  libcfs_nid2str(req->rq_peer.nid), opc);
1432         LASSERT(strlen(keystr) < NRS_TBF_KEY_LEN);
1433         cfs_hash_bd_get_and_lock(hs, (void *)keystr, &bd, 1);
1434         cli = nrs_tbf_cli_hash_lookup(hs, &bd, keystr);
1435         cfs_hash_bd_unlock(hs, &bd, 1);
1436
1437         return cli;
1438 }
1439
1440 static struct nrs_tbf_client *
1441 nrs_tbf_cli_findadd(struct nrs_tbf_head *head,
1442                     struct nrs_tbf_client *cli)
1443 {
1444         const char              *key;
1445         struct nrs_tbf_client   *ret;
1446         struct cfs_hash         *hs = head->th_cli_hash;
1447         struct cfs_hash_bd       bd;
1448
1449         key = cli->tc_key;
1450         cfs_hash_bd_get_and_lock(hs, (void *)key, &bd, 1);
1451         ret = nrs_tbf_cli_hash_lookup(hs, &bd, key);
1452         if (ret == NULL) {
1453                 cfs_hash_bd_add_locked(hs, &bd, &cli->tc_hnode);
1454                 ret = cli;
1455         }
1456         cfs_hash_bd_unlock(hs, &bd, 1);
1457
1458         return ret;
1459 }
1460
1461 static void
1462 nrs_tbf_cli_put(struct nrs_tbf_head *head, struct nrs_tbf_client *cli)
1463 {
1464         struct cfs_hash_bd       bd;
1465         struct cfs_hash         *hs = head->th_cli_hash;
1466         struct nrs_tbf_bucket   *bkt;
1467         int                      hw;
1468         struct list_head         zombies;
1469
1470         INIT_LIST_HEAD(&zombies);
1471         cfs_hash_bd_get(hs, &cli->tc_key, &bd);
1472         bkt = cfs_hash_bd_extra_get(hs, &bd);
1473         if (!cfs_hash_bd_dec_and_lock(hs, &bd, &cli->tc_ref))
1474                 return;
1475         LASSERT(list_empty(&cli->tc_lru));
1476         list_add_tail(&cli->tc_lru, &bkt->ntb_lru);
1477
1478         /**
1479          * Check and purge the LRU, there is at least one client in the LRU.
1480          */
1481         hw = tbf_jobid_cache_size >> (hs->hs_cur_bits - hs->hs_bkt_bits);
1482         while (cfs_hash_bd_count_get(&bd) > hw) {
1483                 if (unlikely(list_empty(&bkt->ntb_lru)))
1484                         break;
1485                 cli = list_entry(bkt->ntb_lru.next,
1486                                  struct nrs_tbf_client,
1487                                  tc_lru);
1488                 LASSERT(atomic_read(&cli->tc_ref) == 0);
1489                 cfs_hash_bd_del_locked(hs, &bd, &cli->tc_hnode);
1490                 list_move(&cli->tc_lru, &zombies);
1491         }
1492         cfs_hash_bd_unlock(head->th_cli_hash, &bd, 1);
1493
1494         while (!list_empty(&zombies)) {
1495                 cli = container_of0(zombies.next,
1496                                     struct nrs_tbf_client, tc_lru);
1497                 list_del_init(&cli->tc_lru);
1498                 nrs_tbf_cli_fini(cli);
1499         }
1500 }
1501
1502 static void
1503 nrs_tbf_generic_cli_init(struct nrs_tbf_client *cli,
1504                          struct ptlrpc_request *req)
1505 {
1506         char keystr[NRS_TBF_KEY_LEN];
1507         const char *jobid;
1508         __u32 opc;
1509
1510         jobid = lustre_msg_get_jobid(req->rq_reqmsg);
1511         if (jobid == NULL)
1512                 jobid = NRS_TBF_JOBID_NULL;
1513         opc = lustre_msg_get_opc(req->rq_reqmsg);
1514         snprintf(keystr, sizeof(keystr), "%s_%s_%d", jobid,
1515                  libcfs_nid2str(req->rq_peer.nid), opc);
1516
1517         LASSERT(strlen(keystr) < NRS_TBF_KEY_LEN);
1518         INIT_LIST_HEAD(&cli->tc_lru);
1519         memcpy(cli->tc_key, keystr, strlen(keystr));
1520         memcpy(cli->tc_jobid, jobid, strlen(jobid));
1521         cli->tc_nid = req->rq_peer.nid;
1522         cli->tc_opcode = opc;
1523 }
1524
1525 static void
1526 nrs_tbf_expression_free(struct nrs_tbf_expression *expr)
1527 {
1528         LASSERT(expr->te_field >= NRS_TBF_FIELD_NID &&
1529                 expr->te_field < NRS_TBF_FIELD_MAX);
1530         switch (expr->te_field) {
1531         case NRS_TBF_FIELD_NID:
1532                 cfs_free_nidlist(&expr->te_cond);
1533                 break;
1534         case NRS_TBF_FIELD_JOBID:
1535                 nrs_tbf_jobid_list_free(&expr->te_cond);
1536                 break;
1537         case NRS_TBF_FIELD_OPCODE:
1538                 CFS_FREE_BITMAP(expr->te_opcodes);
1539                 break;
1540         default:
1541                 LBUG();
1542         }
1543         OBD_FREE_PTR(expr);
1544 }
1545
1546 static void
1547 nrs_tbf_conjunction_free(struct nrs_tbf_conjunction *conjunction)
1548 {
1549         struct nrs_tbf_expression *expression;
1550         struct nrs_tbf_expression *n;
1551
1552         LASSERT(list_empty(&conjunction->tc_linkage));
1553         list_for_each_entry_safe(expression, n,
1554                                  &conjunction->tc_expressions,
1555                                  te_linkage) {
1556                 list_del_init(&expression->te_linkage);
1557                 nrs_tbf_expression_free(expression);
1558         }
1559         OBD_FREE_PTR(conjunction);
1560 }
1561
1562 static void
1563 nrs_tbf_conds_free(struct list_head *cond_list)
1564 {
1565         struct nrs_tbf_conjunction *conjunction;
1566         struct nrs_tbf_conjunction *n;
1567
1568         list_for_each_entry_safe(conjunction, n, cond_list, tc_linkage) {
1569                 list_del_init(&conjunction->tc_linkage);
1570                 nrs_tbf_conjunction_free(conjunction);
1571         }
1572 }
1573
1574 static void
1575 nrs_tbf_generic_cmd_fini(struct nrs_tbf_cmd *cmd)
1576 {
1577         if (!list_empty(&cmd->u.tc_start.ts_conds))
1578                 nrs_tbf_conds_free(&cmd->u.tc_start.ts_conds);
1579         if (cmd->u.tc_start.ts_conds_str)
1580                 OBD_FREE(cmd->u.tc_start.ts_conds_str,
1581                          strlen(cmd->u.tc_start.ts_conds_str) + 1);
1582 }
1583
1584 #define NRS_TBF_DISJUNCTION_DELIM       (',')
1585 #define NRS_TBF_CONJUNCTION_DELIM       ('&')
1586 #define NRS_TBF_EXPRESSION_DELIM        ('=')
1587
1588 static inline bool
1589 nrs_tbf_check_field(struct cfs_lstr *field, char *str)
1590 {
1591         int len = strlen(str);
1592
1593         return (field->ls_len == len &&
1594                 strncmp(field->ls_str, str, len) == 0);
1595 }
1596
1597 static int
1598 nrs_tbf_opcode_list_parse(char *str, int len, struct cfs_bitmap **bitmaptr);
1599
1600 static int
1601 nrs_tbf_expression_parse(struct cfs_lstr *src, struct list_head *cond_list)
1602 {
1603         struct nrs_tbf_expression *expr;
1604         struct cfs_lstr field;
1605         int rc = 0;
1606
1607         OBD_ALLOC(expr, sizeof(struct nrs_tbf_expression));
1608         if (expr == NULL)
1609                 return -ENOMEM;
1610
1611         rc = cfs_gettok(src, NRS_TBF_EXPRESSION_DELIM, &field);
1612         if (rc == 0 || src->ls_len <= 2 || src->ls_str[0] != '{' ||
1613             src->ls_str[src->ls_len - 1] != '}')
1614                 GOTO(out, rc = -EINVAL);
1615
1616         /* Skip '{' and '}' */
1617         src->ls_str++;
1618         src->ls_len -= 2;
1619
1620         if (nrs_tbf_check_field(&field, "nid")) {
1621                 if (cfs_parse_nidlist(src->ls_str,
1622                                       src->ls_len,
1623                                       &expr->te_cond) <= 0)
1624                         GOTO(out, rc = -EINVAL);
1625                 expr->te_field = NRS_TBF_FIELD_NID;
1626         } else if (nrs_tbf_check_field(&field, "jobid")) {
1627                 if (nrs_tbf_jobid_list_parse(src->ls_str,
1628                                              src->ls_len,
1629                                              &expr->te_cond) < 0)
1630                         GOTO(out, rc = -EINVAL);
1631                 expr->te_field = NRS_TBF_FIELD_JOBID;
1632         } else if (nrs_tbf_check_field(&field, "opcode")) {
1633                 if (nrs_tbf_opcode_list_parse(src->ls_str,
1634                                               src->ls_len,
1635                                               &expr->te_opcodes) < 0)
1636                         GOTO(out, rc = -EINVAL);
1637                 expr->te_field = NRS_TBF_FIELD_OPCODE;
1638         } else
1639                 GOTO(out, rc = -EINVAL);
1640
1641         list_add_tail(&expr->te_linkage, cond_list);
1642         return 0;
1643 out:
1644         OBD_FREE_PTR(expr);
1645         return rc;
1646 }
1647
1648 static int
1649 nrs_tbf_conjunction_parse(struct cfs_lstr *src, struct list_head *cond_list)
1650 {
1651         struct nrs_tbf_conjunction *conjunction;
1652         struct cfs_lstr expr;
1653         int rc = 0;
1654
1655         OBD_ALLOC(conjunction, sizeof(struct nrs_tbf_conjunction));
1656         if (conjunction == NULL)
1657                 return -ENOMEM;
1658
1659         INIT_LIST_HEAD(&conjunction->tc_expressions);
1660         list_add_tail(&conjunction->tc_linkage, cond_list);
1661
1662         while (src->ls_str) {
1663                 rc = cfs_gettok(src, NRS_TBF_CONJUNCTION_DELIM, &expr);
1664                 if (rc == 0) {
1665                         rc = -EINVAL;
1666                         break;
1667                 }
1668                 rc = nrs_tbf_expression_parse(&expr,
1669                                               &conjunction->tc_expressions);
1670                 if (rc)
1671                         break;
1672         }
1673         return rc;
1674 }
1675
1676 static int
1677 nrs_tbf_conds_parse(char *str, int len, struct list_head *cond_list)
1678 {
1679         struct cfs_lstr src;
1680         struct cfs_lstr res;
1681         int rc = 0;
1682
1683         src.ls_str = str;
1684         src.ls_len = len;
1685         INIT_LIST_HEAD(cond_list);
1686         while (src.ls_str) {
1687                 rc = cfs_gettok(&src, NRS_TBF_DISJUNCTION_DELIM, &res);
1688                 if (rc == 0) {
1689                         rc = -EINVAL;
1690                         break;
1691                 }
1692                 rc = nrs_tbf_conjunction_parse(&res, cond_list);
1693                 if (rc)
1694                         break;
1695         }
1696         return rc;
1697 }
1698
1699 static int
1700 nrs_tbf_generic_parse(struct nrs_tbf_cmd *cmd, const char *id)
1701 {
1702         int rc;
1703
1704         OBD_ALLOC(cmd->u.tc_start.ts_conds_str, strlen(id) + 1);
1705         if (cmd->u.tc_start.ts_conds_str == NULL)
1706                 return -ENOMEM;
1707
1708         memcpy(cmd->u.tc_start.ts_conds_str, id, strlen(id));
1709
1710         /* Parse hybird NID and JOBID conditions */
1711         rc = nrs_tbf_conds_parse(cmd->u.tc_start.ts_conds_str,
1712                                  strlen(cmd->u.tc_start.ts_conds_str),
1713                                  &cmd->u.tc_start.ts_conds);
1714         if (rc)
1715                 nrs_tbf_generic_cmd_fini(cmd);
1716
1717         return rc;
1718 }
1719
1720 static int
1721 nrs_tbf_expression_match(struct nrs_tbf_expression *expr,
1722                          struct nrs_tbf_rule *rule,
1723                          struct nrs_tbf_client *cli)
1724 {
1725         switch (expr->te_field) {
1726         case NRS_TBF_FIELD_NID:
1727                 return cfs_match_nid(cli->tc_nid, &expr->te_cond);
1728         case NRS_TBF_FIELD_JOBID:
1729                 return nrs_tbf_jobid_list_match(&expr->te_cond, cli->tc_jobid);
1730         case NRS_TBF_FIELD_OPCODE:
1731                 return cfs_bitmap_check(expr->te_opcodes, cli->tc_opcode);
1732         default:
1733                 return 0;
1734         }
1735 }
1736
1737 static int
1738 nrs_tbf_conjunction_match(struct nrs_tbf_conjunction *conjunction,
1739                           struct nrs_tbf_rule *rule,
1740                           struct nrs_tbf_client *cli)
1741 {
1742         struct nrs_tbf_expression *expr;
1743         int matched;
1744
1745         list_for_each_entry(expr, &conjunction->tc_expressions, te_linkage) {
1746                 matched = nrs_tbf_expression_match(expr, rule, cli);
1747                 if (!matched)
1748                         return 0;
1749         }
1750
1751         return 1;
1752 }
1753
1754 static int
1755 nrs_tbf_cond_match(struct nrs_tbf_rule *rule, struct nrs_tbf_client *cli)
1756 {
1757         struct nrs_tbf_conjunction *conjunction;
1758         int matched;
1759
1760         list_for_each_entry(conjunction, &rule->tr_conds, tc_linkage) {
1761                 matched = nrs_tbf_conjunction_match(conjunction, rule, cli);
1762                 if (matched)
1763                         return 1;
1764         }
1765
1766         return 0;
1767 }
1768
1769 static void
1770 nrs_tbf_generic_rule_fini(struct nrs_tbf_rule *rule)
1771 {
1772         if (!list_empty(&rule->tr_conds))
1773                 nrs_tbf_conds_free(&rule->tr_conds);
1774         LASSERT(rule->tr_conds_str != NULL);
1775         OBD_FREE(rule->tr_conds_str, strlen(rule->tr_conds_str) + 1);
1776 }
1777
1778 static int
1779 nrs_tbf_rule_init(struct ptlrpc_nrs_policy *policy,
1780                   struct nrs_tbf_rule *rule, struct nrs_tbf_cmd *start)
1781 {
1782         int rc = 0;
1783
1784         LASSERT(start->u.tc_start.ts_conds_str);
1785         OBD_ALLOC(rule->tr_conds_str,
1786                   strlen(start->u.tc_start.ts_conds_str) + 1);
1787         if (rule->tr_conds_str == NULL)
1788                 return -ENOMEM;
1789
1790         memcpy(rule->tr_conds_str,
1791                start->u.tc_start.ts_conds_str,
1792                strlen(start->u.tc_start.ts_conds_str));
1793
1794         INIT_LIST_HEAD(&rule->tr_conds);
1795         if (!list_empty(&start->u.tc_start.ts_conds)) {
1796                 rc = nrs_tbf_conds_parse(rule->tr_conds_str,
1797                                          strlen(rule->tr_conds_str),
1798                                          &rule->tr_conds);
1799         }
1800         if (rc)
1801                 nrs_tbf_generic_rule_fini(rule);
1802
1803         return rc;
1804 }
1805
1806 static int
1807 nrs_tbf_generic_rule_dump(struct nrs_tbf_rule *rule, struct seq_file *m)
1808 {
1809         seq_printf(m, "%s %s %llu, ref %d\n", rule->tr_name,
1810                    rule->tr_conds_str, rule->tr_rpc_rate,
1811                    atomic_read(&rule->tr_ref) - 1);
1812         return 0;
1813 }
1814
1815 static int
1816 nrs_tbf_generic_rule_match(struct nrs_tbf_rule *rule,
1817                            struct nrs_tbf_client *cli)
1818 {
1819         return nrs_tbf_cond_match(rule, cli);
1820 }
1821
1822 static struct nrs_tbf_ops nrs_tbf_generic_ops = {
1823         .o_name = NRS_TBF_TYPE_GENERIC,
1824         .o_startup = nrs_tbf_startup,
1825         .o_cli_find = nrs_tbf_cli_find,
1826         .o_cli_findadd = nrs_tbf_cli_findadd,
1827         .o_cli_put = nrs_tbf_cli_put,
1828         .o_cli_init = nrs_tbf_generic_cli_init,
1829         .o_rule_init = nrs_tbf_rule_init,
1830         .o_rule_dump = nrs_tbf_generic_rule_dump,
1831         .o_rule_match = nrs_tbf_generic_rule_match,
1832         .o_rule_fini = nrs_tbf_generic_rule_fini,
1833 };
1834
1835 static void nrs_tbf_opcode_rule_fini(struct nrs_tbf_rule *rule)
1836 {
1837         if (rule->tr_opcodes != NULL)
1838                 CFS_FREE_BITMAP(rule->tr_opcodes);
1839
1840         LASSERT(rule->tr_opcodes_str != NULL);
1841         OBD_FREE(rule->tr_opcodes_str, strlen(rule->tr_opcodes_str) + 1);
1842 }
1843
1844 static unsigned nrs_tbf_opcode_hop_hash(struct cfs_hash *hs, const void *key,
1845                                         unsigned mask)
1846 {
1847         return cfs_hash_djb2_hash(key, sizeof(__u32), mask);
1848 }
1849
1850 static int nrs_tbf_opcode_hop_keycmp(const void *key, struct hlist_node *hnode)
1851 {
1852         const __u32     *opc = key;
1853         struct nrs_tbf_client *cli = hlist_entry(hnode,
1854                                                  struct nrs_tbf_client,
1855                                                  tc_hnode);
1856
1857         return *opc == cli->tc_opcode;
1858 }
1859
1860 static void *nrs_tbf_opcode_hop_key(struct hlist_node *hnode)
1861 {
1862         struct nrs_tbf_client *cli = hlist_entry(hnode,
1863                                                  struct nrs_tbf_client,
1864                                                  tc_hnode);
1865
1866         return &cli->tc_opcode;
1867 }
1868
1869 static void *nrs_tbf_opcode_hop_object(struct hlist_node *hnode)
1870 {
1871         return hlist_entry(hnode, struct nrs_tbf_client, tc_hnode);
1872 }
1873
1874 static void nrs_tbf_opcode_hop_get(struct cfs_hash *hs,
1875                                    struct hlist_node *hnode)
1876 {
1877         struct nrs_tbf_client *cli = hlist_entry(hnode,
1878                                                  struct nrs_tbf_client,
1879                                                  tc_hnode);
1880
1881         atomic_inc(&cli->tc_ref);
1882 }
1883
1884 static void nrs_tbf_opcode_hop_put(struct cfs_hash *hs,
1885                                    struct hlist_node *hnode)
1886 {
1887         struct nrs_tbf_client *cli = hlist_entry(hnode,
1888                                                  struct nrs_tbf_client,
1889                                                  tc_hnode);
1890
1891         atomic_dec(&cli->tc_ref);
1892 }
1893
1894 static void nrs_tbf_opcode_hop_exit(struct cfs_hash *hs,
1895                                     struct hlist_node *hnode)
1896 {
1897         struct nrs_tbf_client *cli = hlist_entry(hnode,
1898                                                  struct nrs_tbf_client,
1899                                                  tc_hnode);
1900
1901         LASSERTF(atomic_read(&cli->tc_ref) == 0,
1902                  "Busy TBF object from client with opcode %s, with %d refs\n",
1903                  ll_opcode2str(cli->tc_opcode),
1904                  atomic_read(&cli->tc_ref));
1905
1906         nrs_tbf_cli_fini(cli);
1907 }
1908 static struct cfs_hash_ops nrs_tbf_opcode_hash_ops = {
1909         .hs_hash        = nrs_tbf_opcode_hop_hash,
1910         .hs_keycmp      = nrs_tbf_opcode_hop_keycmp,
1911         .hs_key         = nrs_tbf_opcode_hop_key,
1912         .hs_object      = nrs_tbf_opcode_hop_object,
1913         .hs_get         = nrs_tbf_opcode_hop_get,
1914         .hs_put         = nrs_tbf_opcode_hop_put,
1915         .hs_put_locked  = nrs_tbf_opcode_hop_put,
1916         .hs_exit        = nrs_tbf_opcode_hop_exit,
1917 };
1918
1919 static int
1920 nrs_tbf_opcode_startup(struct ptlrpc_nrs_policy *policy,
1921                     struct nrs_tbf_head *head)
1922 {
1923         struct nrs_tbf_cmd      start = { 0 };
1924         int rc;
1925
1926         head->th_cli_hash = cfs_hash_create("nrs_tbf_hash",
1927                                             NRS_TBF_NID_BITS,
1928                                             NRS_TBF_NID_BITS,
1929                                             NRS_TBF_NID_BKT_BITS, 0,
1930                                             CFS_HASH_MIN_THETA,
1931                                             CFS_HASH_MAX_THETA,
1932                                             &nrs_tbf_opcode_hash_ops,
1933                                             CFS_HASH_RW_BKTLOCK);
1934         if (head->th_cli_hash == NULL)
1935                 return -ENOMEM;
1936
1937         start.u.tc_start.ts_opcodes = NULL;
1938         start.u.tc_start.ts_opcodes_str = "*";
1939
1940         start.u.tc_start.ts_rpc_rate = tbf_rate;
1941         start.u.tc_start.ts_rule_flags = NTRS_DEFAULT;
1942         start.tc_name = NRS_TBF_DEFAULT_RULE;
1943         rc = nrs_tbf_rule_start(policy, head, &start);
1944
1945         return rc;
1946 }
1947
1948 static struct nrs_tbf_client *
1949 nrs_tbf_opcode_cli_find(struct nrs_tbf_head *head,
1950                         struct ptlrpc_request *req)
1951 {
1952         __u32 opc;
1953
1954         opc = lustre_msg_get_opc(req->rq_reqmsg);
1955         return cfs_hash_lookup(head->th_cli_hash, &opc);
1956 }
1957
1958 static struct nrs_tbf_client *
1959 nrs_tbf_opcode_cli_findadd(struct nrs_tbf_head *head,
1960                            struct nrs_tbf_client *cli)
1961 {
1962         return cfs_hash_findadd_unique(head->th_cli_hash, &cli->tc_opcode,
1963                                        &cli->tc_hnode);
1964 }
1965
1966 static void
1967 nrs_tbf_opcode_cli_init(struct nrs_tbf_client *cli,
1968                         struct ptlrpc_request *req)
1969 {
1970         cli->tc_opcode = lustre_msg_get_opc(req->rq_reqmsg);
1971 }
1972
1973 #define MAX_OPCODE_LEN  32
1974 static int
1975 nrs_tbf_opcode_set_bit(const struct cfs_lstr *id, struct cfs_bitmap *opcodes)
1976 {
1977         int     op = 0;
1978         char    opcode_str[MAX_OPCODE_LEN];
1979
1980         if (id->ls_len + 1 > MAX_OPCODE_LEN)
1981                 return -EINVAL;
1982
1983         memcpy(opcode_str, id->ls_str, id->ls_len);
1984         opcode_str[id->ls_len] = '\0';
1985
1986         op = ll_str2opcode(opcode_str);
1987         if (op < 0)
1988                 return -EINVAL;
1989
1990         cfs_bitmap_set(opcodes, op);
1991         return 0;
1992 }
1993
1994 static int
1995 nrs_tbf_opcode_list_parse(char *str, int len, struct cfs_bitmap **bitmaptr)
1996 {
1997         struct cfs_bitmap *opcodes;
1998         struct cfs_lstr src;
1999         struct cfs_lstr res;
2000         int rc = 0;
2001         ENTRY;
2002
2003         opcodes = CFS_ALLOCATE_BITMAP(LUSTRE_MAX_OPCODES);
2004         if (opcodes == NULL)
2005                 return -ENOMEM;
2006
2007         src.ls_str = str;
2008         src.ls_len = len;
2009         while (src.ls_str) {
2010                 rc = cfs_gettok(&src, ' ', &res);
2011                 if (rc == 0) {
2012                         rc = -EINVAL;
2013                         break;
2014                 }
2015                 rc = nrs_tbf_opcode_set_bit(&res, opcodes);
2016                 if (rc)
2017                         break;
2018         }
2019
2020         if (rc == 0)
2021                 *bitmaptr = opcodes;
2022         else
2023                 CFS_FREE_BITMAP(opcodes);
2024
2025         RETURN(rc);
2026 }
2027
2028 static void nrs_tbf_opcode_cmd_fini(struct nrs_tbf_cmd *cmd)
2029 {
2030         if (cmd->u.tc_start.ts_opcodes)
2031                 CFS_FREE_BITMAP(cmd->u.tc_start.ts_opcodes);
2032
2033         if (cmd->u.tc_start.ts_opcodes_str)
2034                 OBD_FREE(cmd->u.tc_start.ts_opcodes_str,
2035                          strlen(cmd->u.tc_start.ts_opcodes_str) + 1);
2036
2037 }
2038
2039 static int nrs_tbf_opcode_parse(struct nrs_tbf_cmd *cmd, char *id)
2040 {
2041         struct cfs_lstr src;
2042         int rc;
2043
2044         src.ls_str = id;
2045         src.ls_len = strlen(id);
2046         rc = nrs_tbf_check_id_value(&src, "opcode");
2047         if (rc)
2048                 return rc;
2049
2050         OBD_ALLOC(cmd->u.tc_start.ts_opcodes_str, src.ls_len + 1);
2051         if (cmd->u.tc_start.ts_opcodes_str == NULL)
2052                 return -ENOMEM;
2053
2054         memcpy(cmd->u.tc_start.ts_opcodes_str, src.ls_str, src.ls_len);
2055
2056         /* parse opcode list */
2057         rc = nrs_tbf_opcode_list_parse(cmd->u.tc_start.ts_opcodes_str,
2058                                        strlen(cmd->u.tc_start.ts_opcodes_str),
2059                                        &cmd->u.tc_start.ts_opcodes);
2060         if (rc)
2061                 nrs_tbf_opcode_cmd_fini(cmd);
2062
2063         return rc;
2064 }
2065
2066 static int
2067 nrs_tbf_opcode_rule_match(struct nrs_tbf_rule *rule,
2068                           struct nrs_tbf_client *cli)
2069 {
2070         if (rule->tr_opcodes == NULL)
2071                 return 0;
2072
2073         return cfs_bitmap_check(rule->tr_opcodes, cli->tc_opcode);
2074 }
2075
2076 static int nrs_tbf_opcode_rule_init(struct ptlrpc_nrs_policy *policy,
2077                                     struct nrs_tbf_rule *rule,
2078                                     struct nrs_tbf_cmd *start)
2079 {
2080         int rc = 0;
2081
2082         LASSERT(start->u.tc_start.ts_opcodes_str != NULL);
2083         OBD_ALLOC(rule->tr_opcodes_str,
2084                   strlen(start->u.tc_start.ts_opcodes_str) + 1);
2085         if (rule->tr_opcodes_str == NULL)
2086                 return -ENOMEM;
2087
2088         strncpy(rule->tr_opcodes_str, start->u.tc_start.ts_opcodes_str,
2089                 strlen(start->u.tc_start.ts_opcodes_str) + 1);
2090
2091         /* Default rule '*' */
2092         if (start->u.tc_start.ts_opcodes == NULL)
2093                 return 0;
2094
2095         rc = nrs_tbf_opcode_list_parse(rule->tr_opcodes_str,
2096                                        strlen(rule->tr_opcodes_str),
2097                                        &rule->tr_opcodes);
2098         if (rc)
2099                 OBD_FREE(rule->tr_opcodes_str,
2100                          strlen(start->u.tc_start.ts_opcodes_str) + 1);
2101
2102         return rc;
2103 }
2104
2105 static int
2106 nrs_tbf_opcode_rule_dump(struct nrs_tbf_rule *rule, struct seq_file *m)
2107 {
2108         seq_printf(m, "%s {%s} %llu, ref %d\n", rule->tr_name,
2109                    rule->tr_opcodes_str, rule->tr_rpc_rate,
2110                    atomic_read(&rule->tr_ref) - 1);
2111         return 0;
2112 }
2113
2114
2115 struct nrs_tbf_ops nrs_tbf_opcode_ops = {
2116         .o_name = NRS_TBF_TYPE_OPCODE,
2117         .o_startup = nrs_tbf_opcode_startup,
2118         .o_cli_find = nrs_tbf_opcode_cli_find,
2119         .o_cli_findadd = nrs_tbf_opcode_cli_findadd,
2120         .o_cli_put = nrs_tbf_nid_cli_put,
2121         .o_cli_init = nrs_tbf_opcode_cli_init,
2122         .o_rule_init = nrs_tbf_opcode_rule_init,
2123         .o_rule_dump = nrs_tbf_opcode_rule_dump,
2124         .o_rule_match = nrs_tbf_opcode_rule_match,
2125         .o_rule_fini = nrs_tbf_opcode_rule_fini,
2126 };
2127
2128 static struct nrs_tbf_type nrs_tbf_types[] = {
2129         {
2130                 .ntt_name = NRS_TBF_TYPE_JOBID,
2131                 .ntt_flag = NRS_TBF_FLAG_JOBID,
2132                 .ntt_ops = &nrs_tbf_jobid_ops,
2133         },
2134         {
2135                 .ntt_name = NRS_TBF_TYPE_NID,
2136                 .ntt_flag = NRS_TBF_FLAG_NID,
2137                 .ntt_ops = &nrs_tbf_nid_ops,
2138         },
2139         {
2140                 .ntt_name = NRS_TBF_TYPE_OPCODE,
2141                 .ntt_flag = NRS_TBF_FLAG_OPCODE,
2142                 .ntt_ops = &nrs_tbf_opcode_ops,
2143         },
2144         {
2145                 .ntt_name = NRS_TBF_TYPE_GENERIC,
2146                 .ntt_flag = NRS_TBF_FLAG_GENERIC,
2147                 .ntt_ops = &nrs_tbf_generic_ops,
2148         },
2149 };
2150
2151 /**
2152  * Is called before the policy transitions into
2153  * ptlrpc_nrs_pol_state::NRS_POL_STATE_STARTED; allocates and initializes a
2154  * policy-specific private data structure.
2155  *
2156  * \param[in] policy The policy to start
2157  *
2158  * \retval -ENOMEM OOM error
2159  * \retval  0      success
2160  *
2161  * \see nrs_policy_register()
2162  * \see nrs_policy_ctl()
2163  */
2164 static int nrs_tbf_start(struct ptlrpc_nrs_policy *policy, char *arg)
2165 {
2166         struct nrs_tbf_head     *head;
2167         struct nrs_tbf_ops      *ops;
2168         __u32                    type;
2169         char                    *name;
2170         int found = 0;
2171         int i;
2172         int rc = 0;
2173
2174         if (arg == NULL)
2175                 name = NRS_TBF_TYPE_GENERIC;
2176         else if (strlen(arg) < NRS_TBF_TYPE_MAX_LEN)
2177                 name = arg;
2178         else
2179                 GOTO(out, rc = -EINVAL);
2180
2181         for (i = 0; i < ARRAY_SIZE(nrs_tbf_types); i++) {
2182                 if (strcmp(name, nrs_tbf_types[i].ntt_name) == 0) {
2183                         ops = nrs_tbf_types[i].ntt_ops;
2184                         type = nrs_tbf_types[i].ntt_flag;
2185                         found = 1;
2186                         break;
2187                 }
2188         }
2189         if (found == 0)
2190                 GOTO(out, rc = -ENOTSUPP);
2191
2192         OBD_CPT_ALLOC_PTR(head, nrs_pol2cptab(policy), nrs_pol2cptid(policy));
2193         if (head == NULL)
2194                 GOTO(out, rc = -ENOMEM);
2195
2196         memcpy(head->th_type, name, strlen(name));
2197         head->th_type[strlen(name)] = '\0';
2198         head->th_ops = ops;
2199         head->th_type_flag = type;
2200
2201         head->th_binheap = cfs_binheap_create(&nrs_tbf_heap_ops,
2202                                               CBH_FLAG_ATOMIC_GROW, 4096, NULL,
2203                                               nrs_pol2cptab(policy),
2204                                               nrs_pol2cptid(policy));
2205         if (head->th_binheap == NULL)
2206                 GOTO(out_free_head, rc = -ENOMEM);
2207
2208         atomic_set(&head->th_rule_sequence, 0);
2209         spin_lock_init(&head->th_rule_lock);
2210         INIT_LIST_HEAD(&head->th_list);
2211         hrtimer_init(&head->th_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
2212         head->th_timer.function = nrs_tbf_timer_cb;
2213         rc = head->th_ops->o_startup(policy, head);
2214         if (rc)
2215                 GOTO(out_free_heap, rc);
2216
2217         policy->pol_private = head;
2218         return 0;
2219 out_free_heap:
2220         cfs_binheap_destroy(head->th_binheap);
2221 out_free_head:
2222         OBD_FREE_PTR(head);
2223 out:
2224         return rc;
2225 }
2226
2227 /**
2228  * Is called before the policy transitions into
2229  * ptlrpc_nrs_pol_state::NRS_POL_STATE_STOPPED; deallocates the policy-specific
2230  * private data structure.
2231  *
2232  * \param[in] policy The policy to stop
2233  *
2234  * \see nrs_policy_stop0()
2235  */
2236 static void nrs_tbf_stop(struct ptlrpc_nrs_policy *policy)
2237 {
2238         struct nrs_tbf_head *head = policy->pol_private;
2239         struct ptlrpc_nrs *nrs = policy->pol_nrs;
2240         struct nrs_tbf_rule *rule, *n;
2241
2242         LASSERT(head != NULL);
2243         LASSERT(head->th_cli_hash != NULL);
2244         hrtimer_cancel(&head->th_timer);
2245         /* Should cleanup hash first before free rules */
2246         cfs_hash_putref(head->th_cli_hash);
2247         list_for_each_entry_safe(rule, n, &head->th_list, tr_linkage) {
2248                 list_del_init(&rule->tr_linkage);
2249                 nrs_tbf_rule_put(rule);
2250         }
2251         LASSERT(list_empty(&head->th_list));
2252         LASSERT(head->th_binheap != NULL);
2253         LASSERT(cfs_binheap_is_empty(head->th_binheap));
2254         cfs_binheap_destroy(head->th_binheap);
2255         OBD_FREE_PTR(head);
2256         nrs->nrs_throttling = 0;
2257         wake_up(&policy->pol_nrs->nrs_svcpt->scp_waitq);
2258 }
2259
2260 /**
2261  * Performs a policy-specific ctl function on TBF policy instances; similar
2262  * to ioctl.
2263  *
2264  * \param[in]     policy the policy instance
2265  * \param[in]     opc    the opcode
2266  * \param[in,out] arg    used for passing parameters and information
2267  *
2268  * \pre assert_spin_locked(&policy->pol_nrs->->nrs_lock)
2269  * \post assert_spin_locked(&policy->pol_nrs->->nrs_lock)
2270  *
2271  * \retval 0   operation carried out successfully
2272  * \retval -ve error
2273  */
2274 static int nrs_tbf_ctl(struct ptlrpc_nrs_policy *policy,
2275                        enum ptlrpc_nrs_ctl opc,
2276                        void *arg)
2277 {
2278         int rc = 0;
2279         ENTRY;
2280
2281         assert_spin_locked(&policy->pol_nrs->nrs_lock);
2282
2283         switch ((enum nrs_ctl_tbf)opc) {
2284         default:
2285                 RETURN(-EINVAL);
2286
2287         /**
2288          * Read RPC rate size of a policy instance.
2289          */
2290         case NRS_CTL_TBF_RD_RULE: {
2291                 struct nrs_tbf_head *head = policy->pol_private;
2292                 struct seq_file *m = (struct seq_file *) arg;
2293                 struct ptlrpc_service_part *svcpt;
2294
2295                 svcpt = policy->pol_nrs->nrs_svcpt;
2296                 seq_printf(m, "CPT %d:\n", svcpt->scp_cpt);
2297
2298                 rc = nrs_tbf_rule_dump_all(head, m);
2299                 }
2300                 break;
2301
2302         /**
2303          * Write RPC rate of a policy instance.
2304          */
2305         case NRS_CTL_TBF_WR_RULE: {
2306                 struct nrs_tbf_head *head = policy->pol_private;
2307                 struct nrs_tbf_cmd *cmd;
2308
2309                 cmd = (struct nrs_tbf_cmd *)arg;
2310                 rc = nrs_tbf_command(policy,
2311                                      head,
2312                                      cmd);
2313                 }
2314                 break;
2315         /**
2316          * Read the TBF policy type of a policy instance.
2317          */
2318         case NRS_CTL_TBF_RD_TYPE_FLAG: {
2319                 struct nrs_tbf_head *head = policy->pol_private;
2320
2321                 *(__u32 *)arg = head->th_type_flag;
2322                 }
2323                 break;
2324         }
2325
2326         RETURN(rc);
2327 }
2328
2329 /**
2330  * Is called for obtaining a TBF policy resource.
2331  *
2332  * \param[in]  policy     The policy on which the request is being asked for
2333  * \param[in]  nrq        The request for which resources are being taken
2334  * \param[in]  parent     Parent resource, unused in this policy
2335  * \param[out] resp       Resources references are placed in this array
2336  * \param[in]  moving_req Signifies limited caller context; unused in this
2337  *                        policy
2338  *
2339  *
2340  * \see nrs_resource_get_safe()
2341  */
2342 static int nrs_tbf_res_get(struct ptlrpc_nrs_policy *policy,
2343                            struct ptlrpc_nrs_request *nrq,
2344                            const struct ptlrpc_nrs_resource *parent,
2345                            struct ptlrpc_nrs_resource **resp,
2346                            bool moving_req)
2347 {
2348         struct nrs_tbf_head   *head;
2349         struct nrs_tbf_client *cli;
2350         struct nrs_tbf_client *tmp;
2351         struct ptlrpc_request *req;
2352
2353         if (parent == NULL) {
2354                 *resp = &((struct nrs_tbf_head *)policy->pol_private)->th_res;
2355                 return 0;
2356         }
2357
2358         head = container_of(parent, struct nrs_tbf_head, th_res);
2359         req = container_of(nrq, struct ptlrpc_request, rq_nrq);
2360         cli = head->th_ops->o_cli_find(head, req);
2361         if (cli != NULL) {
2362                 spin_lock(&policy->pol_nrs->nrs_svcpt->scp_req_lock);
2363                 LASSERT(cli->tc_rule);
2364                 if (cli->tc_rule_sequence !=
2365                     atomic_read(&head->th_rule_sequence) ||
2366                     cli->tc_rule->tr_flags & NTRS_STOPPING) {
2367                         struct nrs_tbf_rule *rule;
2368
2369                         rule = nrs_tbf_rule_match(head, cli);
2370                         if (rule != cli->tc_rule) {
2371                                 nrs_tbf_cli_reset(head, rule, cli);
2372                         } else {
2373                                 if (cli->tc_rule_generation != rule->tr_generation)
2374                                         nrs_tbf_cli_reset_value(head, cli);
2375                                 nrs_tbf_rule_put(rule);
2376                         }
2377                 } else if (cli->tc_rule_generation !=
2378                            cli->tc_rule->tr_generation) {
2379                         nrs_tbf_cli_reset_value(head, cli);
2380                 }
2381                 spin_unlock(&policy->pol_nrs->nrs_svcpt->scp_req_lock);
2382                 goto out;
2383         }
2384
2385         OBD_CPT_ALLOC_GFP(cli, nrs_pol2cptab(policy), nrs_pol2cptid(policy),
2386                           sizeof(*cli), moving_req ? GFP_ATOMIC : __GFP_IO);
2387         if (cli == NULL)
2388                 return -ENOMEM;
2389
2390         nrs_tbf_cli_init(head, cli, req);
2391         tmp = head->th_ops->o_cli_findadd(head, cli);
2392         if (tmp != cli) {
2393                 atomic_dec(&cli->tc_ref);
2394                 nrs_tbf_cli_fini(cli);
2395                 cli = tmp;
2396         }
2397 out:
2398         *resp = &cli->tc_res;
2399
2400         return 1;
2401 }
2402
2403 /**
2404  * Called when releasing references to the resource hierachy obtained for a
2405  * request for scheduling using the TBF policy.
2406  *
2407  * \param[in] policy   the policy the resource belongs to
2408  * \param[in] res      the resource to be released
2409  */
2410 static void nrs_tbf_res_put(struct ptlrpc_nrs_policy *policy,
2411                             const struct ptlrpc_nrs_resource *res)
2412 {
2413         struct nrs_tbf_head   *head;
2414         struct nrs_tbf_client *cli;
2415
2416         /**
2417          * Do nothing for freeing parent, nrs_tbf_net resources
2418          */
2419         if (res->res_parent == NULL)
2420                 return;
2421
2422         cli = container_of(res, struct nrs_tbf_client, tc_res);
2423         head = container_of(res->res_parent, struct nrs_tbf_head, th_res);
2424
2425         head->th_ops->o_cli_put(head, cli);
2426 }
2427
2428 /**
2429  * Called when getting a request from the TBF policy for handling, or just
2430  * peeking; removes the request from the policy when it is to be handled.
2431  *
2432  * \param[in] policy The policy
2433  * \param[in] peek   When set, signifies that we just want to examine the
2434  *                   request, and not handle it, so the request is not removed
2435  *                   from the policy.
2436  * \param[in] force  Force the policy to return a request; unused in this
2437  *                   policy
2438  *
2439  * \retval The request to be handled; this is the next request in the TBF
2440  *         rule
2441  *
2442  * \see ptlrpc_nrs_req_get_nolock()
2443  * \see nrs_request_get()
2444  */
2445 static
2446 struct ptlrpc_nrs_request *nrs_tbf_req_get(struct ptlrpc_nrs_policy *policy,
2447                                            bool peek, bool force)
2448 {
2449         struct nrs_tbf_head       *head = policy->pol_private;
2450         struct ptlrpc_nrs_request *nrq = NULL;
2451         struct nrs_tbf_client     *cli;
2452         struct cfs_binheap_node   *node;
2453
2454         assert_spin_locked(&policy->pol_nrs->nrs_svcpt->scp_req_lock);
2455
2456         if (!peek && policy->pol_nrs->nrs_throttling)
2457                 return NULL;
2458
2459         node = cfs_binheap_root(head->th_binheap);
2460         if (unlikely(node == NULL))
2461                 return NULL;
2462
2463         cli = container_of(node, struct nrs_tbf_client, tc_node);
2464         LASSERT(cli->tc_in_heap);
2465         if (peek) {
2466                 nrq = list_entry(cli->tc_list.next,
2467                                      struct ptlrpc_nrs_request,
2468                                      nr_u.tbf.tr_list);
2469         } else {
2470                 __u64 now = ktime_to_ns(ktime_get());
2471                 __u64 passed;
2472                 __u64 ntoken;
2473                 __u64 deadline;
2474
2475                 deadline = cli->tc_check_time +
2476                           cli->tc_nsecs;
2477                 LASSERT(now >= cli->tc_check_time);
2478                 passed = now - cli->tc_check_time;
2479                 ntoken = passed * cli->tc_rpc_rate;
2480                 do_div(ntoken, NSEC_PER_SEC);
2481                 ntoken += cli->tc_ntoken;
2482                 if (ntoken > cli->tc_depth)
2483                         ntoken = cli->tc_depth;
2484                 if (ntoken > 0) {
2485                         struct ptlrpc_request *req;
2486                         nrq = list_entry(cli->tc_list.next,
2487                                              struct ptlrpc_nrs_request,
2488                                              nr_u.tbf.tr_list);
2489                         req = container_of(nrq,
2490                                            struct ptlrpc_request,
2491                                            rq_nrq);
2492                         ntoken--;
2493                         cli->tc_ntoken = ntoken;
2494                         cli->tc_check_time = now;
2495                         list_del_init(&nrq->nr_u.tbf.tr_list);
2496                         if (list_empty(&cli->tc_list)) {
2497                                 cfs_binheap_remove(head->th_binheap,
2498                                                    &cli->tc_node);
2499                                 cli->tc_in_heap = false;
2500                         } else {
2501                                 cfs_binheap_relocate(head->th_binheap,
2502                                                      &cli->tc_node);
2503                         }
2504                         CDEBUG(D_RPCTRACE,
2505                                "NRS start %s request from %s, "
2506                                "seq: %llu\n",
2507                                policy->pol_desc->pd_name,
2508                                libcfs_id2str(req->rq_peer),
2509                                nrq->nr_u.tbf.tr_sequence);
2510                 } else {
2511                         ktime_t time;
2512
2513                         policy->pol_nrs->nrs_throttling = 1;
2514                         head->th_deadline = deadline;
2515                         time = ktime_set(0, 0);
2516                         time = ktime_add_ns(time, deadline);
2517                         hrtimer_start(&head->th_timer, time, HRTIMER_MODE_ABS);
2518                 }
2519         }
2520
2521         return nrq;
2522 }
2523
2524 /**
2525  * Adds request \a nrq to \a policy's list of queued requests
2526  *
2527  * \param[in] policy The policy
2528  * \param[in] nrq    The request to add
2529  *
2530  * \retval 0 success; nrs_request_enqueue() assumes this function will always
2531  *                    succeed
2532  */
2533 static int nrs_tbf_req_add(struct ptlrpc_nrs_policy *policy,
2534                            struct ptlrpc_nrs_request *nrq)
2535 {
2536         struct nrs_tbf_head   *head;
2537         struct nrs_tbf_client *cli;
2538         int                    rc = 0;
2539
2540         assert_spin_locked(&policy->pol_nrs->nrs_svcpt->scp_req_lock);
2541
2542         cli = container_of(nrs_request_resource(nrq),
2543                            struct nrs_tbf_client, tc_res);
2544         head = container_of(nrs_request_resource(nrq)->res_parent,
2545                             struct nrs_tbf_head, th_res);
2546         if (list_empty(&cli->tc_list)) {
2547                 LASSERT(!cli->tc_in_heap);
2548                 rc = cfs_binheap_insert(head->th_binheap, &cli->tc_node);
2549                 if (rc == 0) {
2550                         cli->tc_in_heap = true;
2551                         nrq->nr_u.tbf.tr_sequence = head->th_sequence++;
2552                         list_add_tail(&nrq->nr_u.tbf.tr_list,
2553                                           &cli->tc_list);
2554                         if (policy->pol_nrs->nrs_throttling) {
2555                                 __u64 deadline = cli->tc_check_time +
2556                                                  cli->tc_nsecs;
2557                                 if ((head->th_deadline > deadline) &&
2558                                     (hrtimer_try_to_cancel(&head->th_timer)
2559                                      >= 0)) {
2560                                         ktime_t time;
2561                                         head->th_deadline = deadline;
2562                                         time = ktime_set(0, 0);
2563                                         time = ktime_add_ns(time, deadline);
2564                                         hrtimer_start(&head->th_timer, time,
2565                                                       HRTIMER_MODE_ABS);
2566                                 }
2567                         }
2568                 }
2569         } else {
2570                 LASSERT(cli->tc_in_heap);
2571                 nrq->nr_u.tbf.tr_sequence = head->th_sequence++;
2572                 list_add_tail(&nrq->nr_u.tbf.tr_list,
2573                                   &cli->tc_list);
2574         }
2575         return rc;
2576 }
2577
2578 /**
2579  * Removes request \a nrq from \a policy's list of queued requests.
2580  *
2581  * \param[in] policy The policy
2582  * \param[in] nrq    The request to remove
2583  */
2584 static void nrs_tbf_req_del(struct ptlrpc_nrs_policy *policy,
2585                              struct ptlrpc_nrs_request *nrq)
2586 {
2587         struct nrs_tbf_head   *head;
2588         struct nrs_tbf_client *cli;
2589
2590         assert_spin_locked(&policy->pol_nrs->nrs_svcpt->scp_req_lock);
2591
2592         cli = container_of(nrs_request_resource(nrq),
2593                            struct nrs_tbf_client, tc_res);
2594         head = container_of(nrs_request_resource(nrq)->res_parent,
2595                             struct nrs_tbf_head, th_res);
2596
2597         LASSERT(!list_empty(&nrq->nr_u.tbf.tr_list));
2598         list_del_init(&nrq->nr_u.tbf.tr_list);
2599         if (list_empty(&cli->tc_list)) {
2600                 cfs_binheap_remove(head->th_binheap,
2601                                    &cli->tc_node);
2602                 cli->tc_in_heap = false;
2603         } else {
2604                 cfs_binheap_relocate(head->th_binheap,
2605                                      &cli->tc_node);
2606         }
2607 }
2608
2609 /**
2610  * Prints a debug statement right before the request \a nrq stops being
2611  * handled.
2612  *
2613  * \param[in] policy The policy handling the request
2614  * \param[in] nrq    The request being handled
2615  *
2616  * \see ptlrpc_server_finish_request()
2617  * \see ptlrpc_nrs_req_stop_nolock()
2618  */
2619 static void nrs_tbf_req_stop(struct ptlrpc_nrs_policy *policy,
2620                               struct ptlrpc_nrs_request *nrq)
2621 {
2622         struct ptlrpc_request *req = container_of(nrq, struct ptlrpc_request,
2623                                                   rq_nrq);
2624
2625         assert_spin_locked(&policy->pol_nrs->nrs_svcpt->scp_req_lock);
2626
2627         CDEBUG(D_RPCTRACE, "NRS stop %s request from %s, seq: %llu\n",
2628                policy->pol_desc->pd_name, libcfs_id2str(req->rq_peer),
2629                nrq->nr_u.tbf.tr_sequence);
2630 }
2631
2632 #ifdef CONFIG_PROC_FS
2633
2634 /**
2635  * lprocfs interface
2636  */
2637
2638 /**
2639  * The maximum RPC rate.
2640  */
2641 #define LPROCFS_NRS_RATE_MAX            65535
2642
2643 static int
2644 ptlrpc_lprocfs_nrs_tbf_rule_seq_show(struct seq_file *m, void *data)
2645 {
2646         struct ptlrpc_service       *svc = m->private;
2647         int                          rc;
2648
2649         seq_printf(m, "regular_requests:\n");
2650         /**
2651          * Perform two separate calls to this as only one of the NRS heads'
2652          * policies may be in the ptlrpc_nrs_pol_state::NRS_POL_STATE_STARTED or
2653          * ptlrpc_nrs_pol_state::NRS_POL_STATE_STOPPING state.
2654          */
2655         rc = ptlrpc_nrs_policy_control(svc, PTLRPC_NRS_QUEUE_REG,
2656                                        NRS_POL_NAME_TBF,
2657                                        NRS_CTL_TBF_RD_RULE,
2658                                        false, m);
2659         if (rc == 0) {
2660                 /**
2661                  * -ENOSPC means buf in the parameter m is overflow, return 0
2662                  * here to let upper layer function seq_read alloc a larger
2663                  * memory area and do this process again.
2664                  */
2665         } else if (rc == -ENOSPC) {
2666                 return 0;
2667
2668                 /**
2669                  * Ignore -ENODEV as the regular NRS head's policy may be in the
2670                  * ptlrpc_nrs_pol_state::NRS_POL_STATE_STOPPED state.
2671                  */
2672         } else if (rc != -ENODEV) {
2673                 return rc;
2674         }
2675
2676         if (!nrs_svc_has_hp(svc))
2677                 goto no_hp;
2678
2679         seq_printf(m, "high_priority_requests:\n");
2680         rc = ptlrpc_nrs_policy_control(svc, PTLRPC_NRS_QUEUE_HP,
2681                                        NRS_POL_NAME_TBF,
2682                                        NRS_CTL_TBF_RD_RULE,
2683                                        false, m);
2684         if (rc == 0) {
2685                 /**
2686                  * -ENOSPC means buf in the parameter m is overflow, return 0
2687                  * here to let upper layer function seq_read alloc a larger
2688                  * memory area and do this process again.
2689                  */
2690         } else if (rc == -ENOSPC) {
2691                 return 0;
2692         }
2693
2694 no_hp:
2695
2696         return rc;
2697 }
2698
2699 static int nrs_tbf_id_parse(struct nrs_tbf_cmd *cmd, char *token)
2700 {
2701         int rc;
2702
2703         switch (cmd->u.tc_start.ts_valid_type) {
2704         case NRS_TBF_FLAG_JOBID:
2705                 rc = nrs_tbf_jobid_parse(cmd, token);
2706                 break;
2707         case NRS_TBF_FLAG_NID:
2708                 rc = nrs_tbf_nid_parse(cmd, token);
2709                 break;
2710         case NRS_TBF_FLAG_OPCODE:
2711                 rc = nrs_tbf_opcode_parse(cmd, token);
2712                 break;
2713         case NRS_TBF_FLAG_GENERIC:
2714                 rc = nrs_tbf_generic_parse(cmd, token);
2715                 break;
2716         default:
2717                 RETURN(-EINVAL);
2718         }
2719
2720         return rc;
2721 }
2722
2723 static void nrs_tbf_cmd_fini(struct nrs_tbf_cmd *cmd)
2724 {
2725         if (cmd->tc_cmd == NRS_CTL_TBF_START_RULE) {
2726                 if (cmd->u.tc_start.ts_valid_type == NRS_TBF_FLAG_JOBID)
2727                         nrs_tbf_jobid_cmd_fini(cmd);
2728                 else if (cmd->u.tc_start.ts_valid_type == NRS_TBF_FLAG_NID)
2729                         nrs_tbf_nid_cmd_fini(cmd);
2730                 else if (cmd->u.tc_start.ts_valid_type == NRS_TBF_FLAG_OPCODE)
2731                         nrs_tbf_opcode_cmd_fini(cmd);
2732                 else if (cmd->u.tc_start.ts_valid_type == NRS_TBF_FLAG_GENERIC)
2733                         nrs_tbf_generic_cmd_fini(cmd);
2734         }
2735 }
2736
2737 static bool name_is_valid(const char *name)
2738 {
2739         int i;
2740
2741         for (i = 0; i < strlen(name); i++) {
2742                 if ((!isalnum(name[i])) &&
2743                     (name[i] != '_'))
2744                         return false;
2745         }
2746         return true;
2747 }
2748
2749 static int
2750 nrs_tbf_parse_value_pair(struct nrs_tbf_cmd *cmd, char *buffer)
2751 {
2752         char    *key;
2753         char    *val;
2754         int      rc;
2755         __u64    rate;
2756
2757         val = buffer;
2758         key = strsep(&val, "=");
2759         if (val == NULL || strlen(val) == 0)
2760                 return -EINVAL;
2761
2762         /* Key of the value pair */
2763         if (strcmp(key, "rate") == 0) {
2764                 rc = kstrtoull(val, 10, &rate);
2765                 if (rc)
2766                         return rc;
2767
2768                 if (rate <= 0 || rate >= LPROCFS_NRS_RATE_MAX)
2769                         return -EINVAL;
2770
2771                 if (cmd->tc_cmd == NRS_CTL_TBF_START_RULE)
2772                         cmd->u.tc_start.ts_rpc_rate = rate;
2773                 else if (cmd->tc_cmd == NRS_CTL_TBF_CHANGE_RULE)
2774                         cmd->u.tc_change.tc_rpc_rate = rate;
2775                 else
2776                         return -EINVAL;
2777         }  else if (strcmp(key, "rank") == 0) {
2778                 if (!name_is_valid(val))
2779                         return -EINVAL;
2780
2781                 if (cmd->tc_cmd == NRS_CTL_TBF_START_RULE)
2782                         cmd->u.tc_start.ts_next_name = val;
2783                 else if (cmd->tc_cmd == NRS_CTL_TBF_CHANGE_RULE)
2784                         cmd->u.tc_change.tc_next_name = val;
2785                 else
2786                         return -EINVAL;
2787         } else {
2788                 return -EINVAL;
2789         }
2790         return 0;
2791 }
2792
2793 static int
2794 nrs_tbf_parse_value_pairs(struct nrs_tbf_cmd *cmd, char *buffer)
2795 {
2796         char    *val;
2797         char    *token;
2798         int      rc;
2799
2800         val = buffer;
2801         while (val != NULL && strlen(val) != 0) {
2802                 token = strsep(&val, " ");
2803                 rc = nrs_tbf_parse_value_pair(cmd, token);
2804                 if (rc)
2805                         return rc;
2806         }
2807
2808         switch (cmd->tc_cmd) {
2809         case NRS_CTL_TBF_START_RULE:
2810                 if (cmd->u.tc_start.ts_rpc_rate == 0)
2811                         cmd->u.tc_start.ts_rpc_rate = tbf_rate;
2812                 break;
2813         case NRS_CTL_TBF_CHANGE_RULE:
2814                 if (cmd->u.tc_change.tc_rpc_rate == 0 &&
2815                     cmd->u.tc_change.tc_next_name == NULL)
2816                         return -EINVAL;
2817                 break;
2818         case NRS_CTL_TBF_STOP_RULE:
2819                 break;
2820         default:
2821                 return -EINVAL;
2822         }
2823         return 0;
2824 }
2825
2826 static struct nrs_tbf_cmd *
2827 nrs_tbf_parse_cmd(char *buffer, unsigned long count, __u32 type_flag)
2828 {
2829         static struct nrs_tbf_cmd       *cmd;
2830         char                            *token;
2831         char                            *val;
2832         int                              rc = 0;
2833
2834         OBD_ALLOC_PTR(cmd);
2835         if (cmd == NULL)
2836                 GOTO(out, rc = -ENOMEM);
2837         memset(cmd, 0, sizeof(*cmd));
2838
2839         val = buffer;
2840         token = strsep(&val, " ");
2841         if (val == NULL || strlen(val) == 0)
2842                 GOTO(out_free_cmd, rc = -EINVAL);
2843
2844         /* Type of the command */
2845         if (strcmp(token, "start") == 0) {
2846                 cmd->tc_cmd = NRS_CTL_TBF_START_RULE;
2847                 cmd->u.tc_start.ts_valid_type = type_flag;
2848         } else if (strcmp(token, "stop") == 0)
2849                 cmd->tc_cmd = NRS_CTL_TBF_STOP_RULE;
2850         else if (strcmp(token, "change") == 0)
2851                 cmd->tc_cmd = NRS_CTL_TBF_CHANGE_RULE;
2852         else
2853                 GOTO(out_free_cmd, rc = -EINVAL);
2854
2855         /* Name of the rule */
2856         token = strsep(&val, " ");
2857         if ((val == NULL && cmd->tc_cmd != NRS_CTL_TBF_STOP_RULE) ||
2858             !name_is_valid(token))
2859                 GOTO(out_free_cmd, rc = -EINVAL);
2860         cmd->tc_name = token;
2861
2862         if (cmd->tc_cmd == NRS_CTL_TBF_START_RULE) {
2863                 /* List of ID */
2864                 LASSERT(val);
2865                 token = val;
2866                 val = strrchr(token, '}');
2867                 if (!val)
2868                         GOTO(out_free_cmd, rc = -EINVAL);
2869
2870                 /* Skip '}' */
2871                 val++;
2872                 if (*val == '\0') {
2873                         val = NULL;
2874                 } else if (*val == ' ') {
2875                         *val = '\0';
2876                         val++;
2877                 } else
2878                         GOTO(out_free_cmd, rc = -EINVAL);
2879
2880                 rc = nrs_tbf_id_parse(cmd, token);
2881                 if (rc)
2882                         GOTO(out_free_cmd, rc);
2883         }
2884
2885         rc = nrs_tbf_parse_value_pairs(cmd, val);
2886         if (rc)
2887                 GOTO(out_cmd_fini, rc = -EINVAL);
2888         goto out;
2889 out_cmd_fini:
2890         nrs_tbf_cmd_fini(cmd);
2891 out_free_cmd:
2892         OBD_FREE_PTR(cmd);
2893 out:
2894         if (rc)
2895                 cmd = ERR_PTR(rc);
2896         return cmd;
2897 }
2898
2899 /**
2900  * Get the TBF policy type (nid, jobid, etc) preset by
2901  * proc entry 'nrs_policies' for command buffer parsing.
2902  *
2903  * \param[in] svc the PTLRPC service
2904  * \param[in] queue the NRS queue type
2905  *
2906  * \retval the preset TBF policy type flag
2907  */
2908 static __u32
2909 nrs_tbf_type_flag(struct ptlrpc_service *svc, enum ptlrpc_nrs_queue_type queue)
2910 {
2911         __u32   type;
2912         int     rc;
2913
2914         rc = ptlrpc_nrs_policy_control(svc, queue,
2915                                        NRS_POL_NAME_TBF,
2916                                        NRS_CTL_TBF_RD_TYPE_FLAG,
2917                                        true, &type);
2918         if (rc != 0)
2919                 type = NRS_TBF_FLAG_INVALID;
2920
2921         return type;
2922 }
2923
2924 extern struct nrs_core nrs_core;
2925 #define LPROCFS_WR_NRS_TBF_MAX_CMD (4096)
2926 static ssize_t
2927 ptlrpc_lprocfs_nrs_tbf_rule_seq_write(struct file *file,
2928                                       const char __user *buffer,
2929                                       size_t count, loff_t *off)
2930 {
2931         struct seq_file           *m = file->private_data;
2932         struct ptlrpc_service     *svc = m->private;
2933         char                      *kernbuf;
2934         char                      *val;
2935         int                        rc;
2936         static struct nrs_tbf_cmd *cmd;
2937         enum ptlrpc_nrs_queue_type queue = PTLRPC_NRS_QUEUE_BOTH;
2938         unsigned long              length;
2939         char                      *token;
2940
2941         OBD_ALLOC(kernbuf, LPROCFS_WR_NRS_TBF_MAX_CMD);
2942         if (kernbuf == NULL)
2943                 GOTO(out, rc = -ENOMEM);
2944
2945         if (count > LPROCFS_WR_NRS_TBF_MAX_CMD - 1)
2946                 GOTO(out_free_kernbuff, rc = -EINVAL);
2947
2948         if (copy_from_user(kernbuf, buffer, count))
2949                 GOTO(out_free_kernbuff, rc = -EFAULT);
2950
2951         val = kernbuf;
2952         token = strsep(&val, " ");
2953         if (val == NULL)
2954                 GOTO(out_free_kernbuff, rc = -EINVAL);
2955
2956         if (strcmp(token, "reg") == 0) {
2957                 queue = PTLRPC_NRS_QUEUE_REG;
2958         } else if (strcmp(token, "hp") == 0) {
2959                 queue = PTLRPC_NRS_QUEUE_HP;
2960         } else {
2961                 kernbuf[strlen(token)] = ' ';
2962                 val = kernbuf;
2963         }
2964         length = strlen(val);
2965
2966         if (length == 0)
2967                 GOTO(out_free_kernbuff, rc = -EINVAL);
2968
2969         if (queue == PTLRPC_NRS_QUEUE_HP && !nrs_svc_has_hp(svc))
2970                 GOTO(out_free_kernbuff, rc = -ENODEV);
2971         else if (queue == PTLRPC_NRS_QUEUE_BOTH && !nrs_svc_has_hp(svc))
2972                 queue = PTLRPC_NRS_QUEUE_REG;
2973
2974         cmd = nrs_tbf_parse_cmd(val, length, nrs_tbf_type_flag(svc, queue));
2975         if (IS_ERR(cmd))
2976                 GOTO(out_free_kernbuff, rc = PTR_ERR(cmd));
2977
2978         /**
2979          * Serialize NRS core lprocfs operations with policy registration/
2980          * unregistration.
2981          */
2982         mutex_lock(&nrs_core.nrs_mutex);
2983         rc = ptlrpc_nrs_policy_control(svc, queue,
2984                                        NRS_POL_NAME_TBF,
2985                                        NRS_CTL_TBF_WR_RULE,
2986                                        false, cmd);
2987         mutex_unlock(&nrs_core.nrs_mutex);
2988
2989         nrs_tbf_cmd_fini(cmd);
2990         OBD_FREE_PTR(cmd);
2991 out_free_kernbuff:
2992         OBD_FREE(kernbuf, LPROCFS_WR_NRS_TBF_MAX_CMD);
2993 out:
2994         return rc ? rc : count;
2995 }
2996 LPROC_SEQ_FOPS(ptlrpc_lprocfs_nrs_tbf_rule);
2997
2998 /**
2999  * Initializes a TBF policy's lprocfs interface for service \a svc
3000  *
3001  * \param[in] svc the service
3002  *
3003  * \retval 0    success
3004  * \retval != 0 error
3005  */
3006 static int nrs_tbf_lprocfs_init(struct ptlrpc_service *svc)
3007 {
3008         struct lprocfs_vars nrs_tbf_lprocfs_vars[] = {
3009                 { .name         = "nrs_tbf_rule",
3010                   .fops         = &ptlrpc_lprocfs_nrs_tbf_rule_fops,
3011                   .data = svc },
3012                 { NULL }
3013         };
3014
3015         if (svc->srv_procroot == NULL)
3016                 return 0;
3017
3018         return lprocfs_add_vars(svc->srv_procroot, nrs_tbf_lprocfs_vars, NULL);
3019 }
3020
3021 /**
3022  * Cleans up a TBF policy's lprocfs interface for service \a svc
3023  *
3024  * \param[in] svc the service
3025  */
3026 static void nrs_tbf_lprocfs_fini(struct ptlrpc_service *svc)
3027 {
3028         if (svc->srv_procroot == NULL)
3029                 return;
3030
3031         lprocfs_remove_proc_entry("nrs_tbf_rule", svc->srv_procroot);
3032 }
3033
3034 #endif /* CONFIG_PROC_FS */
3035
3036 /**
3037  * TBF policy operations
3038  */
3039 static const struct ptlrpc_nrs_pol_ops nrs_tbf_ops = {
3040         .op_policy_start        = nrs_tbf_start,
3041         .op_policy_stop         = nrs_tbf_stop,
3042         .op_policy_ctl          = nrs_tbf_ctl,
3043         .op_res_get             = nrs_tbf_res_get,
3044         .op_res_put             = nrs_tbf_res_put,
3045         .op_req_get             = nrs_tbf_req_get,
3046         .op_req_enqueue         = nrs_tbf_req_add,
3047         .op_req_dequeue         = nrs_tbf_req_del,
3048         .op_req_stop            = nrs_tbf_req_stop,
3049 #ifdef CONFIG_PROC_FS
3050         .op_lprocfs_init        = nrs_tbf_lprocfs_init,
3051         .op_lprocfs_fini        = nrs_tbf_lprocfs_fini,
3052 #endif
3053 };
3054
3055 /**
3056  * TBF policy configuration
3057  */
3058 struct ptlrpc_nrs_pol_conf nrs_conf_tbf = {
3059         .nc_name                = NRS_POL_NAME_TBF,
3060         .nc_ops                 = &nrs_tbf_ops,
3061         .nc_compat              = nrs_policy_compat_all,
3062 };
3063
3064 /** @} tbf */
3065
3066 /** @} nrs */
3067
3068 #endif /* HAVE_SERVER_SUPPORT */