datapath-windows: Allow NdisSwitchPortTypeSynthetic to be updated
[cascardo/ovs.git] / lib / classifier.h
index a89c562..f9af33e 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2009, 2010, 2011, 2012, 2013 Nicira, Inc.
+ * Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014, 2015 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 "flow.h"
-#include "hindex.h"
-#include "hmap.h"
-#include "list.h"
+#include "cmap.h"
 #include "match.h"
 #include "meta-flow.h"
-#include "tag.h"
-#include "openflow/nicira-ext.h"
-#include "openflow/openflow.h"
-#include "ovs-thread.h"
-#include "util.h"
+#include "pvector.h"
+#include "rculist.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;
+    int n_rules;                    /* 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;
+    bool publish;                   /* Make changes visible to lookups? */
+};
+
+struct cls_conjunction {
+    uint32_t id;
+    uint8_t clause;
+    uint8_t n_clauses;
 };
 
 /* A rule to be inserted to the classifier. */
 struct cls_rule {
+    struct rculist node;         /* In struct cls_subtable 'rules_list'. */
+    int priority;                /* Larger numbers are higher priorities. */
+    struct cls_match *cls_match; /* NULL if not in a classifier. */
     struct minimatch match;      /* Matching rule. */
-    unsigned int priority;       /* Larger numbers are higher priorities. */
-    struct cls_match *cls_match; /* NULL if rule is not in a classifier. */
 };
 
-void cls_rule_init(struct cls_rule *, const struct match *,
-                   unsigned int priority);
+void cls_rule_init(struct cls_rule *, const struct match *, int priority);
 void cls_rule_init_from_minimatch(struct cls_rule *, const struct minimatch *,
-                                  unsigned int priority);
+                                  int priority);
 void cls_rule_clone(struct cls_rule *, const struct cls_rule *);
 void cls_rule_move(struct cls_rule *dst, struct cls_rule *src);
 void cls_rule_destroy(struct cls_rule *);
 
+void cls_rule_set_conjunctions(struct cls_rule *,
+                               const struct cls_conjunction *, size_t n);
+
 bool cls_rule_equal(const struct cls_rule *, const struct cls_rule *);
 uint32_t cls_rule_hash(const struct cls_rule *, uint32_t basis);
-
 void cls_rule_format(const struct cls_rule *, struct ds *);
-
 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);
+/* Constructor/destructor.  Must run single-threaded. */
+void classifier_init(struct classifier *, const uint8_t *flow_segments);
 void classifier_destroy(struct classifier *);
-void classifier_set_prefix_fields(struct classifier *cls,
-                                  const enum mf_field_id *trie_fields,
-                                  unsigned int n_trie_fields)
-    OVS_REQ_WRLOCK(cls->rwlock);
-
-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,
-                                   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);
 
-typedef void cls_cb_func(struct cls_rule *, void *aux);
+/* Modifiers.  Caller MUST exclude concurrent calls from other threads. */
+bool classifier_set_prefix_fields(struct classifier *,
+                                  const enum mf_field_id *trie_fields,
+                                  unsigned int n_trie_fields);
+void classifier_insert(struct classifier *, const struct cls_rule *,
+                       const struct cls_conjunction *, size_t n_conjunctions);
+const struct cls_rule *classifier_replace(struct classifier *,
+                                          const struct cls_rule *,
+                                          const struct cls_conjunction *,
+                                          size_t n_conjunctions);
+const struct cls_rule *classifier_remove(struct classifier *,
+                                         const struct cls_rule *);
+static inline void classifier_defer(struct classifier *);
+static inline void classifier_publish(struct classifier *);
 
-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,
-                                               const struct match *,
-                                               unsigned int priority)
-    OVS_REQ_RDLOCK(cls->rwlock);
+/* Lookups.  These are RCU protected and may run concurrently with modifiers
+ * and each other. */
+const struct cls_rule *classifier_lookup(const struct classifier *,
+                                         struct flow *,
+                                         struct flow_wildcards *);
+bool classifier_rule_overlaps(const struct classifier *,
+                              const struct cls_rule *);
+const struct cls_rule *classifier_find_rule_exactly(const struct classifier *,
+                                                    const struct cls_rule *);
+const struct cls_rule *classifier_find_match_exactly(const struct classifier *,
+                                                     const struct match *,
+                                                     int priority);
+bool classifier_is_empty(const struct classifier *);
+int classifier_count(const struct classifier *);
 \f
-/* Iteration. */
-
+/* Iteration.
+ *
+ * Iteration is lockless and RCU-protected.  Concurrent threads may perform all
+ * kinds of concurrent modifications without ruining the iteration.  Obviously,
+ * any modifications may or may not be visible to the concurrent iterator, but
+ * all the rules not deleted are visited by the iteration.  The iterating
+ * thread may also modify the classifier rules itself.
+ *
+ * 'TARGET' iteration only iterates rules matching the 'TARGET' criteria.
+ * Rather than looping through all the rules and skipping ones that can't
+ * match, 'TARGET' iteration skips whole subtables, if the 'TARGET' happens to
+ * be more specific than the subtable. */
 struct cls_cursor {
-    const struct cls_classifier *cls;
+    const struct classifier *cls;
     const struct cls_subtable *subtable;
     const struct cls_rule *target;
+    struct pvector_cursor subtables;
+    const struct cls_rule *rule;
 };
 
-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 *);
-
-#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))
+struct cls_cursor cls_cursor_start(const struct classifier *cls,
+                                   const struct cls_rule *target);
+void cls_cursor_advance(struct cls_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))
+#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);    \
+         (cursor__.rule                                                 \
+          ? (INIT_CONTAINER(RULE, cursor__.rule, MEMBER),               \
+             cls_cursor_advance(&cursor__),                             \
+             true)                                                      \
+          : false);                                                     \
+        )
 
 #ifdef __cplusplus
 }
 #endif
 
+\f
+static inline void
+classifier_defer(struct classifier *cls)
+{
+    cls->publish = false;
+}
+
+static inline void
+classifier_publish(struct classifier *cls)
+{
+    cls->publish = true;
+    pvector_publish(&cls->subtables);
+}
 #endif /* classifier.h */