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