Skip to contentSkip to conversation

textsearch infrastructure et al v2

RFC In review Thomas Graf 4 participants 18 messages May 27, 2005 – May 31, 2005

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

Dave, I'm going through another RFC round to hopefully get some comments on the notes below.

I'm not yet satistifed with the core infrastructure wrt to non-linear data, especially for complex cases such as non linear skbs. The current way can be easly described as:

search_occurrance()
search-algorithm()
while get-next-segment()
process-segment()

So basically we save the state of the segment fetching instead of saving the state of the search algorithm which would be way too complex for things like regular expression string matching.

In the case of non linear skbs this would lead to:

1) start of search algorithm
2) first call to get_text(), return skb->data
3) processing of the text segment, return if match found
4) second call to get_text(), map and return first fragment
5) processing ...
6) third call to get_text(), unmap first fragment, map and return second fragment
7) processing ...
n) etc.

Assuming that the most common case is to have no fragments at all or if we only process a very limited number of fragments (due to restricted search area) this should be quite fast. However, the complexity of get_text() for the skb case including the map/unmap of the fragment interrupts the search flow quite a bit which hurts performance.

In order to address this issue I'm having second thoughts about whether to allow the textsearch user to provide an array of text segments including their size, i.e. the skb fragments could be maped in prior and the algorithm could scan through it like nothing. ;-> However, this has one essential drawback, we might map fragments which we'll never use so this can be slower in the end.

Any other ideas around?

#1 lore raw

PATCH 1/5 textsearch infrastructure

Before a textsearch can be performed, a configuration must be created by calling textsearch_prepare() specyfing the searching algorithm and the pattern to match. The returned configuration may then be used for an arbitary amount of times and even in parallel as long as you provide a separate struct ts_state variable for every instance.

The actual search is performed by either calling textsearch_find_- continuous() for linear text or by providing an own get_text() implementation and calling textsearch_find(). Both functions will returns the position of the first matching occurrence of the patern. Subsequent matches may be retrived via textsearch_next() irrespective of the linearity of the text.

Once you're done using a configuration you must give back the allocted resources by calling textsearch_destroy().

---
commit bf7ae763f13d767bd039703b3ab4f5954561df39
tree ab065819ea6e966aa3db4f1c5935c421dd689d2e
parent fde09d4989b6c7b5183ffb3ec2076ff53dd6fa78
author Thomas Graf <[email protected]> Fri, 27 May 2005 20:18:48 +0200 committer Thomas Graf <[email protected]> Fri, 27 May 2005 20:18:48 +0200

 include/linux/textsearch.h |  196 ++++++++++++++++++++++++++++++++++++
 lib/Makefile               |    2 
 lib/textsearch.c           |  241 +++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 438 insertions(+), 1 deletion(-)
