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