Import from old repository commit 61ef2b42a9c4ba8e1600f15bb0236765edc2ad45.
[cascardo/ovs.git] / lib / flow.h
1 /*
2  * Copyright (c) 2008, 2009 Nicira Networks.
3  *
4  * Permission to use, copy, modify, and/or distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  */
16 #ifndef FLOW_H
17 #define FLOW_H 1
18
19 #include <netinet/in.h>
20 #include <stdbool.h>
21 #include <stdint.h>
22 #include <string.h>
23 #include "openflow/openflow.h"
24 #include "hash.h"
25 #include "openflow/openflow.h"
26 #include "openvswitch/datapath-protocol.h"
27 #include "util.h"
28
29 struct ds;
30 struct ofp_match;
31 struct ofpbuf;
32
33 typedef struct odp_flow_key flow_t;
34
35 int flow_extract(struct ofpbuf *, uint16_t in_port, flow_t *);
36 void flow_extract_stats(const flow_t *flow, struct ofpbuf *packet, 
37         struct odp_flow_stats *stats);
38 void flow_to_match(const flow_t *, uint32_t wildcards, struct ofp_match *);
39 void flow_from_match(flow_t *, uint32_t *wildcards, const struct ofp_match *);
40 char *flow_to_string(const flow_t *);
41 void flow_format(struct ds *, const flow_t *);
42 void flow_print(FILE *, const flow_t *);
43 static inline int flow_compare(const flow_t *, const flow_t *);
44 static inline bool flow_equal(const flow_t *, const flow_t *);
45 static inline size_t flow_hash(const flow_t *, uint32_t basis);
46
47 static inline int
48 flow_compare(const flow_t *a, const flow_t *b)
49 {
50     return memcmp(a, b, sizeof *a);
51 }
52
53 static inline bool
54 flow_equal(const flow_t *a, const flow_t *b)
55 {
56     return !flow_compare(a, b);
57 }
58
59 static inline size_t
60 flow_hash(const flow_t *flow, uint32_t basis)
61 {
62     BUILD_ASSERT_DECL(!(sizeof *flow % sizeof(uint32_t)));
63     return hash_words((const uint32_t *) flow,
64                       sizeof *flow / sizeof(uint32_t), basis);
65 }
66
67 /* Information on wildcards for a flow, as a supplement to flow_t. */
68 struct flow_wildcards {
69     uint32_t wildcards;         /* enum ofp_flow_wildcards (in host order). */
70     uint32_t nw_src_mask;       /* 1-bit in each significant nw_src bit. */
71     uint32_t nw_dst_mask;       /* 1-bit in each significant nw_dst bit. */
72 };
73
74 /* Given the wildcard bit count in bits 'shift' through 'shift + 5' (inclusive)
75  * of 'wildcards', returns a 32-bit bit mask with a 1 in each bit that must
76  * match and a 0 in each bit that is wildcarded.
77  *
78  * The bits in 'wildcards' are in the format used in enum ofp_flow_wildcards: 0
79  * is exact match, 1 ignores the LSB, 2 ignores the 2 least-significant bits,
80  * ..., 32 and higher wildcard the entire field.  This is the *opposite* of the
81  * usual convention where e.g. /24 indicates that 8 bits (not 24 bits) are
82  * wildcarded.
83  *
84  * 'wildcards' is in host byte order.  The return value is in network byte
85  * order. */
86 static inline uint32_t
87 flow_nw_bits_to_mask(uint32_t wildcards, int shift)
88 {
89     wildcards = (wildcards >> shift) & 0x3f;
90     return wildcards < 32 ? htonl(~((1u << wildcards) - 1)) : 0;
91 }
92
93 static inline void
94 flow_wildcards_init(struct flow_wildcards *wc, uint32_t wildcards)
95 {
96     wc->wildcards = wildcards & OFPFW_ALL;
97     wc->nw_src_mask = flow_nw_bits_to_mask(wc->wildcards, OFPFW_NW_SRC_SHIFT);
98     wc->nw_dst_mask = flow_nw_bits_to_mask(wc->wildcards, OFPFW_NW_DST_SHIFT);
99 }
100
101 #endif /* flow.h */