include/linux/textsearch.h (mode:100644) renamed from dev/null (tree:b032d0d440d93aac252e656bd41df32ff5461e3a) +196 -0
Index: include/linux/textsearch.h
===================================================================
@@ -0,0 +1,196 @@
+#ifndef __LINUX_TEXTSEARCH_H
+#define __LINUX_TEXTSEARCH_H
+
+#ifdef __KERNEL__
+
+#include <linux/types.h>
+#include <linux/list.h>
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/err.h>
+
+struct ts_config;
+
+/**
+ * struct ts_state - textsearch state
+ * @offset: offset for next match
+ * @args: for persistant variables of get_text()
+ */
+struct ts_state
+{
+	int			offset;
+	long			args[6];
+};
+
+/**
+ * struct ts_ops - textsearch operations
+ * @name: name of search algorithm
+ * @init: called upon initialization to prepare a search
+ * @find: does the actual matching on a prepared configuration
+ * @destroy: called upon destroyage of a configuration
+ * @owner: reference to algorithm module if existent
+ * @list: operations list we are on
+ */
+struct ts_ops
+{
+	const char		*name;
+	struct ts_config *	(*init)(const unsigned char *, size_t, int);
+	int			(*find)(struct ts_config *,
+					struct ts_state *);
+	void			(*destroy)(struct ts_config *);
+	unsigned char *		(*get_pattern)(struct ts_config *);
+	unsigned int		(*get_pattern_len)(struct ts_config *);
+	struct module		*owner;
+	struct list_head	list;
+};
+
+/**
+ * struct ts_config - textsearch configuration
+ * @ops: textsearch operations
+ * @get_text: callback to fetch text to search in
+ * @len: length of pattern
+ */
+struct ts_config
+{
+	struct ts_ops		*ops;
+
+	/**
+	 * get_text - return next chunk of text
+	 * @offset: Number of bytes consumed by the matcher
+	 * @dst: destination buffer
+	 * @conf: search configuration
+	 * @state: search state
+	 *
+	 * Gets called repeatedly until 0 is returned.Must assign a pointer
+	 * to the start of the next chunk of text to *dst and return the
+	 * length of the chunk or 0 if at the end. offset==0 indicates
+	 * a new search. May store/read persistant values in state->args[].
+	 */
+	int			(*get_text)(int offset, unsigned char **dst,
+					    struct ts_config *conf,
+					    struct ts_state *state);
+
+	/**
+	 * finish - called when the matching has been completed successful
+	 *          or not.
+	 * @conf: search configuration
+	 * @state: search state
+	 */
+	void			(*finish)(struct ts_config *conf,
+					  struct ts_state *state);
+};
+
+/**
+ * textsearch_next - continue search for a pattern in a text
+ * @conf: search configuration
+ * @state: search state
+ *
+ * Continues a search looking for more occurrences of the pattern.
+ * You must call textsearch_find() to search the first occurrence
+ * in order to reset the state.
+ *
+ * Returns the position of next occurance of the pattern or a
+ *         negative number if no match was found.
+ */ 
+static inline int textsearch_next(struct ts_config *conf,
+				  struct ts_state *state)
+{
+	int ret = conf->ops->find(conf, state);
+
+	if (conf->finish)
+		conf->finish(conf, state);
+
+	return ret;
+}
+
+/**
+ * textsearch_find - search for a pattern in a text
+ * @conf: search configuration
+ * @state: search state
+ *
+ * Peforms the actual search on the prepared configuration.
+ *
+ * Returns the position of first occurrence of the pattern or a
+ *         negative number if no match was found.
+ */ 
+static inline int textsearch_find(struct ts_config *conf,
+				  struct ts_state *state)
+{
+	state->offset = 0;
+	return textsearch_next(conf, state);
+}
+
+/**
+ * textsearch_destroy - destroy a textsearch configuration
+ * @conf: search configuration
+ *
+ * Releases all references of the configuration and frees
+ * up the memory.
+ */
+static inline void textsearch_destroy(struct ts_config *conf)
+{
+	if (conf->ops) {
+		if (conf->ops->destroy)
+			conf->ops->destroy(conf);
+		module_put(conf->ops->owner);
+	}
+
+	kfree(conf);
+}
+
+/**
+ * textsearch_get_pattern - return pattern of a textsearch configuration
+ * @conf: textsearch configuration
+ */
+static inline unsigned char *textsearch_get_pattern(struct ts_config *conf)
+{
+	return conf->ops->get_pattern(conf);
+}
+
+/**
+ * textsearch_get_pattern_len - return pattern length of a textsearch configuration
+ * @conf: textsearch configuration
+ */
+static inline unsigned int textsearch_get_pattern_len(struct ts_config *conf)
+{
+	return conf->ops->get_pattern_len(conf);
+}
+
+/**
+ * alloc_ts_config - allocate a textsearch configuration
+ * @payload: size of additional module specific data required
+ * @gfp_mask: allocation mask
+ *
+ * Returns a empty textsearch configuration or a ERR_PTR().
+ */
+static inline struct ts_config *alloc_ts_config(size_t payload, int gfp_mask)
+{
+	struct ts_config *conf;
+
+	conf = kmalloc(sizeof(*conf) + payload, gfp_mask);
+	if (conf == NULL)
+		return ERR_PTR(-ENOMEM);
+
+	memset(conf, 0, sizeof(*conf) + payload);
+	return conf;
+}
+
+/**
+ * ts_config_priv - returns private part of a textsearch configuration
+ * @conf: textsearch configuration
+ */
+static inline void *ts_config_priv(struct ts_config *conf)
+{
+	return ((unsigned char *) conf + sizeof(struct ts_config));
+}
+
+extern int textsearch_register(struct ts_ops *);
+extern int textsearch_unregister(struct ts_ops *);
+extern struct ts_config *textsearch_prepare(const char *, const unsigned char *,
+					    size_t, int, int);
+extern int textsearch_find_continuous(struct ts_config *, struct ts_state *,
+				      const unsigned char *, size_t);
+
+#endif /* __KERNEL__ */
+
+#endif
lib/Makefile (mode:100644) +1 -1
Index: lib/Makefile
===================================================================
@@ -6,7 +6,7 @@
 	 bust_spinlocks.o rbtree.o radix-tree.o dump_stack.o \
 	 kobject.o kref.o idr.o div64.o int_sqrt.o \
 	 bitmap.o extable.o kobject_uevent.o prio_tree.o sha1.o \
