datapath-windows: Rename switch context's nameHashArray and vport's nameLink login...
[cascardo/ovs.git] / lib / classifier.h
index 326ca08..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.
  * The classifier may safely be accessed by many reader threads concurrently or
  * by a single writer. */
 
-#include "fat-rwlock.h"
+#include "cmap.h"
 #include "match.h"
 #include "meta-flow.h"
+#include "ovs-thread.h"
+#include "pvector.h"
 
 #ifdef __cplusplus
 extern "C" {
 #endif
 
-/* Needed only for the lock annotation in struct classifier. */
-extern struct ovs_mutex ofproto_mutex;
-
 /* Classifier internal data structures. */
-struct cls_classifier;
 struct cls_subtable;
-struct cls_partition;
 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 fat_rwlock rwlock OVS_ACQ_AFTER(ofproto_mutex);
-    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. */
@@ -265,70 +280,78 @@ bool cls_rule_is_catchall(const struct cls_rule *);
 bool cls_rule_is_loose_match(const struct cls_rule *rule,
                              const struct minimatch *criteria);
 
-void classifier_init(struct classifier *cls, const uint8_t *flow_segments);
+void classifier_init(struct classifier *, const uint8_t *flow_segments);
 void classifier_destroy(struct classifier *);
-void classifier_set_prefix_fields(struct classifier *cls,
+bool classifier_set_prefix_fields(struct classifier *,
                                   const enum mf_field_id *trie_fields,
-                                  unsigned int n_trie_fields)
-    OVS_REQ_WRLOCK(cls->rwlock);
+                                  unsigned int n_trie_fields);
+
+bool classifier_is_empty(const struct classifier *);
+int classifier_count(const struct classifier *);
+void classifier_insert(struct classifier *, struct cls_rule *);
+struct cls_rule *classifier_replace(struct classifier *, struct cls_rule *);
 
-bool classifier_is_empty(const struct classifier *cls)
-    OVS_REQ_RDLOCK(cls->rwlock);
-int classifier_count(const struct classifier *cls)
-    OVS_REQ_RDLOCK(cls->rwlock);
-void classifier_insert(struct classifier *cls, struct cls_rule *)
-    OVS_REQ_WRLOCK(cls->rwlock);
-struct cls_rule *classifier_replace(struct classifier *cls, struct cls_rule *)
-    OVS_REQ_WRLOCK(cls->rwlock);
-void classifier_remove(struct classifier *cls, struct cls_rule *)
-    OVS_REQ_WRLOCK(cls->rwlock);
-struct cls_rule *classifier_lookup(const struct classifier *cls,
+void classifier_remove(struct classifier *, struct cls_rule *);
+struct cls_rule *classifier_lookup(const struct classifier *,
                                    const struct flow *,
-                                   struct flow_wildcards *)
-    OVS_REQ_RDLOCK(cls->rwlock);
-struct cls_rule *classifier_lookup_miniflow_first(const struct classifier *cls,
-                                                  const struct miniflow *)
-    OVS_REQ_RDLOCK(cls->rwlock);
-bool classifier_rule_overlaps(const struct classifier *cls,
-                              const struct cls_rule *)
-    OVS_REQ_RDLOCK(cls->rwlock);
+                                   struct flow_wildcards *);
+bool classifier_lookup_miniflow_batch(const struct classifier *cls,
+                                      const struct miniflow **flows,
+                                      struct cls_rule **rules,
+                                      const size_t cnt);
+enum { CLASSIFIER_MAX_BATCH = 256 };
+bool classifier_rule_overlaps(const struct classifier *,
+                              const struct cls_rule *);
 
-typedef void cls_cb_func(struct cls_rule *, void *aux);
+struct cls_rule *classifier_find_rule_exactly(const struct classifier *,
+                                              const struct cls_rule *);
 
-struct cls_rule *classifier_find_rule_exactly(const struct classifier *cls,
-                                              const struct cls_rule *)
-    OVS_REQ_RDLOCK(cls->rwlock);
-struct cls_rule *classifier_find_match_exactly(const struct classifier *cls,
+struct cls_rule *classifier_find_match_exactly(const struct classifier *,
                                                const struct match *,
-                                               unsigned int priority)
-    OVS_REQ_RDLOCK(cls->rwlock);
+                                               unsigned int priority);
 \f
 /* 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;
 };
 
-void cls_cursor_init(struct cls_cursor *cursor, const struct classifier *cls,
-                     const struct cls_rule *match) OVS_REQ_RDLOCK(cls->rwlock);
-struct cls_rule *cls_cursor_first(struct cls_cursor *cursor);
-struct cls_rule *cls_cursor_next(struct cls_cursor *, const struct cls_rule *);
+/* 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_start(const struct classifier *cls,
+                                   const struct cls_rule *target,
+                                   bool safe);
+
+void cls_cursor_advance(struct cls_cursor *);
 
-#define CLS_CURSOR_FOR_EACH(RULE, MEMBER, CURSOR)                       \
-    for (ASSIGN_CONTAINER(RULE, cls_cursor_first(CURSOR), MEMBER);      \
-         RULE != OBJECT_CONTAINING(NULL, RULE, MEMBER);                 \
-         ASSIGN_CONTAINER(RULE, cls_cursor_next(CURSOR, &(RULE)->MEMBER), \
-                          MEMBER))
+#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(CLS, TARGET, false); \
+         (cursor__.rule                                                 \
+          ? (INIT_CONTAINER(RULE, cursor__.rule, MEMBER),               \
+             true)                                                      \
+          : false);                                                     \
+         cls_cursor_advance(&cursor__))
 
-#define CLS_CURSOR_FOR_EACH_SAFE(RULE, NEXT, MEMBER, CURSOR)            \
-    for (ASSIGN_CONTAINER(RULE, cls_cursor_first(CURSOR), MEMBER);      \
-         (RULE != OBJECT_CONTAINING(NULL, RULE, MEMBER)                 \
-          ? ASSIGN_CONTAINER(NEXT, cls_cursor_next(CURSOR, &(RULE)->MEMBER), \
-                             MEMBER), 1                                 \
-          : 0);                                                         \
-         (RULE) = (NEXT))
+/* 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);                                                     \
+        )                                                               \
 
 #ifdef __cplusplus
 }