datapath-windows: Rename switch context's nameHashArray and vport's nameLink login...
[cascardo/ovs.git] / lib / classifier.h
index d0a408b..f75d242 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2009, 2010, 2011, 2012, 2013 Nicira, Inc.
+ * Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014 Nicira, Inc.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
 #include "cmap.h"
 #include "match.h"
 #include "meta-flow.h"
+#include "ovs-thread.h"
+#include "pvector.h"
 
 #ifdef __cplusplus
 extern "C" {
 #endif
 
 /* Classifier internal data structures. */
-struct cls_classifier;
 struct cls_subtable;
 struct cls_match;
 
+struct trie_node;
+typedef OVSRCU_TYPE(struct trie_node *) rcu_trie_ptr;
+
+/* Prefix trie for a 'field' */
+struct cls_trie {
+    const struct mf_field *field; /* Trie field, or NULL. */
+    rcu_trie_ptr root;            /* NULL if none. */
+};
+
 enum {
-    CLS_MAX_TRIES = 3    /* Maximum number of prefix trees per classifier. */
+    CLS_MAX_INDICES = 3,   /* Maximum number of lookup indices per subtable. */
+    CLS_MAX_TRIES = 3      /* Maximum number of prefix trees per classifier. */
 };
 
 /* A flow classifier. */
 struct classifier {
-    struct cls_classifier *cls;
+    struct ovs_mutex mutex;
+    int n_rules OVS_GUARDED;        /* Total number of rules. */
+    uint8_t n_flow_segments;
+    uint8_t flow_segments[CLS_MAX_INDICES]; /* Flow segment boundaries to use
+                                             * for staged lookup. */
+    struct cmap subtables_map;      /* Contains "struct cls_subtable"s.  */
+    struct pvector subtables;
+    struct cmap partitions;         /* Contains "struct cls_partition"s. */
+    struct cls_trie tries[CLS_MAX_TRIES]; /* Prefix tries. */
+    unsigned int n_tries;
 };
 
 /* A rule to be inserted to the classifier. */
@@ -275,9 +295,11 @@ void classifier_remove(struct classifier *, struct cls_rule *);
 struct cls_rule *classifier_lookup(const struct classifier *,
                                    const struct flow *,
                                    struct flow_wildcards *);
-void classifier_lookup_miniflow_batch(const struct classifier *cls,
+bool classifier_lookup_miniflow_batch(const struct classifier *cls,
                                       const struct miniflow **flows,
-                                      struct cls_rule **rules, size_t len);
+                                      struct cls_rule **rules,
+                                      const size_t cnt);
+enum { CLASSIFIER_MAX_BATCH = 256 };
 bool classifier_rule_overlaps(const struct classifier *,
                               const struct cls_rule *);
 
@@ -291,69 +313,45 @@ struct cls_rule *classifier_find_match_exactly(const struct classifier *,
 /* Iteration. */
 
 struct cls_cursor {
-    const struct cls_classifier *cls;
+    const struct classifier *cls;
     const struct cls_subtable *subtable;
     const struct cls_rule *target;
     struct cmap_cursor subtables;
     struct cmap_cursor rules;
+    struct cls_rule *rule;
     bool safe;
 };
 
-
 /* Iteration requires mutual exclusion of writers.  We do this by taking
  * a mutex for the duration of the iteration, except for the
  * 'SAFE' variant, where we release the mutex for the body of the loop. */
-struct cls_cursor cls_cursor_init(const struct classifier *cls,
-                                  const struct cls_rule *target,
-                                  void **pnode, const void *offset, bool safe);
-
-struct cls_rule *cls_cursor_next(struct cls_cursor *cursor,
-                                 const struct cls_rule *);
+struct cls_cursor cls_cursor_start(const struct classifier *cls,
+                                   const struct cls_rule *target,
+                                   bool safe);
 
-#define CLS_CURSOR_START(RULE, MEMBER, CLS, TARGET)                     \
-    cls_cursor_init(CLS, (TARGET), (void **)&(RULE),                    \
-                    OBJECT_CONTAINING(NULL, RULE, MEMBER), false)
-
-#define CLS_CURSOR_START_SAFE(RULE, MEMBER, CLS, TARGET)                \
-    cls_cursor_init(CLS, (TARGET), (void **)&(RULE),                    \
-                    OBJECT_CONTAINING(NULL, RULE, MEMBER), true)
-
-#define CLS_FOR_EACH(RULE, MEMBER, CLS)                                 \
-    for (struct cls_cursor cursor__ = CLS_CURSOR_START(RULE, MEMBER, CLS, \
-                                                       NULL);           \
-         RULE != OBJECT_CONTAINING(NULL, RULE, MEMBER);                 \
-         ASSIGN_CONTAINER(RULE, cls_cursor_next(&cursor__, &(RULE)->MEMBER), \
-                          MEMBER))
+void cls_cursor_advance(struct cls_cursor *);
 
+#define CLS_FOR_EACH(RULE, MEMBER, CLS) \
+    CLS_FOR_EACH_TARGET(RULE, MEMBER, CLS, NULL)
 #define CLS_FOR_EACH_TARGET(RULE, MEMBER, CLS, TARGET)                  \
-    for (struct cls_cursor cursor__ = CLS_CURSOR_START(RULE, MEMBER, CLS, \
-                                                       TARGET);         \
-         RULE != OBJECT_CONTAINING(NULL, RULE, MEMBER);                 \
-         ASSIGN_CONTAINER(RULE, cls_cursor_next(&cursor__, &(RULE)->MEMBER), \
-                          MEMBER))
-
-/* This form allows classifier_remove() to be called within the loop. */
-#define CLS_FOR_EACH_SAFE(RULE, NEXT, MEMBER, CLS)                      \
-    for (struct cls_cursor cursor__ = CLS_CURSOR_START_SAFE(RULE, MEMBER, \
-                                                            CLS, NULL); \
-         (RULE != OBJECT_CONTAINING(NULL, RULE, MEMBER)                 \
-          ? ASSIGN_CONTAINER(NEXT, cls_cursor_next(&cursor__,           \
-                                                   &(RULE)->MEMBER),    \
-                             MEMBER), true                              \
+    for (struct cls_cursor cursor__ = cls_cursor_start(CLS, TARGET, false); \
+         (cursor__.rule                                                 \
+          ? (INIT_CONTAINER(RULE, cursor__.rule, MEMBER),               \
+             true)                                                      \
           : false);                                                     \
-         (RULE) = (NEXT))
+         cls_cursor_advance(&cursor__))
 
-/* This form allows classifier_remove() to be called within the loop. */
-#define CLS_FOR_EACH_TARGET_SAFE(RULE, NEXT, MEMBER, CLS, TARGET)       \
-    for (struct cls_cursor cursor__ = CLS_CURSOR_START_SAFE(RULE, MEMBER, \
-                                                            CLS, TARGET); \
-         (RULE != OBJECT_CONTAINING(NULL, RULE, MEMBER)                 \
-          ? ASSIGN_CONTAINER(NEXT, cls_cursor_next(&cursor__,           \
-                                                   &(RULE)->MEMBER),    \
-                             MEMBER), true                              \
+/* These forms allows classifier_remove() to be called within the loop. */
+#define CLS_FOR_EACH_SAFE(RULE, MEMBER, CLS) \
+    CLS_FOR_EACH_TARGET_SAFE(RULE, MEMBER, CLS, NULL)
+#define CLS_FOR_EACH_TARGET_SAFE(RULE, MEMBER, CLS, TARGET)             \
+    for (struct cls_cursor cursor__ = cls_cursor_start(CLS, TARGET, true); \
+         (cursor__.rule                                                 \
+          ? (INIT_CONTAINER(RULE, cursor__.rule, MEMBER),               \
+             cls_cursor_advance(&cursor__),                             \
+             true)                                                      \
           : false);                                                     \
-         (RULE) = (NEXT))
-
+        )                                                               \
 
 #ifdef __cplusplus
 }