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