-	 halfmd4.o
+	 halfmd4.o textsearch.o
 
 obj-y += sort.o parser.o
 
lib/textsearch.c (mode:100644) renamed from dev/null (tree:b032d0d440d93aac252e656bd41df32ff5461e3a) +241 -0

241 lines not on this page · raw message

#2 lore raw

PATCH 2/5 Knuth-Morris-Pratt string-matching algorithm

In reply to Thomas Graf

---
commit 5b70ca8eab4c7d7ef884582d9713cdbffa0f4cd4
tree 4d90ca82120da7b308b9a6bf11a1069473ca5d30
parent bf7ae763f13d767bd039703b3ab4f5954561df39
author Thomas Graf <[email protected]> Fri, 27 May 2005 23:44:02 +0200 committer Thomas Graf <[email protected]> Fri, 27 May 2005 23:44:02 +0200

 lib/Kconfig  |   13 +++++
 lib/Makefile |    2 
 lib/ts_kmp.c |  145 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 160 insertions(+)
lib/Kconfig (mode:100644) +13 -0
Index: lib/Kconfig
===================================================================
@@ -57,5 +57,18 @@
 config REED_SOLOMON_DEC16
 	boolean
 
+menu "Textsearch facility"
+
+config TEXTSEARCH_KMP
+	tristate "Knuth-Morris-Pratt"
+	help
+	  Say Y here if you want to be able to search text using the
+	  Knuth-Morris-Pratt textsearch algorithm.
+
+	  To compile this code as a module, choose M here: the
+	  module will be called ts_kmp.
+
+endmenu
+
 endmenu
 
lib/Makefile (mode:100644) +2 -0
Index: lib/Makefile
===================================================================
@@ -33,6 +33,8 @@
 obj-$(CONFIG_ZLIB_DEFLATE) += zlib_deflate/
 obj-$(CONFIG_REED_SOLOMON) += reed_solomon/
 
+obj-$(CONFIG_TEXTSEARCH_KMP) += ts_kmp.o
+
 hostprogs-y	:= gen_crc32table
 clean-files	:= crc32table.h
 
lib/ts_kmp.c (mode:100644) renamed from dev/null (tree:ab065819ea6e966aa3db4f1c5935c421dd689d2e) +145 -0

145 lines not on this page · raw message

#3 lore raw

PATCH 3/5 Naive regular expression string-matching algorithm

In reply to Thomas Graf

---
commit 81553cdea2a7ff762cb44aeced743d3a77efd2d7
tree 266c4ad512e5ae71bd7cd88c94bfb18fb2f761d6
parent 5b70ca8eab4c7d7ef884582d9713cdbffa0f4cd4
author Thomas Graf <[email protected]> Fri, 27 May 2005 23:44:24 +0200 committer Thomas Graf <[email protected]> Fri, 27 May 2005 23:44:24 +0200

 include/linux/textsearch_regexp.h |   47 ++++++
 lib/Kconfig                       |    9 +
 lib/Makefile                      |    1 
 lib/ts_regexp.c                   |  272 ++++++++++++++++++++++++++++++++++++++
 4 files changed, 329 insertions(+)
include/linux/textsearch_regexp.h (mode:100644) renamed from dev/null (tree:4d90ca82120da7b308b9a6bf11a1069473ca5d30) +47 -0
Index: include/linux/textsearch_regexp.h
===================================================================
@@ -0,0 +1,47 @@
+#ifndef __LINUX_TEXTSEARCH_REGEXP_H
+#define __LINUX_TEXTSEARCH_REGEXP_H
+
+#include <linux/types.h>
+
+enum {
+	TS_RE_SPECIFIC,		/* specific character */
+	TS_RE_WILDCARD,		/* any character */
+	TS_RE_DIGIT,		/* isdigit() */
+	TS_RE_XDIGIT,		/* isxdigit() */
+	TS_RE_PRINT,		/* isprint() */
+	TS_RE_ALPHA,		/* isalpha() */
+	TS_RE_ALNUM,		/* isalnum() */
+	TS_RE_ASCII,		/* isascii() */
+	TS_RE_CNTRL,		/* iscntrl() */
+	TS_RE_GRAPH,		/* isgraph() */
+	TS_RE_LOWER,		/* islower() */
+	TS_RE_UPPER,		/* isupper() */
+	TS_RE_PUNCT,		/* ispunct() */
+	TS_RE_SPACE,		/* isspace() */
+	__TS_RE_TYPE_MAX,
+};
+#define TS_RE_TYPE_MAX (__TS_RE_TYPE_MAX - 1)
+
+enum {
+	TS_RE_SINGLE,		/* 1 occurrence */
+	TS_RE_PERHAPS,		/* 1 or 0 occurrence */
+	TS_RE_ANY,		/* 0..n occurrences */
+	TS_RE_MULTI,		/* 1..n occurrences */
+	__TS_RE_RECUR_MAX,
+};
+#define TS_RE_RECUR_MAX (__TS_RE_RECUR_MAX - 1)
+
+/**
+ * struct ts_regexp_token - regular expression token
+ * @type: type of token
+ * @recur: number of recurrences
+ * @value: character value for TS_RE_SPECIFIC
+ */
+struct ts_regexp_token
+{
+	__u16		type;
+	__u8		recur;
+	__u8		value;
+};
+
+#endif
lib/Kconfig (mode:100644) +9 -0
Index: lib/Kconfig
===================================================================
@@ -68,6 +68,15 @@
 	  To compile this code as a module, choose M here: the
 	  module will be called ts_kmp.
 
