Fix: network instrumentation protocol enum
[lttng-modules.git] / lttng-filter.c
CommitLineData
07dfc1d0
MD
1/*
2 * lttng-filter.c
3 *
4 * LTTng modules filter code.
5 *
bbf3aef5 6 * Copyright (C) 2010-2016 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
07dfc1d0 7 *
bbf3aef5
MD
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
07dfc1d0 14 *
bbf3aef5
MD
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
07dfc1d0 17 *
bbf3aef5
MD
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 * SOFTWARE.
07dfc1d0
MD
25 */
26
27#include <linux/list.h>
28#include <linux/slab.h>
29
241ae9a8 30#include <lttng-filter.h>
07dfc1d0
MD
31
32static const char *opnames[] = {
33 [ FILTER_OP_UNKNOWN ] = "UNKNOWN",
34
35 [ FILTER_OP_RETURN ] = "RETURN",
36
37 /* binary */
38 [ FILTER_OP_MUL ] = "MUL",
39 [ FILTER_OP_DIV ] = "DIV",
40 [ FILTER_OP_MOD ] = "MOD",
41 [ FILTER_OP_PLUS ] = "PLUS",
42 [ FILTER_OP_MINUS ] = "MINUS",
43 [ FILTER_OP_RSHIFT ] = "RSHIFT",
44 [ FILTER_OP_LSHIFT ] = "LSHIFT",
45 [ FILTER_OP_BIN_AND ] = "BIN_AND",
46 [ FILTER_OP_BIN_OR ] = "BIN_OR",
47 [ FILTER_OP_BIN_XOR ] = "BIN_XOR",
48
49 /* binary comparators */
50 [ FILTER_OP_EQ ] = "EQ",
51 [ FILTER_OP_NE ] = "NE",
52 [ FILTER_OP_GT ] = "GT",
53 [ FILTER_OP_LT ] = "LT",
54 [ FILTER_OP_GE ] = "GE",
55 [ FILTER_OP_LE ] = "LE",
56
57 /* string binary comparators */
58 [ FILTER_OP_EQ_STRING ] = "EQ_STRING",
59 [ FILTER_OP_NE_STRING ] = "NE_STRING",
60 [ FILTER_OP_GT_STRING ] = "GT_STRING",
61 [ FILTER_OP_LT_STRING ] = "LT_STRING",
62 [ FILTER_OP_GE_STRING ] = "GE_STRING",
63 [ FILTER_OP_LE_STRING ] = "LE_STRING",
64
65 /* s64 binary comparators */
66 [ FILTER_OP_EQ_S64 ] = "EQ_S64",
67 [ FILTER_OP_NE_S64 ] = "NE_S64",
68 [ FILTER_OP_GT_S64 ] = "GT_S64",
69 [ FILTER_OP_LT_S64 ] = "LT_S64",
70 [ FILTER_OP_GE_S64 ] = "GE_S64",
71 [ FILTER_OP_LE_S64 ] = "LE_S64",
72
73 /* double binary comparators */
74 [ FILTER_OP_EQ_DOUBLE ] = "EQ_DOUBLE",
75 [ FILTER_OP_NE_DOUBLE ] = "NE_DOUBLE",
76 [ FILTER_OP_GT_DOUBLE ] = "GT_DOUBLE",
77 [ FILTER_OP_LT_DOUBLE ] = "LT_DOUBLE",
78 [ FILTER_OP_GE_DOUBLE ] = "GE_DOUBLE",
79 [ FILTER_OP_LE_DOUBLE ] = "LE_DOUBLE",
80
81 /* Mixed S64-double binary comparators */
82 [ FILTER_OP_EQ_DOUBLE_S64 ] = "EQ_DOUBLE_S64",
83 [ FILTER_OP_NE_DOUBLE_S64 ] = "NE_DOUBLE_S64",
84 [ FILTER_OP_GT_DOUBLE_S64 ] = "GT_DOUBLE_S64",
85 [ FILTER_OP_LT_DOUBLE_S64 ] = "LT_DOUBLE_S64",
86 [ FILTER_OP_GE_DOUBLE_S64 ] = "GE_DOUBLE_S64",
87 [ FILTER_OP_LE_DOUBLE_S64 ] = "LE_DOUBLE_S64",
88
89 [ FILTER_OP_EQ_S64_DOUBLE ] = "EQ_S64_DOUBLE",
90 [ FILTER_OP_NE_S64_DOUBLE ] = "NE_S64_DOUBLE",
91 [ FILTER_OP_GT_S64_DOUBLE ] = "GT_S64_DOUBLE",
92 [ FILTER_OP_LT_S64_DOUBLE ] = "LT_S64_DOUBLE",
93 [ FILTER_OP_GE_S64_DOUBLE ] = "GE_S64_DOUBLE",
94 [ FILTER_OP_LE_S64_DOUBLE ] = "LE_S64_DOUBLE",
95
96 /* unary */
97 [ FILTER_OP_UNARY_PLUS ] = "UNARY_PLUS",
98 [ FILTER_OP_UNARY_MINUS ] = "UNARY_MINUS",
99 [ FILTER_OP_UNARY_NOT ] = "UNARY_NOT",
100 [ FILTER_OP_UNARY_PLUS_S64 ] = "UNARY_PLUS_S64",
101 [ FILTER_OP_UNARY_MINUS_S64 ] = "UNARY_MINUS_S64",
102 [ FILTER_OP_UNARY_NOT_S64 ] = "UNARY_NOT_S64",
103 [ FILTER_OP_UNARY_PLUS_DOUBLE ] = "UNARY_PLUS_DOUBLE",
104 [ FILTER_OP_UNARY_MINUS_DOUBLE ] = "UNARY_MINUS_DOUBLE",
105 [ FILTER_OP_UNARY_NOT_DOUBLE ] = "UNARY_NOT_DOUBLE",
106
107 /* logical */
108 [ FILTER_OP_AND ] = "AND",
109 [ FILTER_OP_OR ] = "OR",
110
111 /* load field ref */
112 [ FILTER_OP_LOAD_FIELD_REF ] = "LOAD_FIELD_REF",
113 [ FILTER_OP_LOAD_FIELD_REF_STRING ] = "LOAD_FIELD_REF_STRING",
114 [ FILTER_OP_LOAD_FIELD_REF_SEQUENCE ] = "LOAD_FIELD_REF_SEQUENCE",
115 [ FILTER_OP_LOAD_FIELD_REF_S64 ] = "LOAD_FIELD_REF_S64",
116 [ FILTER_OP_LOAD_FIELD_REF_DOUBLE ] = "LOAD_FIELD_REF_DOUBLE",
117
118 /* load from immediate operand */
119 [ FILTER_OP_LOAD_STRING ] = "LOAD_STRING",
120 [ FILTER_OP_LOAD_S64 ] = "LOAD_S64",
121 [ FILTER_OP_LOAD_DOUBLE ] = "LOAD_DOUBLE",
122
123 /* cast */
124 [ FILTER_OP_CAST_TO_S64 ] = "CAST_TO_S64",
125 [ FILTER_OP_CAST_DOUBLE_TO_S64 ] = "CAST_DOUBLE_TO_S64",
126 [ FILTER_OP_CAST_NOP ] = "CAST_NOP",
127
128 /* get context ref */
129 [ FILTER_OP_GET_CONTEXT_REF ] = "GET_CONTEXT_REF",
130 [ FILTER_OP_GET_CONTEXT_REF_STRING ] = "GET_CONTEXT_REF_STRING",
131 [ FILTER_OP_GET_CONTEXT_REF_S64 ] = "GET_CONTEXT_REF_S64",
132 [ FILTER_OP_GET_CONTEXT_REF_DOUBLE ] = "GET_CONTEXT_REF_DOUBLE",
f127e61e
MD
133
134 /* load userspace field ref */
135 [ FILTER_OP_LOAD_FIELD_REF_USER_STRING ] = "LOAD_FIELD_REF_USER_STRING",
136 [ FILTER_OP_LOAD_FIELD_REF_USER_SEQUENCE ] = "LOAD_FIELD_REF_USER_SEQUENCE",
83fb57ab
MD
137
138 /*
139 * load immediate star globbing pattern (literal string)
140 * from immediate.
141 */
142 [ FILTER_OP_LOAD_STAR_GLOB_STRING ] = "LOAD_STAR_GLOB_STRING",
143
144 /* globbing pattern binary operator: apply to */
145 [ FILTER_OP_EQ_STAR_GLOB_STRING ] = "EQ_STAR_GLOB_STRING",
146 [ FILTER_OP_NE_STAR_GLOB_STRING ] = "NE_STAR_GLOB_STRING",
07dfc1d0
MD
147};
148
149const char *lttng_filter_print_op(enum filter_op op)
150{
151 if (op >= NR_FILTER_OPS)
152 return "UNKNOWN";
153 else
154 return opnames[op];
155}
156
157static
158int apply_field_reloc(struct lttng_event *event,
159 struct bytecode_runtime *runtime,
160 uint32_t runtime_len,
161 uint32_t reloc_offset,
162 const char *field_name)
163{
164 const struct lttng_event_desc *desc;
165 const struct lttng_event_field *fields, *field = NULL;
166 unsigned int nr_fields, i;
167 struct field_ref *field_ref;
168 struct load_op *op;
169 uint32_t field_offset = 0;
170
171 dbg_printk("Apply field reloc: %u %s\n", reloc_offset, field_name);
172
173 /* Lookup event by name */
174 desc = event->desc;
175 if (!desc)
176 return -EINVAL;
177 fields = desc->fields;
178 if (!fields)
179 return -EINVAL;
180 nr_fields = desc->nr_fields;
181 for (i = 0; i < nr_fields; i++) {
182 if (!strcmp(fields[i].name, field_name)) {
183 field = &fields[i];
184 break;
185 }
186 /* compute field offset */
187 switch (fields[i].type.atype) {
188 case atype_integer:
189 case atype_enum:
190 field_offset += sizeof(int64_t);
191 break;
192 case atype_array:
193 case atype_sequence:
194 field_offset += sizeof(unsigned long);
195 field_offset += sizeof(void *);
196 break;
197 case atype_string:
198 field_offset += sizeof(void *);
199 break;
f513b2bf
MD
200 case atype_struct: /* Unsupported. */
201 case atype_array_compound: /* Unsupported. */
202 case atype_sequence_compound: /* Unsupported. */
65c85aa6 203 case atype_variant: /* Unsupported. */
07dfc1d0
MD
204 default:
205 return -EINVAL;
206 }
207 }
208 if (!field)
209 return -EINVAL;
210
211 /* Check if field offset is too large for 16-bit offset */
f127e61e 212 if (field_offset > LTTNG_KERNEL_FILTER_BYTECODE_MAX_LEN - 1)
07dfc1d0
MD
213 return -EINVAL;
214
215 /* set type */
216 op = (struct load_op *) &runtime->data[reloc_offset];
217 field_ref = (struct field_ref *) op->data;
218 switch (field->type.atype) {
219 case atype_integer:
220 case atype_enum:
221 op->op = FILTER_OP_LOAD_FIELD_REF_S64;
222 break;
223 case atype_array:
224 case atype_sequence:
f127e61e
MD
225 if (field->user)
226 op->op = FILTER_OP_LOAD_FIELD_REF_USER_SEQUENCE;
227 else
228 op->op = FILTER_OP_LOAD_FIELD_REF_SEQUENCE;
07dfc1d0
MD
229 break;
230 case atype_string:
f127e61e
MD
231 if (field->user)
232 op->op = FILTER_OP_LOAD_FIELD_REF_USER_STRING;
233 else
234 op->op = FILTER_OP_LOAD_FIELD_REF_STRING;
07dfc1d0 235 break;
f513b2bf
MD
236 case atype_struct: /* Unsupported. */
237 case atype_array_compound: /* Unsupported. */
238 case atype_sequence_compound: /* Unsupported. */
65c85aa6 239 case atype_variant: /* Unsupported. */
07dfc1d0
MD
240 default:
241 return -EINVAL;
242 }
243 /* set offset */
244 field_ref->offset = (uint16_t) field_offset;
245 return 0;
246}
247
248static
249int apply_context_reloc(struct lttng_event *event,
250 struct bytecode_runtime *runtime,
251 uint32_t runtime_len,
252 uint32_t reloc_offset,
253 const char *context_name)
254{
255 struct field_ref *field_ref;
256 struct load_op *op;
257 struct lttng_ctx_field *ctx_field;
258 int idx;
259
260 dbg_printk("Apply context reloc: %u %s\n", reloc_offset, context_name);
261
262 /* Get context index */
263 idx = lttng_get_context_index(lttng_static_ctx, context_name);
264 if (idx < 0)
265 return -ENOENT;
266
267 /* Check if idx is too large for 16-bit offset */
f127e61e 268 if (idx > LTTNG_KERNEL_FILTER_BYTECODE_MAX_LEN - 1)
07dfc1d0
MD
269 return -EINVAL;
270
271 /* Get context return type */
272 ctx_field = &lttng_static_ctx->fields[idx];
273 op = (struct load_op *) &runtime->data[reloc_offset];
274 field_ref = (struct field_ref *) op->data;
275 switch (ctx_field->event_field.type.atype) {
276 case atype_integer:
277 case atype_enum:
278 op->op = FILTER_OP_GET_CONTEXT_REF_S64;
279 break;
280 /* Sequence and array supported as string */
281 case atype_string:
282 case atype_array:
283 case atype_sequence:
f127e61e 284 BUG_ON(ctx_field->event_field.user);
07dfc1d0
MD
285 op->op = FILTER_OP_GET_CONTEXT_REF_STRING;
286 break;
f513b2bf
MD
287 case atype_struct: /* Unsupported. */
288 case atype_array_compound: /* Unsupported. */
289 case atype_sequence_compound: /* Unsupported. */
65c85aa6 290 case atype_variant: /* Unsupported. */
07dfc1d0
MD
291 default:
292 return -EINVAL;
293 }
294 /* set offset to context index within channel contexts */
295 field_ref->offset = (uint16_t) idx;
296 return 0;
297}
298
299static
300int apply_reloc(struct lttng_event *event,
301 struct bytecode_runtime *runtime,
302 uint32_t runtime_len,
303 uint32_t reloc_offset,
304 const char *name)
305{
306 struct load_op *op;
307
308 dbg_printk("Apply reloc: %u %s\n", reloc_offset, name);
309
310 /* Ensure that the reloc is within the code */
311 if (runtime_len - reloc_offset < sizeof(uint16_t))
312 return -EINVAL;
313
314 op = (struct load_op *) &runtime->data[reloc_offset];
315 switch (op->op) {
316 case FILTER_OP_LOAD_FIELD_REF:
317 return apply_field_reloc(event, runtime, runtime_len,
318 reloc_offset, name);
319 case FILTER_OP_GET_CONTEXT_REF:
320 return apply_context_reloc(event, runtime, runtime_len,
321 reloc_offset, name);
322 default:
323 printk(KERN_WARNING "Unknown reloc op type %u\n", op->op);
324 return -EINVAL;
325 }
326 return 0;
327}
328
329static
330int bytecode_is_linked(struct lttng_filter_bytecode_node *filter_bytecode,
331 struct lttng_event *event)
332{
333 struct lttng_bytecode_runtime *bc_runtime;
334
335 list_for_each_entry(bc_runtime,
336 &event->bytecode_runtime_head, node) {
337 if (bc_runtime->bc == filter_bytecode)
338 return 1;
339 }
340 return 0;
341}
342
343/*
344 * Take a bytecode with reloc table and link it to an event to create a
345 * bytecode runtime.
346 */
347static
348int _lttng_filter_event_link_bytecode(struct lttng_event *event,
349 struct lttng_filter_bytecode_node *filter_bytecode,
350 struct list_head *insert_loc)
351{
352 int ret, offset, next_offset;
353 struct bytecode_runtime *runtime = NULL;
354 size_t runtime_alloc_len;
355
356 if (!filter_bytecode)
357 return 0;
358 /* Bytecode already linked */
359 if (bytecode_is_linked(filter_bytecode, event))
360 return 0;
361
362 dbg_printk("Linking...\n");
363
364 /* We don't need the reloc table in the runtime */
365 runtime_alloc_len = sizeof(*runtime) + filter_bytecode->bc.reloc_offset;
366 runtime = kzalloc(runtime_alloc_len, GFP_KERNEL);
367 if (!runtime) {
368 ret = -ENOMEM;
369 goto alloc_error;
370 }
371 runtime->p.bc = filter_bytecode;
372 runtime->len = filter_bytecode->bc.reloc_offset;
373 /* copy original bytecode */
374 memcpy(runtime->data, filter_bytecode->bc.data, runtime->len);
375 /*
376 * apply relocs. Those are a uint16_t (offset in bytecode)
377 * followed by a string (field name).
378 */
379 for (offset = filter_bytecode->bc.reloc_offset;
380 offset < filter_bytecode->bc.len;
381 offset = next_offset) {
382 uint16_t reloc_offset =
383 *(uint16_t *) &filter_bytecode->bc.data[offset];
384 const char *name =
385 (const char *) &filter_bytecode->bc.data[offset + sizeof(uint16_t)];
386
387 ret = apply_reloc(event, runtime, runtime->len, reloc_offset, name);
388 if (ret) {
389 goto link_error;
390 }
391 next_offset = offset + sizeof(uint16_t) + strlen(name) + 1;
392 }
393 /* Validate bytecode */
394 ret = lttng_filter_validate_bytecode(runtime);
395 if (ret) {
396 goto link_error;
397 }
398 /* Specialize bytecode */
399 ret = lttng_filter_specialize_bytecode(runtime);
400 if (ret) {
401 goto link_error;
402 }
403 runtime->p.filter = lttng_filter_interpret_bytecode;
404 runtime->p.link_failed = 0;
405 list_add_rcu(&runtime->p.node, insert_loc);
406 dbg_printk("Linking successful.\n");
407 return 0;
408
409link_error:
410 runtime->p.filter = lttng_filter_false;
411 runtime->p.link_failed = 1;
412 list_add_rcu(&runtime->p.node, insert_loc);
413alloc_error:
414 dbg_printk("Linking failed.\n");
415 return ret;
416}
417
418void lttng_filter_sync_state(struct lttng_bytecode_runtime *runtime)
419{
420 struct lttng_filter_bytecode_node *bc = runtime->bc;
421
422 if (!bc->enabler->enabled || runtime->link_failed)
423 runtime->filter = lttng_filter_false;
424 else
425 runtime->filter = lttng_filter_interpret_bytecode;
426}
427
428/*
429 * Link bytecode for all enablers referenced by an event.
430 */
431void lttng_enabler_event_link_bytecode(struct lttng_event *event,
432 struct lttng_enabler *enabler)
433{
434 struct lttng_filter_bytecode_node *bc;
435 struct lttng_bytecode_runtime *runtime;
436
437 /* Can only be called for events with desc attached */
438 WARN_ON_ONCE(!event->desc);
439
440 /* Link each bytecode. */
441 list_for_each_entry(bc, &enabler->filter_bytecode_head, node) {
442 int found = 0, ret;
443 struct list_head *insert_loc;
444
445 list_for_each_entry(runtime,
446 &event->bytecode_runtime_head, node) {
447 if (runtime->bc == bc) {
448 found = 1;
449 break;
450 }
451 }
452 /* Skip bytecode already linked */
453 if (found)
454 continue;
455
456 /*
457 * Insert at specified priority (seqnum) in increasing
458 * order.
459 */
460 list_for_each_entry_reverse(runtime,
461 &event->bytecode_runtime_head, node) {
462 if (runtime->bc->bc.seqnum < bc->bc.seqnum) {
463 /* insert here */
464 insert_loc = &runtime->node;
465 goto add_within;
466 }
467 }
468 /* Add to head to list */
469 insert_loc = &event->bytecode_runtime_head;
470 add_within:
471 dbg_printk("linking bytecode\n");
472 ret = _lttng_filter_event_link_bytecode(event, bc,
473 insert_loc);
474 if (ret) {
475 dbg_printk("[lttng filter] warning: cannot link event bytecode\n");
476 }
477 }
478}
479
480/*
481 * We own the filter_bytecode if we return success.
482 */
483int lttng_filter_enabler_attach_bytecode(struct lttng_enabler *enabler,
484 struct lttng_filter_bytecode_node *filter_bytecode)
485{
486 list_add(&filter_bytecode->node, &enabler->filter_bytecode_head);
487 return 0;
488}
489
490void lttng_free_enabler_filter_bytecode(struct lttng_enabler *enabler)
491{
492 struct lttng_filter_bytecode_node *filter_bytecode, *tmp;
493
494 list_for_each_entry_safe(filter_bytecode, tmp,
495 &enabler->filter_bytecode_head, node) {
496 kfree(filter_bytecode);
497 }
498}
499
500void lttng_free_event_filter_runtime(struct lttng_event *event)
501{
502 struct bytecode_runtime *runtime, *tmp;
503
504 list_for_each_entry_safe(runtime, tmp,
505 &event->bytecode_runtime_head, p.node) {
506 kfree(runtime);
507 }
508}
This page took 0.044658 seconds and 4 git commands to generate.