2 * Copyright (c) 2008, 2009, 2010 Nicira Networks.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <netinet/in.h>
23 #include "openflow/openflow.h"
25 #include "openflow/openflow.h"
26 #include "openvswitch/datapath-protocol.h"
33 typedef struct odp_flow_key flow_t;
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);
48 flow_compare(const flow_t *a, const flow_t *b)
50 return memcmp(a, b, sizeof *a);
54 flow_equal(const flow_t *a, const flow_t *b)
56 return !flow_compare(a, b);
60 flow_hash(const flow_t *flow, uint32_t basis)
62 BUILD_ASSERT_DECL(!(sizeof *flow % sizeof(uint32_t)));
63 return hash_words((const uint32_t *) flow,
64 sizeof *flow / sizeof(uint32_t), basis);
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. */
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.
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
84 * 'wildcards' is in host byte order. The return value is in network byte
86 static inline uint32_t
87 flow_nw_bits_to_mask(uint32_t wildcards, int shift)
89 wildcards = (wildcards >> shift) & 0x3f;
90 return wildcards < 32 ? htonl(~((1u << wildcards) - 1)) : 0;
94 flow_wildcards_init(struct flow_wildcards *wc, uint32_t wildcards)
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);