+config TEXTSEARCH_REGEXP
+	tristate "Regular Expression"
+	help
+	  Say Y here if you want to be able to search text using a
+	  naive and limited regular expression textsearch algorithm.
+
+	  To compile this code as a module, choose M here: the
+	  module will be called ts_regexp.
+
 endmenu
 
 endmenu
lib/Makefile (mode:100644) +1 -0
Index: lib/Makefile
===================================================================
@@ -34,6 +34,7 @@
 obj-$(CONFIG_REED_SOLOMON) += reed_solomon/
 
 obj-$(CONFIG_TEXTSEARCH_KMP) += ts_kmp.o
+obj-$(CONFIG_TEXTSEARCH_REGEXP) += ts_regexp.o
 
 hostprogs-y	:= gen_crc32table
 clean-files	:= crc32table.h
lib/ts_regexp.c (mode:100644) renamed from dev/null (tree:4d90ca82120da7b308b9a6bf11a1069473ca5d30) +272 -0

272 lines not on this page · raw message

#4 lore raw

PATCH 4/5 Add skb_find_text() to search for a text pattern in skbs

In reply to Thomas Graf

Adds the function skb_find_text() to search for text patterns in both, linear and non-linear skbs.

---
commit 4fdcfba0bf99efed709bc8ebaeda0d5ed7e9cc67
tree 13ce1cd98014267f2a353b96e813c1e70bbe9c3b
parent 81553cdea2a7ff762cb44aeced743d3a77efd2d7
author Thomas Graf <[email protected]> Fri, 27 May 2005 23:44:50 +0200 committer Thomas Graf <[email protected]> Fri, 27 May 2005 23:44:50 +0200

 include/linux/skbuff.h |    4 ++
 net/core/skbuff.c      |   83 +++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 87 insertions(+)
include/linux/skbuff.h (mode:100644) +4 -0
Index: include/linux/skbuff.h
===================================================================
@@ -28,6 +28,7 @@
 #include <linux/poll.h>
 #include <linux/net.h>
 #include <net/checksum.h>
+#include <linux/textsearch.h>
 
 #define HAVE_ALLOC_SKB		/* For the drivers to know */
 #define HAVE_ALIGNABLE_SKB	/* Ditto 8)		   */
@@ -1187,6 +1188,9 @@
 extern void	       skb_split(struct sk_buff *skb,
 				 struct sk_buff *skb1, const u32 len);
 
+extern int skb_find_text(struct sk_buff *skb, int from, int to,
+			 struct ts_config *config, struct ts_state *state);
+
 static inline void *skb_header_pointer(const struct sk_buff *skb, int offset,
 				       int len, void *buffer)
 {
net/core/skbuff.c (mode:100644) +83 -0

92 lines not on this page · raw message

#7 lore raw

PATCH 5/5 textsearch ematch

In reply to Thomas Graf

---
commit 5d28d54b48451e7833295be68b7e4b6fcc2b5375
tree cc252d153b4b21f347aab71bbfe04c0bf2a3a8a2
parent 4fdcfba0bf99efed709bc8ebaeda0d5ed7e9cc67
author Thomas Graf <[email protected]> Fri, 27 May 2005 23:45:15 +0200 committer Thomas Graf <[email protected]> Fri, 27 May 2005 23:45:15 +0200

 include/linux/pkt_cls.h              |    1 
 include/linux/tc_ematch/tc_em_text.h |   19 ++++
 net/sched/Kconfig                    |   11 ++
 net/sched/Makefile                   |    1 
 net/sched/em_text.c                  |  142 +++++++++++++++++++++++++++++++++++
 5 files changed, 174 insertions(+)
include/linux/pkt_cls.h (mode:100644) +1 -0
Index: include/linux/pkt_cls.h
===================================================================
@@ -408,6 +408,7 @@
 	TCF_EM_NBYTE,
 	TCF_EM_U32,
 	TCF_EM_META,
+	TCF_EM_TEXT,
 	__TCF_EM_MAX
 };
 
