[v2,tracing,2/6] tracing: support IPv4 address filter predicate

Message ID 1682696089-27937-3-git-send-email-alan.maguire@oracle.com
State New
Headers
Series tracing: additional filter predicates |

Commit Message

Alan Maguire April 28, 2023, 3:34 p.m. UTC
  Support '==' and '!=' predicates for IPv4 address format;
for example

 cd /sys/kernel/debug/tracing/events/tcp/tcp_receive_reset
 echo "saddr == 127.0.0.1" > filter

Signed-off-by: Alan Maguire <alan.maguire@oracle.com>
---
 kernel/trace/trace_events_filter.c | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
  

Patch

diff --git a/kernel/trace/trace_events_filter.c b/kernel/trace/trace_events_filter.c
index 64f1dfb72cb5..d8e08d3c3594 100644
--- a/kernel/trace/trace_events_filter.c
+++ b/kernel/trace/trace_events_filter.c
@@ -1384,6 +1384,7 @@  static int parse_pred(const char *str, void *data,
 	unsigned long size;
 	unsigned long ip;
 	char num_buf[24];	/* Big enough to hold an address */
+	char scratch[4];	/* Big enough to hold an IPv4 address */
 	u8 *pred_val;
 	char *field_name;
 	char *name;
@@ -1646,6 +1647,24 @@  static int parse_pred(const char *str, void *data,
 		/* go past the last quote */
 		i++;
 
+	} else if (field->size == 4 &&
+		   sscanf(&str[i], "%hhd.%hhd.%hhd.%hhd",
+			  /* assume address in network byte order */
+			  &scratch[0], &scratch[1], &scratch[2], &scratch[3]) == 4) {
+		/* For IPv4 addresses, only '==' or '!=' are supported. */
+		if (pred->op != OP_EQ && pred->op != OP_NE) {
+			parse_error(pe, FILT_ERR_ILLEGAL_FIELD_OP, pos + i);
+			goto err_free;
+		}
+		while (isdigit(str[i]) || str[i] == '.')
+			i++;
+		pred_val = kzalloc(field->size, GFP_KERNEL);
+		memcpy(pred_val, scratch, field->size);
+		pred->val = (u64)pred_val;
+		pred->fn_num = FILTER_PRED_FN_MEMCMP;
+		if (pred->op == OP_NE)
+			pred->not = 1;
+
 	} else if (str[i] == '0' && tolower(str[i + 1]) == 'x' &&
 		   field->size > 8) {
 		/* For sizes > 8 bytes, we store hex bytes for comparison;