Motr  M0
flt_eval.c
Go to the documentation of this file.
1 /* -*- C -*- */
2 /*
3  * Copyright (c) 2017-2020 Seagate Technology LLC and/or its Affiliates
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * For any questions about this software or licensing,
18  * please email opensource@seagate.com or cortx-questions@seagate.com.
19  *
20  */
21 
22 
23 #define M0_TRACE_SUBSYSTEM M0_TRACE_SUBSYS_FDMI
24 #include "lib/trace.h"
25 
26 #include "lib/types.h"
27 #include "lib/errno.h"
28 
29 #include "fdmi/filter.h"
30 #include "fdmi/flt_eval.h"
31 
32 #include "conf/obj.h" /* m0_conf_fdmi_filter */
33 
34 static int eval_or(struct m0_fdmi_flt_operands *opnds,
35  struct m0_fdmi_flt_operand *res)
36 {
37  int rc = 0;
38 
39  M0_ENTRY();
40 
41  if (opnds->ffp_count != 2 ||
42  opnds->ffp_operands[0].ffo_type != M0_FF_OPND_BOOL ||
43  opnds->ffp_operands[1].ffo_type != M0_FF_OPND_BOOL) {
44  rc = -EINVAL;
45  } else {
47  opnds->ffp_operands[0].ffo_data.fpl_pld.fpl_boolean ||
48  opnds->ffp_operands[1].ffo_data.fpl_pld.fpl_boolean);
49  }
50  return M0_RC(rc);
51 }
52 
53 static int eval_gt(struct m0_fdmi_flt_operands *opnds,
54  struct m0_fdmi_flt_operand *res)
55 {
56  int rc = 0;
57  enum m0_fdmi_flt_operand_type opnd_type;
58 
59  M0_ENTRY();
60 
61  if (opnds->ffp_count != 2) {
62  return M0_RC(-EINVAL);
63  }
64 
65  opnd_type = opnds->ffp_operands[0].ffo_type;
66 
67  /* Both operands should be int or both operands shoulb be uint */
68  if (opnd_type == M0_FF_OPND_UINT) {
69  if (opnds->ffp_operands[1].ffo_type != M0_FF_OPND_UINT) {
70  rc = -EINVAL;
71  } else {
73  !!(opnds->ffp_operands[0].ffo_data.
74  fpl_pld.fpl_uinteger >
75  opnds->ffp_operands[1].ffo_data.
76  fpl_pld.fpl_uinteger));
77  }
78  } else if (opnd_type == M0_FF_OPND_INT) {
79  if (opnds->ffp_operands[1].ffo_type != M0_FF_OPND_INT) {
80  rc = -EINVAL;
81  } else {
83  !!(opnds->ffp_operands[0].ffo_data.
84  fpl_pld.fpl_integer >
85  opnds->ffp_operands[1].ffo_data.
86  fpl_pld.fpl_integer));
87  }
88  } else {
89  rc = -EINVAL;
90  }
91 
92  return M0_RC(rc);
93 }
94 
96 {
97  handlers[M0_FFO_OR] = eval_or;
98  handlers[M0_FFO_GT] = eval_gt;
99 }
100 
102  enum m0_fdmi_flt_op_code op,
104 {
105  int rc = 0;
106 
107  M0_ENTRY("ctx=%p, op=%d", ctx, op);
109 
110  if (ctx->opers[op] != NULL) {
111  rc = -EEXIST;
112  } else {
113  ctx->opers[op] = cb;
114  }
115 
116  return M0_RC(rc);
117 }
118 
119 M0_INTERNAL void m0_fdmi_eval_del_op_cb(struct m0_fdmi_eval_ctx *ctx,
120  enum m0_fdmi_flt_op_code op)
121 {
122  M0_ENTRY("ctx=%p, op=%d", ctx, op);
124 
125  ctx->opers[op] = NULL;
126 
127  M0_LEAVE();
128 }
129 
130 M0_INTERNAL void m0_fdmi_eval_init(struct m0_fdmi_eval_ctx *ctx)
131 {
132  M0_ENTRY("ctx=%p", ctx);
133  M0_SET_ARR0(ctx->opers);
135  M0_LEAVE();
136 }
137 
138 static int eval_flt_node(struct m0_fdmi_eval_ctx *ctx,
139  struct m0_fdmi_flt_node *node,
140  struct m0_fdmi_flt_operand *res,
141  struct m0_fdmi_eval_var_info *var_info)
142 {
143  struct m0_fdmi_flt_op_node *on = &node->ffn_u.ffn_oper;
144  struct m0_fdmi_flt_operands operands = {0};
145  int rc = 0;
146  int i;
147 
148  M0_ENTRY();
149 
150  switch (node->ffn_type) {
152  /* Gather operands and execute operation. */
153  for (i = 0; i < on->ffon_opnds.fno_cnt; i ++) {
154  rc = eval_flt_node(ctx, (struct m0_fdmi_flt_node *)
155  on->ffon_opnds.fno_opnds[i].ffnp_ptr,
156  &operands.ffp_operands[i], var_info);
157  if (rc != 0)
158  break;
159  operands.ffp_count++;
160  }
161 
162  if (rc == 0)
163  rc = ctx->opers[on->ffon_op_code](&operands, res);
165  break;
166  case M0_FLT_OPERAND_NODE:
167  *res = node->ffn_u.ffn_operand;
168  rc = 0;
169  break;
171  if (var_info->get_value_cb != NULL)
172  rc = var_info->get_value_cb(var_info->user_data,
173  &node->ffn_u.ffn_var, res);
174  else
175  rc = -EINVAL;
176  break;
177  default:
178  M0_ASSERT(false);
179  }
180 
181  return M0_RC(rc);
182 }
183 
184 M0_INTERNAL int m0_fdmi_eval_flt(struct m0_fdmi_eval_ctx *ctx,
185  struct m0_conf_fdmi_filter *filter,
186  struct m0_fdmi_eval_var_info *var_info)
187 {
188  int rc;
189  struct m0_fdmi_flt_operand res;
190 
191  M0_ENTRY();
192 
193  rc = eval_flt_node(ctx, filter->ff_filter.ff_root, &res, var_info);
194 
195  if (rc == 0) {
196  M0_ASSERT(res.ffo_type == M0_FF_OPND_BOOL);
197  M0_ASSERT(res.ffo_data.fpl_type == M0_FF_OPND_PLD_BOOL);
198  rc = res.ffo_data.fpl_pld.fpl_boolean;
199  }
200 
201  return M0_RC(rc);
202 }
203 
204 M0_INTERNAL void m0_fdmi_eval_fini(struct m0_fdmi_eval_ctx *ctx)
205 {
206  M0_ENTRY("ctx=%p", ctx);
207  M0_LEAVE();
208 }
209 
210 #undef M0_TRACE_SUBSYSTEM
211 
212 /*
213  * Local variables:
214  * c-indentation-style: "K&R"
215  * c-basic-offset: 8
216  * tab-width: 8
217  * fill-column: 80
218  * scroll-step: 1
219  * End:
220  */
221 /*
222  * vim: tabstop=8 shiftwidth=8 noexpandtab textwidth=80 nowrap
223  */
M0_INTERNAL int m0_fdmi_eval_add_op_cb(struct m0_fdmi_eval_ctx *ctx, enum m0_fdmi_flt_op_code op, m0_fdmi_flt_op_cb_t cb)
Definition: flt_eval.c:101
#define M0_PRE(cond)
#define NULL
Definition: misc.h:38
m0_fdmi_flt_operand_type
Definition: filter.h:126
M0_INTERNAL void m0_fdmi_flt_bool_opnd_fill(struct m0_fdmi_flt_operand *opnd, bool value)
Definition: filter.c:125
M0_LEAVE()
m0_fdmi_flt_op_code
Definition: filter.h:96
static struct net_test_cmd_node * node
Definition: commands.c:72
struct m0_fdmi_flt_opnd_pld ffo_data
Definition: filter.h:177
uint32_t ffo_type
Definition: filter.h:175
struct m0_fdmi_flt_node_ptr * fno_opnds
Definition: filter.h:212
M0_INTERNAL void m0_fdmi_eval_del_op_cb(struct m0_fdmi_eval_ctx *ctx, enum m0_fdmi_flt_op_code op)
Definition: flt_eval.c:119
return M0_RC(rc)
op
Definition: libdemo.c:64
M0_INTERNAL void m0_fdmi_eval_init(struct m0_fdmi_eval_ctx *ctx)
Definition: flt_eval.c:130
#define M0_ENTRY(...)
Definition: trace.h:170
Definition: filter.py:1
int i
Definition: dir.c:1033
#define M0_SET_ARR0(arr)
Definition: misc.h:72
struct m0_fdmi_flt_op_node_opnds ffon_opnds
Definition: filter.h:221
union m0_fdmi_flt_opnd_pld::@169 fpl_pld
#define M0_ASSERT(cond)
int(* get_value_cb)(void *user_data, struct m0_fdmi_flt_var_node *value_desc, struct m0_fdmi_flt_operand *value)
Definition: flt_eval.h:70
M0_INTERNAL int m0_fdmi_eval_flt(struct m0_fdmi_eval_ctx *ctx, struct m0_conf_fdmi_filter *filter, struct m0_fdmi_eval_var_info *var_info)
Definition: flt_eval.c:184
int(* m0_fdmi_flt_op_cb_t)(struct m0_fdmi_flt_operands *opnds, struct m0_fdmi_flt_operand *res)
Definition: flt_eval.h:51
uint32_t ffon_op_code
Definition: filter.h:220
struct m0_fdmi_flt_operand ffp_operands[FDMI_FLT_MAX_OPNDS_NR]
Definition: flt_eval.h:45
static int eval_flt_node(struct m0_fdmi_eval_ctx *ctx, struct m0_fdmi_flt_node *node, struct m0_fdmi_flt_operand *res, struct m0_fdmi_eval_var_info *var_info)
Definition: flt_eval.c:138
static void init_std_operation_handlers(m0_fdmi_flt_op_cb_t *handlers)
Definition: flt_eval.c:95
static int eval_gt(struct m0_fdmi_flt_operands *opnds, struct m0_fdmi_flt_operand *res)
Definition: flt_eval.c:53
static int eval_or(struct m0_fdmi_flt_operands *opnds, struct m0_fdmi_flt_operand *res)
Definition: flt_eval.c:34
Definition: nucleus.c:42
M0_INTERNAL void m0_fdmi_eval_fini(struct m0_fdmi_eval_ctx *ctx)
Definition: flt_eval.c:204
int32_t rc
Definition: trigger_fop.h:47