include/linux/tc_ematch/tc_em_text.h (mode:100644) renamed from dev/null (tree:13ce1cd98014267f2a353b96e813c1e70bbe9c3b) +19 -0
Index: include/linux/tc_ematch/tc_em_text.h
===================================================================
@@ -0,0 +1,19 @@
+#ifndef __LINUX_TC_EM_TEXT_H
+#define __LINUX_TC_EM_TEXT_H
+
+#include <linux/pkt_cls.h>
+
+#define TC_EM_TEXT_ALGOSIZ	16
+
+struct tcf_em_text
+{
+	char		algo[TC_EM_TEXT_ALGOSIZ];
+	__u16		from_offset;
+	__u16		to_offset;
+	__u16		pattern_len;
+	__u8		from_layer:4;
+	__u8		to_layer:4;
+	__u8		pad;
+};
+
+#endif
net/sched/Kconfig (mode:100644) +11 -0
Index: net/sched/Kconfig
===================================================================
@@ -449,6 +449,17 @@
 	  To compile this code as a module, choose M here: the
 	  module will be called em_meta.
 
+config NET_EMATCH_TEXT
+	tristate "Textsearch"
+	depends on NET_EMATCH
+	---help---
+	  Say Y here if you want to be ablt to classify packets based on
+	  textsearch comparisons. Please select the appropriate textsearch
+	  algorithms in the Library section.
+
+	  To compile this code as a module, choose M here: the
+	  module will be called em_text.
+
 config NET_CLS_ACT
 	bool "Packet ACTION"
 	depends on EXPERIMENTAL && NET_CLS && NET_QOS
net/sched/Makefile (mode:100644) +1 -0
Index: net/sched/Makefile
===================================================================
@@ -40,3 +40,4 @@
 obj-$(CONFIG_NET_EMATCH_NBYTE)	+= em_nbyte.o
 obj-$(CONFIG_NET_EMATCH_U32)	+= em_u32.o
 obj-$(CONFIG_NET_EMATCH_META)	+= em_meta.o
+obj-$(CONFIG_NET_EMATCH_TEXT)	+= em_text.o
net/sched/em_text.c (mode:100644) renamed from dev/null (tree:13ce1cd98014267f2a353b96e813c1e70bbe9c3b) +142 -0

142 lines not on this page · raw message

#5 lore raw

Re: [PATCH 4/5] [NET] Add skb_find_text() to search for a text pattern in skbs

In reply to Thomas Graf
Quoted 41 lines · Thomas Graf
net/core/skbuff.c (mode:100644) +41 -0
+static int get_skb_text(int offset, unsigned char **text,
+			struct ts_config *conf, struct ts_state *state)
+{
+	/* args[0]: lower limit
+	 * args[1]: upper limit
+	 * args[2]: skb
+	 * args[3]: fragment index
+	 * args[4]: current fragment data buffer
+	 * args[5]: octets consumed up to previous fragment */
+	int from = state->args[0], to = state->args[1];
+	struct sk_buff *skb = (struct sk_buff *) state->args[2];
+	int limit = min_t(int, skb_headlen(skb), to);
+	int real_offset = offset + from;
+	skb_frag_t *f;
+
+	if (!skb_is_nonlinear(skb)) {
+		if (real_offset < limit) {
+linear:
+			*text = skb->data + real_offset;
+			return limit - real_offset;
+		}
+
+		return 0;
+	}
+
+	if (real_offset < limit)
+		goto linear;
+
+next_fragment:
+	f = &skb_shinfo(skb)->frags[state->args[3]];
+	limit = min_t(int, f->size + state->args[5], to);
+
+	if (!state->args[4])
+		state->args[4] = (long) kmap_skb_frag(f);
+
+	if (real_offset < limit) {
+		*text = (unsigned char *) state->args[4] + f->page_offset +
+			(real_offset - (int) state->args[5]);
+		return limit - real_offset;
+	}
+

net/core/skbuff.c (mode:100644) line 1559

Quoted 10 lines
net/core/skbuff.c (mode:100644) +10 -0
+	kunmap_skb_frag((void *) state->args[4]);
+	state->args[3]++;
+	state->args[4] = (long) NULL;
+	state->args[5] += f->size;
+
+	if (state->args[3] >= skb_shinfo(skb)->nr_frags)
+		return 0;
+
+	goto next_fragment;
+}

