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