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