Still miss something here. You aren't looking for matches in skbs inserted in skb_shinfo(skb)->frag_list. Have a look at rusty's skb_iter functions. Since he cooked those for this purpose (string matching), why don't we use them instead of yours ?

--
Pablo

#6 lore raw

Re: [PATCH 4/5] [NET] Add skb_find_text() to search for a text pattern in skbs

* Pablo Neira <[email protected]> 2005-05-28 05:11

Quoted 4 lines

Still miss something here. You aren't looking for matches in skbs inserted in skb_shinfo(skb)->frag_list. Have a look at rusty's skb_iter functions. Since he cooked those for this purpose (string matching), why don't we use them instead of yours ?

Absolutely, I'm planning to use rusty's skb_iter instead. I postponed this to when the core infrastructure is stable but thanks a lot for this comment.

#8 lore raw
In reply to Thomas Graf
Quoted 12 lines · Thomas Graf

Dave, I'm going through another RFC round to hopefully get some comments on the notes below.

I'm not yet satistifed with the core infrastructure wrt to non-linear data, especially for complex cases such as non linear skbs. The current way can be easly described as:

search_occurrance()
search-algorithm()
while get-next-segment()
process-segment()

I dont have opinions on this since you and Pablo seem to agree on it. What I remember is that libssearch (or whatever that thing Harald was looking at) did it differently (callback invoked and it return a code which said to continue or not).
Also the design should (I think you do already, just double checking) - should be wary of optimizing for a specific algorithmn; i see you have KMP but not Boyer-Moore for example and i am not sure what the repurcassions of above approach are etc etc.

Quoted 5 lines

So basically we save the state of the segment fetching instead of saving the state of the search algorithm which would be way too complex for things like regular expression string matching.

Can you explain this a little more. Didnt quiet understand. If you are going across skbs, dont you need to save state?

Quoted 2 lines

In the case of non linear skbs this would lead to:

I think the best approach would be to first linearize then search. On the egress side this would mean annoying things like no TSO or what was posted yesterday USO etc. Same thing with ingress, but i suspect other than the netiron NIC we probably wont be seeing much of non-linear skbs on the ingress.
On ingress as well you are also better off to assemble fragments first on that side before doing text searches.
Perhaps implementing an action like the one used by openbsd to "normalize" packets before a string match.
i.e
classifier (pkt header) --> normalize --> classifier(string match)-> ?

Infact one could argue that if you are to scan a virus you may need to assemble more than one skb on ingress (essentially a message).

The only other comment i have is on the patch you called naive regexp; I think you should probably call it something else instead of regexp since you invented it.

cheers,
jamal

#9 lore raw

* jamal <[email protected]> 2005-05-28 07:59

Quoted 4 lines

I dont have opinions on this since you and Pablo seem to agree on it. What I remember is that libssearch (or whatever that thing Harald was looking at) did it differently (callback invoked and it return a code which said to continue or not).

Same for my infrastructure with the difference that libqsearch uses a single input buffer so no chance for non-linear data.

Quoted 4 lines

Also the design should (I think you do already, just double checking) - should be wary of optimizing for a specific algorithmn; i see you have KMP but not Boyer-Moore for example and i am not sure what the repurcassions of above approach are etc etc.

This is a weakness of the current implementation, it could be addresses via the other method that I proposed. The current patch doesn't allow for random access over fragment borders which means that Boyer-Moore would require a temporary buffer with a size of the pattern length in order to do random access over multiple fragments. However, I haven't seen any infrastructure yet that can handle non-linear data with bm wihout a complete prefetch in the beginning though. You could still implement a bm by either using a naive search at the borders or simply define the limitation that you can only look at the first fragment so you'd have to linearize.

Quoted 8 lines
Quoted 5 lines

So basically we save the state of the segment fetching instead of saving the state of the search algorithm which would be way too complex for things like regular expression string matching.

Can you explain this a little more. Didnt quiet understand. If you are going across skbs, dont you need to save state?

Most people look at it from the angle of iterate over text segments and apply a text search algorithm. I inverted this and said, start the text search algorithm and iterate over all text segments inside the algorithm. This makes many things a lot easier because we only have to save the state of the iterator. This brings up another limitation though, we cannot stop a search in progress and continue later on.

Quoted 1 line

I think the best approach would be to first linearize then search.

This is what I'm trying to avoid. ;->

Quoted 2 lines

