2009-07-08 13:19:16 -07:00
|
|
|
|
/*
|
2012-01-27 17:16:05 -08:00
|
|
|
|
* Copyright (c) 2009, 2010, 2011, 2012 Nicira Networks.
|
2009-07-08 13:19:16 -07:00
|
|
|
|
*
|
2009-06-15 15:11:30 -07:00
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
|
* You may obtain a copy of the License at:
|
2009-07-08 13:19:16 -07:00
|
|
|
|
*
|
2009-06-15 15:11:30 -07:00
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
*
|
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
|
* limitations under the License.
|
2009-07-08 13:19:16 -07:00
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#ifndef CLASSIFIER_H
|
|
|
|
|
#define CLASSIFIER_H 1
|
|
|
|
|
|
|
|
|
|
/* Flow classifier.
|
|
|
|
|
*
|
2010-11-03 11:00:58 -07:00
|
|
|
|
* A classifier is a "struct classifier",
|
|
|
|
|
* a hash map from a set of wildcards to a "struct cls_table",
|
|
|
|
|
* a hash map from fixed field values to "struct cls_rule",
|
|
|
|
|
* which can contain a list of otherwise identical rules
|
|
|
|
|
* with lower priorities.
|
2009-07-08 13:19:16 -07:00
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "flow.h"
|
|
|
|
|
#include "hmap.h"
|
|
|
|
|
#include "list.h"
|
2010-04-12 11:49:16 -04:00
|
|
|
|
#include "openflow/nicira-ext.h"
|
2009-07-08 13:19:16 -07:00
|
|
|
|
#include "openflow/openflow.h"
|
|
|
|
|
|
2011-08-04 16:18:59 -07:00
|
|
|
|
#ifdef __cplusplus
|
|
|
|
|
extern "C" {
|
|
|
|
|
#endif
|
|
|
|
|
|
2009-07-08 13:19:16 -07:00
|
|
|
|
/* A flow classifier. */
|
|
|
|
|
struct classifier {
|
2010-11-03 11:00:58 -07:00
|
|
|
|
int n_rules; /* Total number of rules. */
|
|
|
|
|
struct hmap tables; /* Contains "struct cls_table"s. */
|
2009-07-08 13:19:16 -07:00
|
|
|
|
};
|
|
|
|
|
|
2010-11-03 11:00:58 -07:00
|
|
|
|
/* A set of rules that all have the same fields wildcarded. */
|
|
|
|
|
struct cls_table {
|
2011-08-18 13:02:22 -07:00
|
|
|
|
struct hmap_node hmap_node; /* Within struct classifier 'tables' hmap. */
|
2010-11-03 11:00:58 -07:00
|
|
|
|
struct hmap rules; /* Contains "struct cls_rule"s. */
|
|
|
|
|
struct flow_wildcards wc; /* Wildcards for fields. */
|
|
|
|
|
int n_table_rules; /* Number of rules, including duplicates. */
|
2012-04-09 15:49:22 -07:00
|
|
|
|
bool is_catchall; /* True if this table wildcards every field. */
|
2009-07-08 13:19:16 -07:00
|
|
|
|
};
|
|
|
|
|
|
2011-09-12 16:48:07 -07:00
|
|
|
|
/* Returns true if 'table' is a "catch-all" table that will match every
|
|
|
|
|
* packet (if there is no higher-priority match). */
|
|
|
|
|
static inline bool
|
|
|
|
|
cls_table_is_catchall(const struct cls_table *table)
|
|
|
|
|
{
|
2012-04-09 15:49:22 -07:00
|
|
|
|
return table->is_catchall;
|
2011-09-12 16:48:07 -07:00
|
|
|
|
}
|
|
|
|
|
|
2009-07-08 13:19:16 -07:00
|
|
|
|
/* A flow classification rule.
|
|
|
|
|
*
|
2010-11-03 11:00:58 -07:00
|
|
|
|
* Use one of the cls_rule_*() functions to initialize a cls_rule.
|
|
|
|
|
*
|
|
|
|
|
* The cls_rule_*() functions below maintain the following important
|
|
|
|
|
* invariant that the classifier depends on:
|
|
|
|
|
*
|
|
|
|
|
* - If a bit or a field is wildcarded in 'wc', then the corresponding bit or
|
2010-11-08 10:36:26 -08:00
|
|
|
|
* field in 'flow' is set to all-0-bits. (The
|
|
|
|
|
* cls_rule_zero_wildcarded_fields() function can be used to restore this
|
|
|
|
|
* invariant after adding wildcards.)
|
2010-11-03 11:00:58 -07:00
|
|
|
|
*/
|
2009-07-08 13:19:16 -07:00
|
|
|
|
struct cls_rule {
|
2010-11-03 11:00:58 -07:00
|
|
|
|
struct hmap_node hmap_node; /* Within struct cls_table 'rules'. */
|
|
|
|
|
struct list list; /* List of identical, lower-priority rules. */
|
2010-09-03 11:30:02 -07:00
|
|
|
|
struct flow flow; /* All field values. */
|
2009-07-08 13:19:16 -07:00
|
|
|
|
struct flow_wildcards wc; /* Wildcards for fields. */
|
|
|
|
|
unsigned int priority; /* Larger numbers are higher priorities. */
|
|
|
|
|
};
|
|
|
|
|
|
2010-11-08 10:37:35 -08:00
|
|
|
|
void cls_rule_init(const struct flow *, const struct flow_wildcards *,
|
|
|
|
|
unsigned int priority, struct cls_rule *);
|
|
|
|
|
void cls_rule_init_exact(const struct flow *, unsigned int priority,
|
|
|
|
|
struct cls_rule *);
|
2010-11-22 10:10:14 -08:00
|
|
|
|
void cls_rule_init_catchall(struct cls_rule *, unsigned int priority);
|
|
|
|
|
|
2010-11-08 10:36:26 -08:00
|
|
|
|
void cls_rule_zero_wildcarded_fields(struct cls_rule *);
|
2010-11-03 11:00:58 -07:00
|
|
|
|
|
2010-11-17 10:07:48 -08:00
|
|
|
|
void cls_rule_set_reg(struct cls_rule *, unsigned int reg_idx, uint32_t value);
|
|
|
|
|
void cls_rule_set_reg_masked(struct cls_rule *, unsigned int reg_idx,
|
|
|
|
|
uint32_t value, uint32_t mask);
|
2010-12-10 10:42:42 -08:00
|
|
|
|
void cls_rule_set_tun_id(struct cls_rule *, ovs_be64 tun_id);
|
2011-01-20 15:29:00 -08:00
|
|
|
|
void cls_rule_set_tun_id_masked(struct cls_rule *,
|
|
|
|
|
ovs_be64 tun_id, ovs_be64 mask);
|
2011-12-28 10:51:15 -08:00
|
|
|
|
void cls_rule_set_in_port(struct cls_rule *, uint16_t ofp_port);
|
2010-11-08 10:31:29 -08:00
|
|
|
|
void cls_rule_set_dl_type(struct cls_rule *, ovs_be16);
|
2010-10-26 16:41:44 -07:00
|
|
|
|
void cls_rule_set_dl_src(struct cls_rule *, const uint8_t[6]);
|
|
|
|
|
void cls_rule_set_dl_dst(struct cls_rule *, const uint8_t[6]);
|
2011-06-06 14:21:40 -07:00
|
|
|
|
void cls_rule_set_dl_dst_masked(struct cls_rule *, const uint8_t dl_dst[6],
|
|
|
|
|
const uint8_t mask[6]);
|
2010-11-23 10:06:28 -08:00
|
|
|
|
void cls_rule_set_dl_tci(struct cls_rule *, ovs_be16 tci);
|
|
|
|
|
void cls_rule_set_dl_tci_masked(struct cls_rule *,
|
2010-11-08 10:31:29 -08:00
|
|
|
|
ovs_be16 tci, ovs_be16 mask);
|
2010-11-23 10:06:28 -08:00
|
|
|
|
void cls_rule_set_any_vid(struct cls_rule *);
|
2010-11-08 10:31:29 -08:00
|
|
|
|
void cls_rule_set_dl_vlan(struct cls_rule *, ovs_be16);
|
2010-11-23 10:06:28 -08:00
|
|
|
|
void cls_rule_set_any_pcp(struct cls_rule *);
|
2010-11-08 10:31:29 -08:00
|
|
|
|
void cls_rule_set_dl_vlan_pcp(struct cls_rule *, uint8_t);
|
2010-10-26 16:41:44 -07:00
|
|
|
|
void cls_rule_set_tp_src(struct cls_rule *, ovs_be16);
|
2012-01-27 17:16:05 -08:00
|
|
|
|
void cls_rule_set_tp_src_masked(struct cls_rule *,
|
|
|
|
|
ovs_be16 port, ovs_be16 mask);
|
2010-10-26 16:41:44 -07:00
|
|
|
|
void cls_rule_set_tp_dst(struct cls_rule *, ovs_be16);
|
2012-01-27 17:16:05 -08:00
|
|
|
|
void cls_rule_set_tp_dst_masked(struct cls_rule *,
|
|
|
|
|
ovs_be16 port, ovs_be16 mask);
|
2010-10-26 16:41:44 -07:00
|
|
|
|
void cls_rule_set_nw_proto(struct cls_rule *, uint8_t);
|
|
|
|
|
void cls_rule_set_nw_src(struct cls_rule *, ovs_be32);
|
2011-11-09 17:42:17 -08:00
|
|
|
|
void cls_rule_set_nw_src_masked(struct cls_rule *, ovs_be32 ip, ovs_be32 mask);
|
2010-10-26 16:41:44 -07:00
|
|
|
|
void cls_rule_set_nw_dst(struct cls_rule *, ovs_be32);
|
2011-11-09 17:42:17 -08:00
|
|
|
|
void cls_rule_set_nw_dst_masked(struct cls_rule *, ovs_be32 ip, ovs_be32 mask);
|
2011-11-02 23:34:15 -07:00
|
|
|
|
void cls_rule_set_nw_dscp(struct cls_rule *, uint8_t);
|
|
|
|
|
void cls_rule_set_nw_ecn(struct cls_rule *, uint8_t);
|
2011-11-05 15:48:12 -07:00
|
|
|
|
void cls_rule_set_nw_ttl(struct cls_rule *, uint8_t);
|
2011-11-09 17:10:27 -08:00
|
|
|
|
void cls_rule_set_nw_frag(struct cls_rule *, uint8_t nw_frag);
|
|
|
|
|
void cls_rule_set_nw_frag_masked(struct cls_rule *,
|
|
|
|
|
uint8_t nw_frag, uint8_t mask);
|
2010-11-08 10:31:29 -08:00
|
|
|
|
void cls_rule_set_icmp_type(struct cls_rule *, uint8_t);
|
|
|
|
|
void cls_rule_set_icmp_code(struct cls_rule *, uint8_t);
|
2010-12-07 14:02:17 -08:00
|
|
|
|
void cls_rule_set_arp_sha(struct cls_rule *, const uint8_t[6]);
|
|
|
|
|
void cls_rule_set_arp_tha(struct cls_rule *, const uint8_t[6]);
|
2010-12-29 19:03:46 -08:00
|
|
|
|
void cls_rule_set_ipv6_src(struct cls_rule *, const struct in6_addr *);
|
2011-11-09 17:42:17 -08:00
|
|
|
|
void cls_rule_set_ipv6_src_masked(struct cls_rule *, const struct in6_addr *,
|
2010-12-29 19:03:46 -08:00
|
|
|
|
const struct in6_addr *);
|
|
|
|
|
void cls_rule_set_ipv6_dst(struct cls_rule *, const struct in6_addr *);
|
2011-11-09 17:42:17 -08:00
|
|
|
|
void cls_rule_set_ipv6_dst_masked(struct cls_rule *, const struct in6_addr *,
|
2010-12-29 19:03:46 -08:00
|
|
|
|
const struct in6_addr *);
|
2011-11-01 15:57:56 -07:00
|
|
|
|
void cls_rule_set_ipv6_label(struct cls_rule *, ovs_be32);
|
2011-09-12 10:57:28 -07:00
|
|
|
|
void cls_rule_set_nd_target(struct cls_rule *, const struct in6_addr *);
|
2012-04-25 15:48:40 -07:00
|
|
|
|
void cls_rule_set_nd_target_masked(struct cls_rule *, const struct in6_addr *,
|
|
|
|
|
const struct in6_addr *);
|
2010-10-26 16:41:44 -07:00
|
|
|
|
|
2010-11-08 16:35:34 -08:00
|
|
|
|
bool cls_rule_equal(const struct cls_rule *, const struct cls_rule *);
|
2011-05-26 16:24:38 -07:00
|
|
|
|
uint32_t cls_rule_hash(const struct cls_rule *, uint32_t basis);
|
2010-11-08 16:35:34 -08:00
|
|
|
|
|
2010-11-23 12:31:50 -08:00
|
|
|
|
void cls_rule_format(const struct cls_rule *, struct ds *);
|
2010-02-11 13:47:30 -08:00
|
|
|
|
char *cls_rule_to_string(const struct cls_rule *);
|
2009-07-08 13:19:16 -07:00
|
|
|
|
void cls_rule_print(const struct cls_rule *);
|
|
|
|
|
|
|
|
|
|
void classifier_init(struct classifier *);
|
|
|
|
|
void classifier_destroy(struct classifier *);
|
|
|
|
|
bool classifier_is_empty(const struct classifier *);
|
|
|
|
|
int classifier_count(const struct classifier *);
|
2011-05-11 14:06:48 -07:00
|
|
|
|
void classifier_insert(struct classifier *, struct cls_rule *);
|
|
|
|
|
struct cls_rule *classifier_replace(struct classifier *, struct cls_rule *);
|
2009-07-08 13:19:16 -07:00
|
|
|
|
void classifier_remove(struct classifier *, struct cls_rule *);
|
2010-09-03 11:30:02 -07:00
|
|
|
|
struct cls_rule *classifier_lookup(const struct classifier *,
|
2010-10-28 13:26:31 -07:00
|
|
|
|
const struct flow *);
|
2010-10-19 13:04:52 -07:00
|
|
|
|
bool classifier_rule_overlaps(const struct classifier *,
|
|
|
|
|
const struct cls_rule *);
|
2009-07-08 13:19:16 -07:00
|
|
|
|
|
|
|
|
|
typedef void cls_cb_func(struct cls_rule *, void *aux);
|
|
|
|
|
|
|
|
|
|
struct cls_rule *classifier_find_rule_exactly(const struct classifier *,
|
2010-10-14 13:25:36 -07:00
|
|
|
|
const struct cls_rule *);
|
2010-10-28 16:18:20 -07:00
|
|
|
|
|
|
|
|
|
/* Iteration. */
|
|
|
|
|
|
|
|
|
|
struct cls_cursor {
|
|
|
|
|
const struct classifier *cls;
|
|
|
|
|
const struct cls_table *table;
|
|
|
|
|
const struct cls_rule *target;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
void cls_cursor_init(struct cls_cursor *, const struct classifier *,
|
|
|
|
|
const struct cls_rule *match);
|
|
|
|
|
struct cls_rule *cls_cursor_first(struct cls_cursor *);
|
|
|
|
|
struct cls_rule *cls_cursor_next(struct cls_cursor *, struct cls_rule *);
|
|
|
|
|
|
|
|
|
|
#define CLS_CURSOR_FOR_EACH(RULE, MEMBER, CURSOR) \
|
2010-11-17 14:25:33 -08:00
|
|
|
|
for (ASSIGN_CONTAINER(RULE, cls_cursor_first(CURSOR), MEMBER); \
|
2010-10-28 16:18:20 -07:00
|
|
|
|
&(RULE)->MEMBER != NULL; \
|
2010-11-17 14:25:33 -08:00
|
|
|
|
ASSIGN_CONTAINER(RULE, cls_cursor_next(CURSOR, &(RULE)->MEMBER), \
|
|
|
|
|
MEMBER))
|
2010-10-28 16:18:20 -07:00
|
|
|
|
|
|
|
|
|
#define CLS_CURSOR_FOR_EACH_SAFE(RULE, NEXT, MEMBER, CURSOR) \
|
2010-11-17 14:25:33 -08:00
|
|
|
|
for (ASSIGN_CONTAINER(RULE, cls_cursor_first(CURSOR), MEMBER); \
|
2010-10-28 16:18:20 -07:00
|
|
|
|
(&(RULE)->MEMBER != NULL \
|
2010-11-17 14:25:33 -08:00
|
|
|
|
? ASSIGN_CONTAINER(NEXT, cls_cursor_next(CURSOR, &(RULE)->MEMBER), \
|
|
|
|
|
MEMBER) \
|
2010-10-28 16:18:20 -07:00
|
|
|
|
: 0); \
|
|
|
|
|
(RULE) = (NEXT))
|
2009-07-08 13:19:16 -07:00
|
|
|
|
|
2011-08-04 16:18:59 -07:00
|
|
|
|
#ifdef __cplusplus
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2009-07-08 13:19:16 -07:00
|
|
|
|
#endif /* classifier.h */
|