]> git.proxmox.com Git - ovs.git/blame - lib/meta-flow.h
meta-flow: Fix and simplify mf_get_mask().
[ovs.git] / lib / meta-flow.h
CommitLineData
6a885fd0 1/*
e0edde6f 2 * Copyright (c) 2011, 2012 Nicira, Inc.
6a885fd0
BP
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
17#ifndef META_FLOW_H
18#define META_FLOW_H 1
19
6ca00f6f
ETN
20#include <sys/types.h>
21#include <netinet/in.h>
6a885fd0
BP
22#include <netinet/ip6.h>
23#include "flow.h"
816fd533 24#include "ofp-errors.h"
6a885fd0
BP
25#include "packets.h"
26
6a885fd0 27struct ds;
81a76618 28struct match;
6a885fd0
BP
29
30/* The comment on each of these indicates the member in "union mf_value" used
31 * to represent its value. */
32enum mf_field_id {
33 /* Metadata. */
34 MFF_TUN_ID, /* be64 */
4fe3445a
PS
35 MFF_TUN_SRC, /* be32 */
36 MFF_TUN_DST, /* be32 */
37 MFF_TUN_FLAGS, /* be16 */
38 MFF_TUN_TTL, /* u8 */
39 MFF_TUN_TOS, /* u8 */
969fc56c 40 MFF_METADATA, /* be64 */
6a885fd0
BP
41 MFF_IN_PORT, /* be16 */
42
43#if FLOW_N_REGS > 0
44 MFF_REG0, /* be32 */
45#endif
46#if FLOW_N_REGS > 1
47 MFF_REG1, /* be32 */
48#endif
49#if FLOW_N_REGS > 2
50 MFF_REG2, /* be32 */
51#endif
52#if FLOW_N_REGS > 3
53 MFF_REG3, /* be32 */
54#endif
55#if FLOW_N_REGS > 4
d2c0fed9
JP
56 MFF_REG4, /* be32 */
57#endif
58#if FLOW_N_REGS > 5
e9358af6
EJ
59 MFF_REG5, /* be32 */
60#endif
61#if FLOW_N_REGS > 6
62 MFF_REG6, /* be32 */
63#endif
64#if FLOW_N_REGS > 7
65 MFF_REG7, /* be32 */
6a885fd0
BP
66#endif
67
68 /* L2. */
69 MFF_ETH_SRC, /* mac */
70 MFF_ETH_DST, /* mac */
71 MFF_ETH_TYPE, /* be16 */
72
73 MFF_VLAN_TCI, /* be16 */
441c57a9 74 MFF_DL_VLAN, /* be16 (OpenFlow 1.0 compatibility) */
cc34bc8c 75 MFF_VLAN_VID, /* be16 (OpenFlow 1.2 compatibility) */
441c57a9 76 MFF_DL_VLAN_PCP, /* u8 (OpenFlow 1.0 compatibility) */
cc34bc8c 77 MFF_VLAN_PCP, /* be16 (OpenFlow 1.2 compatibility) */
6a885fd0
BP
78
79 /* L3. */
80 MFF_IPV4_SRC, /* be32 */
81 MFF_IPV4_DST, /* be32 */
82
83 MFF_IPV6_SRC, /* ipv6 */
84 MFF_IPV6_DST, /* ipv6 */
fa8223b7 85 MFF_IPV6_LABEL, /* be32 */
6a885fd0
BP
86
87 MFF_IP_PROTO, /* u8 (used for IPv4 or IPv6) */
530180fd
JP
88 MFF_IP_DSCP, /* u8 (used for IPv4 or IPv6) */
89 MFF_IP_ECN, /* u8 (used for IPv4 or IPv6) */
a61680c6 90 MFF_IP_TTL, /* u8 (used for IPv4 or IPv6) */
7257b535 91 MFF_IP_FRAG, /* u8 (used for IPv4 or IPv6) */
6a885fd0
BP
92
93 MFF_ARP_OP, /* be16 */
94 MFF_ARP_SPA, /* be32 */
95 MFF_ARP_TPA, /* be32 */
96 MFF_ARP_SHA, /* mac */
97 MFF_ARP_THA, /* mac */
98
99 /* L4. */
100 MFF_TCP_SRC, /* be16 (used for IPv4 or IPv6) */
101 MFF_TCP_DST, /* be16 (used for IPv4 or IPv6) */
102
103 MFF_UDP_SRC, /* be16 (used for IPv4 or IPv6) */
104 MFF_UDP_DST, /* be16 (used for IPv4 or IPv6) */
105
268a95e0
BP
106 MFF_ICMPV4_TYPE, /* u8 */
107 MFF_ICMPV4_CODE, /* u8 */
108
109 MFF_ICMPV6_TYPE, /* u8 */
110 MFF_ICMPV6_CODE, /* u8 */
6a885fd0
BP
111
112 /* ICMPv6 Neighbor Discovery. */
113 MFF_ND_TARGET, /* ipv6 */
114 MFF_ND_SLL, /* mac */
115 MFF_ND_TLL, /* mac */
116
117 MFF_N_IDS
118};
119
0d7e2fe4
IY
120/* Use this macro as CASE_MFF_REGS: in a switch statement to choose all of the
121 * MFF_REGx cases. */
122#if FLOW_N_REGS == 1
123# define CASE_MFF_REGS \
124 case MFF_REG0
125#elif FLOW_N_REGS == 2
126# define CASE_MFF_REGS \
127 case MFF_REG0: case MFF_REG1
128#elif FLOW_N_REGS == 3
129# define CASE_MFF_REGS \
130 case MFF_REG0: case MFF_REG1: case MFF_REG2
131#elif FLOW_N_REGS == 4
132# define CASE_MFF_REGS \
133 case MFF_REG0: case MFF_REG1: case MFF_REG2: case MFF_REG3
134#elif FLOW_N_REGS == 5
135# define CASE_MFF_REGS \
136 case MFF_REG0: case MFF_REG1: case MFF_REG2: case MFF_REG3: \
137 case MFF_REG4
138#elif FLOW_N_REGS == 6
139# define CASE_MFF_REGS \
140 case MFF_REG0: case MFF_REG1: case MFF_REG2: case MFF_REG3: \
141 case MFF_REG4: case MFF_REG5
142#elif FLOW_N_REGS == 7
143# define CASE_MFF_REGS \
144 case MFF_REG0: case MFF_REG1: case MFF_REG2: case MFF_REG3: \
145 case MFF_REG4: case MFF_REG5: case MFF_REG6
146#elif FLOW_N_REGS == 8
147# define CASE_MFF_REGS \
148 case MFF_REG0: case MFF_REG1: case MFF_REG2: case MFF_REG3: \
149 case MFF_REG4: case MFF_REG5: case MFF_REG6: case MFF_REG7
150#else
151# error
152#endif
153
6a885fd0
BP
154/* Prerequisites for matching a field.
155 *
156 * A field may only be matched if the correct lower-level protocols are also
157 * matched. For example, the TCP port may be matched only if the Ethernet type
158 * matches ETH_TYPE_IP and the IP protocol matches IPPROTO_TCP. */
159enum mf_prereqs {
160 MFP_NONE,
161
162 /* L2 requirements. */
163 MFP_ARP,
8069b0da 164 MFP_VLAN_VID,
6a885fd0
BP
165 MFP_IPV4,
166 MFP_IPV6,
167 MFP_IP_ANY,
168
169 /* L2+L3 requirements. */
170 MFP_TCP, /* On IPv4 or IPv6. */
171 MFP_UDP, /* On IPv4 or IPv6. */
268a95e0 172 MFP_ICMPV4,
6a885fd0 173 MFP_ICMPV6,
6a885fd0
BP
174
175 /* L2+L3+L4 requirements. */
176 MFP_ND,
177 MFP_ND_SOLICIT,
178 MFP_ND_ADVERT
179};
180
181/* Forms of partial-field masking allowed for a field.
182 *
183 * Every field may be masked as a whole. */
184enum mf_maskable {
185 MFM_NONE, /* No sub-field masking. */
186 MFM_FULLY, /* Every bit is individually maskable. */
6a885fd0
BP
187};
188
189/* How to format or parse a field's value. */
190enum mf_string {
191 /* Integer formats.
192 *
193 * The particular MFS_* constant sets the output format. On input, either
194 * decimal or hexadecimal (prefixed with 0x) is accepted. */
195 MFS_DECIMAL,
196 MFS_HEXADECIMAL,
197
198 /* Other formats. */
199 MFS_ETHERNET,
200 MFS_IPV4,
201 MFS_IPV6,
7257b535 202 MFS_OFP_PORT, /* An OpenFlow port number or name. */
4fe3445a
PS
203 MFS_FRAG, /* no, yes, first, later, not_later */
204 MFS_TNL_FLAGS, /* FLOW_TNL_F_* flags */
6a885fd0
BP
205};
206
207struct mf_field {
208 /* Identification. */
209 enum mf_field_id id; /* MFF_*. */
210 const char *name; /* Name of this field, e.g. "eth_type". */
211 const char *extra_name; /* Alternate name, e.g. "dl_type", or NULL. */
212
213 /* Size.
214 *
9df6a679
JP
215 * Most fields have n_bytes * 8 == n_bits. There are a few exceptions:
216 *
217 * - "dl_vlan" is 2 bytes but only 12 bits.
218 * - "dl_vlan_pcp" is 1 byte but only 3 bits.
219 * - "is_frag" is 1 byte but only 2 bits.
fa8223b7 220 * - "ipv6_label" is 4 bytes but only 20 bits.
9df6a679 221 */
6a885fd0
BP
222 unsigned int n_bytes; /* Width of the field in bytes. */
223 unsigned int n_bits; /* Number of significant bits in field. */
224
225 /* Properties. */
226 enum mf_maskable maskable;
6a885fd0
BP
227 enum mf_string string;
228 enum mf_prereqs prereqs;
28da1f8f
BP
229 bool writable; /* May be written by actions? */
230
b5e5143b 231 /* NXM and OXM properties.
28da1f8f 232 *
b5e5143b
BP
233 * There are the following possibilities for these members for a given
234 * mf_field:
235 *
236 * - Neither NXM nor OXM defines such a field: these members will all be
237 * zero or NULL.
238 *
239 * - NXM and OXM both define such a field: nxm_header and oxm_header will
240 * both be nonzero and different, similarly for nxm_name and oxm_name.
241 *
242 * - Only NXM or only OXM defines such a field: nxm_header and oxm_header
243 * will both have the same value (either an OXM_* or NXM_* value) and
244 * similarly for nxm_name and oxm_name.
245 *
246 * Thus, 'nxm_header' is the appropriate header to use when outputting an
247 * NXM formatted match, since it will be an NXM_* constant when possible
248 * for compatibility with OpenFlow implementations that expect that, with
249 * OXM_* constants used for fields that OXM adds. Conversely, 'oxm_header'
250 * is the header to use when outputting an OXM formatted match. */
251 uint32_t nxm_header; /* An NXM_* (or OXM_*) constant. */
252 const char *nxm_name; /* The nxm_header constant's name. */
253 uint32_t oxm_header; /* An OXM_* (or NXM_*) constant. */
254 const char *oxm_name; /* The oxm_header constant's name */
6a885fd0
BP
255};
256
257/* The representation of a field's value. */
258union mf_value {
259 uint8_t u8;
260 ovs_be16 be16;
261 ovs_be32 be32;
262 ovs_be64 be64;
263 uint8_t mac[ETH_ADDR_LEN];
264 struct in6_addr ipv6;
265};
1b35df45 266BUILD_ASSERT_DECL(sizeof(union mf_value) == 16);
6a885fd0 267
816fd533
BP
268/* Part of a field. */
269struct mf_subfield {
270 const struct mf_field *field;
271 unsigned int ofs; /* Bit offset. */
272 unsigned int n_bits; /* Number of bits. */
273};
274
1b35df45
BP
275/* Data for some part of an mf_field.
276 *
277 * The data is stored "right-justified". For example, if "union mf_subvalue
278 * value" contains NXM_OF_VLAN_TCI[0..11], then one could access the
279 * corresponding data in value.be16[7] as the bits in the mask htons(0xfff). */
280union mf_subvalue {
281 uint8_t u8[16];
282 ovs_be16 be16[8];
283 ovs_be32 be32[4];
284 ovs_be64 be64[2];
285};
286BUILD_ASSERT_DECL(sizeof(union mf_value) == sizeof (union mf_subvalue));
287
6a885fd0
BP
288/* Finding mf_fields. */
289const struct mf_field *mf_from_id(enum mf_field_id);
290const struct mf_field *mf_from_name(const char *name);
28da1f8f
BP
291const struct mf_field *mf_from_nxm_header(uint32_t nxm_header);
292const struct mf_field *mf_from_nxm_name(const char *nxm_name);
6a885fd0
BP
293
294/* Inspecting wildcarded bits. */
295bool mf_is_all_wild(const struct mf_field *, const struct flow_wildcards *);
296
297bool mf_is_mask_valid(const struct mf_field *, const union mf_value *mask);
298void mf_get_mask(const struct mf_field *, const struct flow_wildcards *,
299 union mf_value *mask);
300
301/* Prerequisites. */
302bool mf_are_prereqs_ok(const struct mf_field *, const struct flow *);
81a76618 303void mf_force_prereqs(const struct mf_field *, struct match *);
6a885fd0
BP
304
305/* Field values. */
306bool mf_is_value_valid(const struct mf_field *, const union mf_value *value);
307
308void mf_get_value(const struct mf_field *, const struct flow *,
309 union mf_value *value);
310void mf_set_value(const struct mf_field *, const union mf_value *value,
81a76618 311 struct match *);
28da1f8f
BP
312void mf_set_flow_value(const struct mf_field *, const union mf_value *value,
313 struct flow *);
ccbe50f8 314bool mf_is_zero(const struct mf_field *, const struct flow *);
6a885fd0 315
81a76618 316void mf_get(const struct mf_field *, const struct match *,
6a885fd0
BP
317 union mf_value *value, union mf_value *mask);
318void mf_set(const struct mf_field *,
319 const union mf_value *value, const union mf_value *mask,
81a76618 320 struct match *);
6a885fd0 321
81a76618 322void mf_set_wild(const struct mf_field *, struct match *);
6a885fd0
BP
323
324void mf_random_value(const struct mf_field *, union mf_value *value);
325
816fd533 326/* Subfields. */
9bab681f
IY
327void mf_write_subfield_flow(const struct mf_subfield *,
328 const union mf_subvalue *, struct flow *);
1b35df45 329void mf_write_subfield(const struct mf_subfield *, const union mf_subvalue *,
81a76618 330 struct match *);
1b35df45
BP
331
332void mf_read_subfield(const struct mf_subfield *, const struct flow *,
333 union mf_subvalue *);
816fd533
BP
334uint64_t mf_get_subfield(const struct mf_subfield *, const struct flow *);
335
1b35df45 336
816fd533
BP
337void mf_format_subfield(const struct mf_subfield *, struct ds *);
338char *mf_parse_subfield__(struct mf_subfield *sf, const char **s);
339const char *mf_parse_subfield(struct mf_subfield *, const char *);
340
341enum ofperr mf_check_src(const struct mf_subfield *, const struct flow *);
342enum ofperr mf_check_dst(const struct mf_subfield *, const struct flow *);
343
6a885fd0
BP
344/* Parsing and formatting. */
345char *mf_parse(const struct mf_field *, const char *,
346 union mf_value *value, union mf_value *mask);
347char *mf_parse_value(const struct mf_field *, const char *, union mf_value *);
348void mf_format(const struct mf_field *,
349 const union mf_value *value, const union mf_value *mask,
350 struct ds *);
9bab681f 351void mf_format_subvalue(const union mf_subvalue *subvalue, struct ds *s);
6a885fd0
BP
352
353#endif /* meta-flow.h */