On ingress as well you are also better off to assemble fragments first on that side before doing text searches.

We can still do this optionally, just because we support it doesn't mean we have to use it.

Quoted 7 lines

Perhaps implementing an action like the one used by openbsd to "normalize" packets before a string match.
i.e
classifier (pkt header) --> normalize --> classifier(string match)-> ?

Infact one could argue that if you are to scan a virus you may need to assemble more than one skb on ingress (essentially a message).

http://oss.sgi.com/archives/netdev/2005-05/msg00298.html

See the section after <<coffee break>>

Quoted 3 lines

The only other comment i have is on the patch you called naive regexp; I think you should probably call it something else instead of regexp since you invented it.

I was never good at names, any suggestions?

#10 lore raw
Quoted 3 lines · Thomas Graf

* jamal <[email protected]> 2005-05-28 07:59

Quoted 1 line

I dont have opinions on this since you and Pablo seem to agree on it.

to be frank, i'm still willing to propose some changes to Thomas, I'll do soon since he's pulled my ear with this second rfc request ;).

Quoted 7 lines
Quoted 3 lines

What I remember is that libssearch (or whatever that thing Harald was looking at) did it differently (callback invoked and it return a code which said to continue or not).

Same for my infrastructure with the difference that libqsearch uses a single input buffer so no chance for non-linear data.

hm, i don't understand quite well, i bet that libqsearch was already fragment-aware. Anyway the main difference is that libqsearch wasn't designed to be used in user space so, for example, it needed a complete rework to reduce dynamic memory allocations.

Quoted 6 lines
Quoted 4 lines

Also the design should (I think you do already, just double checking) - should be wary of optimizing for a specific algorithmn; i see you have KMP but not Boyer-Moore for example and i am not sure what the repurcassions of above approach are etc etc.

Quoted 10 lines

This is a weakness of the current implementation, it could be addresses via the other method that I proposed. The current patch doesn't allow for random access over fragment borders which means that Boyer-Moore would require a temporary buffer with a size of the pattern length in order to do random access over multiple fragments. However, I haven't seen any infrastructure yet that can handle non-linear data with bm wihout a complete prefetch in the beginning though. You could still implement a bm by either using a naive search at the borders or simply define the limitation that you can only look at the first fragment so you'd have to linearize.

For small pattern and long packets, such pattern searching on the fragment borders doesn't really hurt the performance. Anyway the matter of having several algorithms will let users choose that one that suits better their needs.

boyer-moore (BM) and other variants are definitely a must to have. I'm still reading some papers about string matching and practical applications (p2p traffic recognition based on string matching, ids, etc etc) and the most interesting practical results come always from the use of BM friends.

Quoted 4 lines
Quoted 1 line

I think the best approach would be to first linearize then search.

This is what I'm trying to avoid. ;->

sure, agree with Thomas.

Netfilter used to follow this approach in early 2.6 kernels and Patrick McHardy demostrated with some oprofile stuff that skb_copy_bits decreased performance.

I'm not familiar with those problems jamal has mentioned though, could they really affect the string matching infrastructure in some way? I truly prefer avoiding linearizing skb's.

Quoted 6 lines
Quoted 3 lines

The only other comment i have is on the patch you called naive regexp; I think you should probably call it something else instead of regexp since you invented it.

I was never good at names, any suggestions?

nor me, i'd propose flamenco-dancing-regexp ;->.

--
Pablo

#11 lore raw
Quoted 5 lines · Pablo Neira
Quoted 2 lines

Same for my infrastructure with the difference that libqsearch uses a single input buffer so no chance for non-linear data.

hm, i don't understand quite well, i bet that libqsearch was already fragment-aware. Anyway the main difference is that libqsearch wasn't designed to be used in user space so, for example, it needed a complete rework to reduce dynamic memory allocations.

sorry, i meant '/s/wasn't/was/g'. libqsearch was designed to be used my snort.

--
Pablo

#12 lore raw
In reply to Pablo Neira
Quoted 5 lines · Pablo Neira
Quoted 2 lines

Same for my infrastructure with the difference that libqsearch uses a single input buffer so no chance for non-linear data.

hm, i don't understand quite well, i bet that libqsearch was already fragment-aware. Anyway the main difference is that libqsearch wasn't designed to be used in user space so, for example, it needed a complete rework to reduce dynamic memory allocations.

sorry, i meant '/s/wasn't/was/g'. libqsearch was designed to be used by snort.

--
Pablo

#13 lore raw
In reply to Pablo Neira

* Pablo Neira <[email protected]> 2005-05-28 14:56

Quoted 2 lines

