treewide: Remove trailing whitespace
[sliver-openvswitch.git] / lib / flow.h
1 /*
2  * Copyright (c) 2008, 2009, 2010 Nicira Networks.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #ifndef FLOW_H
17 #define FLOW_H 1
18
19 #include <sys/types.h>
20 #include <netinet/in.h>
21 #include <stdbool.h>
22 #include <stdint.h>
23 #include <string.h>
24 #include "openflow/nicira-ext.h"
25 #include "openflow/openflow.h"
26 #include "hash.h"
27 #include "openvswitch/datapath-protocol.h"
28 #include "util.h"
29
30 struct ds;
31 struct ofp_match;
32 struct ofpbuf;
33
34 typedef struct odp_flow_key flow_t;
35
36 int flow_extract(struct ofpbuf *, uint32_t tun_id, uint16_t in_port, flow_t *);
37 void flow_extract_stats(const flow_t *flow, struct ofpbuf *packet,
38         struct odp_flow_stats *stats);
39 void flow_to_match(const flow_t *, uint32_t wildcards, bool tun_id_cookie,
40                    struct ofp_match *);
41 void flow_from_match(const struct ofp_match *, bool tun_id_from_cookie,
42                      uint64_t cookie, flow_t *, uint32_t *wildcards);
43 char *flow_to_string(const flow_t *);
44 void flow_format(struct ds *, const flow_t *);
45 void flow_print(FILE *, const flow_t *);
46 static inline int flow_compare(const flow_t *, const flow_t *);
47 static inline bool flow_equal(const flow_t *, const flow_t *);
48 static inline size_t flow_hash(const flow_t *, uint32_t basis);
49
50 static inline int
51 flow_compare(const flow_t *a, const flow_t *b)
52 {
53     return memcmp(a, b, sizeof *a);
54 }
55
56 static inline bool
57 flow_equal(const flow_t *a, const flow_t *b)
58 {
59     return !flow_compare(a, b);
60 }
61
62 static inline size_t
63 flow_hash(const flow_t *flow, uint32_t basis)
64 {
65     BUILD_ASSERT_DECL(!(sizeof *flow % sizeof(uint32_t)));
66     return hash_words((const uint32_t *) flow,
67                       sizeof *flow / sizeof(uint32_t), basis);
68 }
69
70 /* Information on wildcards for a flow, as a supplement to flow_t. */
71 struct flow_wildcards {
72     uint32_t wildcards;         /* enum ofp_flow_wildcards (in host order). */
73     uint32_t nw_src_mask;       /* 1-bit in each significant nw_src bit. */
74     uint32_t nw_dst_mask;       /* 1-bit in each significant nw_dst bit. */
75 };
76
77 /* Given the wildcard bit count in bits 'shift' through 'shift + 5' (inclusive)
78  * of 'wildcards', returns a 32-bit bit mask with a 1 in each bit that must
79  * match and a 0 in each bit that is wildcarded.
80  *
81  * The bits in 'wildcards' are in the format used in enum ofp_flow_wildcards: 0
82  * is exact match, 1 ignores the LSB, 2 ignores the 2 least-significant bits,
83  * ..., 32 and higher wildcard the entire field.  This is the *opposite* of the
84  * usual convention where e.g. /24 indicates that 8 bits (not 24 bits) are
85  * wildcarded.
86  *
87  * 'wildcards' is in host byte order.  The return value is in network byte
88  * order. */
89 static inline uint32_t
90 flow_nw_bits_to_mask(uint32_t wildcards, int shift)
91 {
92     wildcards = (wildcards >> shift) & 0x3f;
93     return wildcards < 32 ? htonl(~((1u << wildcards) - 1)) : 0;
94 }
95
96 static inline void
97 flow_wildcards_init(struct flow_wildcards *wc, uint32_t wildcards)
98 {
99     wc->wildcards = wildcards & OVSFW_ALL;
100     wc->nw_src_mask = flow_nw_bits_to_mask(wc->wildcards, OFPFW_NW_SRC_SHIFT);
101     wc->nw_dst_mask = flow_nw_bits_to_mask(wc->wildcards, OFPFW_NW_DST_SHIFT);
102 }
103
104 #endif /* flow.h */