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