hm, i don't understand quite well, i bet that libqsearch was already fragment-aware.

I'll rephrase, libqsearch, according to my understanding, is not able to do optimized scanning over borders, i.e. it uses the same naive approach as your proposal. This isn't necessarly a weakness, it heavly depends on the length of the pattern and the amount of fragments.

One of the major differences between libqsearch and the textsearch I propose is that libqsearch implements "jokers", a subset of what ts_regexp.c does. This heavly complicates their reference implementation of bm. Again, this isn't necessarly a weakness, since the framework doesn't require the algorithm to actually implement these.

I cannot really tell which of the proposals we have right now is the best/fastest/cleanest/fanciest/whatever. For that reason I decided to transfer as much responsibility as possibly away from the core into the actual algorithms, respectively made it configureable via callback functions. It is still possible to write a ts_(kmp|bm)_state.c which saves the state of the scanning progress but other than in libqsearch this is not a requirement.

Quoted 2 lines

For small pattern and long packets, such pattern searching on the fragment borders doesn't really hurt the performance.

Exactly.

Quoted 5 lines

boyer-moore (BM) and other variants are definitely a must to have. I'm still reading some papers about string matching and practical applications (p2p traffic recognition based on string matching, ids, etc etc) and the most interesting practical results come always from the use of BM friends.

Absolultely, I implemented KMP because it doesn't require random access to the text and thus serves well as a reference implementation. I'll be glad to see your BM ported.

Quoted 3 lines

I'm not familiar with those problems jamal has mentioned though, could they really affect the string matching infrastructure in some way? I truly prefer avoiding linearizing skb's.

I think we have to differ between non-linear skbs which we should have without linearization and the scanning through real fragments.

In order to make some progress we have to answer these questions:

* Do we want to be able to search on non-linear data in general? I say: yes

* Do we want to be able to search on non-linear data which is not completely available and/or present at the time the search starts? In other words, do we want a search to be interruptable to continue later on. e.g. search over multiple skbs without queueing them up.
I say: no

* Do we want to provide random access to the text to be searched? If so, optionally or as a requirement? This merely has impact on a) BM could be efficiently implemented w/o naive scanning around the borders and b) requires the text segments to be completely maped/prepare at the risk they'll never be used. [0] I say: probably no

[0] This impact can be limited by having the user specify strict searching area limits.

maintainer
#15 lore raw
In reply to Thomas Graf

From: Thomas Graf <[email protected]>
Date: Sat, 28 May 2005 00:47:25 +0200

Quoted 1 line

Any other ideas around?

You could just fetch "windows" of data.

You can define this window to be 32 bytes, or whatever.

Then you use skb_copy_data() into this 32 byte window scratch pad the text searcher uses, and you're done. It starts looking like an MPEG stream parser :-)

maintainer
#14 lore raw
In reply to Pablo Neira

From: Pablo Neira <[email protected]>
Date: Sat, 28 May 2005 14:56:37 +0200

Quoted 3 lines

Netfilter used to follow this approach in early 2.6 kernels and Patrick McHardy demostrated with some oprofile stuff that skb_copy_bits decreased performance.

This case got converted into what skb_copy_bits() was probably meant to be, skb_header_pointer().

The idea is, if it's linear in the SKB already (headers almost certainly are) just pass back the pointer to it, else copy into the user provided temporary buffer and return a pointer to that.

The text search stuff could easily do the same thing, using a 32-byte or so sliding window to run the text search on.

#16 lore raw
In reply to David S. Miller

* David S. Miller <[email protected]> 2005-05-31 14:56

Quoted 8 lines

From: Thomas Graf <[email protected]>
Date: Sat, 28 May 2005 00:47:25 +0200

Quoted 1 line

Any other ideas around?

You could just fetch "windows" of data.

You can define this window to be 32 bytes, or whatever.

Heh, I had something like this in mind. Well, basically the current behaviour is not different except that the window is variable and uses the page data directly rather than copying.

Back on the static window subject, it would definitely be helpful for right-to-left scan algorithms such as boyer-moore. However, I think that the overhead due to the massive map/unmap and copying is bigger than the costs of naive searches around the fragment borders.

Pablo joined me on the subject, he's currently working on converting the fragmentation iteration to use Rusty's skb_iter code. we'll present new work with some numbers shortly.

maintainer
#17 lore raw

From: Thomas Graf <[email protected]>
Date: Wed, 1 Jun 2005 00:44:39 +0200

Quoted 4 lines

Pablo joined me on the subject, he's currently working on converting the fragmentation iteration to use Rusty's skb_iter code. we'll present new work with some numbers shortly.

Sounds good.