initscript: pass complete path to pidfile to status command
[sliver-openvswitch.git] / lib / flow.h
1 /*
2  * Copyright (c) 2008, 2009 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 <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_to_ovs_match(const flow_t *, uint32_t wildcards, struct ofp_match *);
40 void flow_from_match(flow_t *, uint32_t *wildcards, const struct ofp_match *);
41 char *flow_to_string(const flow_t *);
42 void flow_format(struct ds *, const flow_t *);
43 void flow_print(FILE *, const flow_t *);
44 static inline int flow_compare(const flow_t *, const flow_t *);
45 static inline bool flow_equal(const flow_t *, const flow_t *);
46 static inline size_t flow_hash(const flow_t *, uint32_t basis);
47
48 static inline int
49 flow_compare(const flow_t *a, const flow_t *b)
50 {
51     return memcmp(a, b, sizeof *a);
52 }
53
54 static inline bool
55 flow_equal(const flow_t *a, const flow_t *b)
56 {
57     return !flow_compare(a, b);
58 }
59
60 static inline size_t
61 flow_hash(const flow_t *flow, uint32_t basis)
62 {
63     BUILD_ASSERT_DECL(!(sizeof *flow % sizeof(uint32_t)));
64     return hash_words((const uint32_t *) flow,
65                       sizeof *flow / sizeof(uint32_t), basis);
66 }
67
68 /* Information on wildcards for a flow, as a supplement to flow_t. */
69 struct flow_wildcards {
70     uint32_t wildcards;         /* enum ofp_flow_wildcards (in host order). */
71     uint32_t nw_src_mask;       /* 1-bit in each significant nw_src bit. */
72     uint32_t nw_dst_mask;       /* 1-bit in each significant nw_dst bit. */
73 };
74
75 /* Given the wildcard bit count in bits 'shift' through 'shift + 5' (inclusive)
76  * of 'wildcards', returns a 32-bit bit mask with a 1 in each bit that must
77  * match and a 0 in each bit that is wildcarded.
78  *
79  * The bits in 'wildcards' are in the format used in enum ofp_flow_wildcards: 0
80  * is exact match, 1 ignores the LSB, 2 ignores the 2 least-significant bits,
81  * ..., 32 and higher wildcard the entire field.  This is the *opposite* of the
82  * usual convention where e.g. /24 indicates that 8 bits (not 24 bits) are
83  * wildcarded.
84  *
85  * 'wildcards' is in host byte order.  The return value is in network byte
86  * order. */
87 static inline uint32_t
88 flow_nw_bits_to_mask(uint32_t wildcards, int shift)
89 {
90     wildcards = (wildcards >> shift) & 0x3f;
91     return wildcards < 32 ? htonl(~((1u << wildcards) - 1)) : 0;
92 }
93
94 static inline void
95 flow_wildcards_init(struct flow_wildcards *wc, uint32_t wildcards)
96 {
97     wc->wildcards = wildcards & OFPFW_ALL;
98     wc->nw_src_mask = flow_nw_bits_to_mask(wc->wildcards, OFPFW_NW_SRC_SHIFT);
99     wc->nw_dst_mask = flow_nw_bits_to_mask(wc->wildcards, OFPFW_NW_DST_SHIFT);
100 }
101
102 #endif /* flow.h */