2 * Copyright (c) 2011, 2012, 2013 Nicira, Inc.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 #include <sys/types.h>
21 #include <netinet/in.h>
22 #include <netinet/ip6.h>
24 #include "ofp-errors.h"
30 /* The comment on each of these indicates the member in "union mf_value" used
31 * to represent its value. */
34 MFF_TUN_ID, /* be64 */
35 MFF_TUN_SRC, /* be32 */
36 MFF_TUN_DST, /* be32 */
37 MFF_TUN_FLAGS, /* be16 */
40 MFF_METADATA, /* be64 */
41 MFF_IN_PORT, /* be16 */
42 MFF_IN_PORT_OXM, /* be32 */
43 MFF_SKB_PRIORITY, /* be32 */
44 MFF_SKB_MARK, /* be32 */
72 MFF_ETH_SRC, /* mac */
73 MFF_ETH_DST, /* mac */
74 MFF_ETH_TYPE, /* be16 */
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) */
83 MFF_MPLS_LABEL, /* be32 */
85 MFF_MPLS_BOS, /* u8 */
88 MFF_IPV4_SRC, /* be32 */
89 MFF_IPV4_DST, /* be32 */
91 MFF_IPV6_SRC, /* ipv6 */
92 MFF_IPV6_DST, /* ipv6 */
93 MFF_IPV6_LABEL, /* be32 */
95 /* The IPv4/IPv6 DSCP field has two different views:
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
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) */
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 */
118 MFF_TCP_SRC, /* be16 (used for IPv4 or IPv6) */
119 MFF_TCP_DST, /* be16 (used for IPv4 or IPv6) */
121 MFF_UDP_SRC, /* be16 (used for IPv4 or IPv6) */
122 MFF_UDP_DST, /* be16 (used for IPv4 or IPv6) */
124 MFF_ICMPV4_TYPE, /* u8 */
125 MFF_ICMPV4_CODE, /* u8 */
127 MFF_ICMPV6_TYPE, /* u8 */
128 MFF_ICMPV6_CODE, /* u8 */
130 /* ICMPv6 Neighbor Discovery. */
131 MFF_ND_TARGET, /* ipv6 */
132 MFF_ND_SLL, /* mac */
133 MFF_ND_TLL, /* mac */
138 /* Use this macro as CASE_MFF_REGS: in a switch statement to choose all of the
141 # define CASE_MFF_REGS \
143 #elif FLOW_N_REGS == 2
144 # define CASE_MFF_REGS \
145 case MFF_REG0: case MFF_REG1
146 #elif FLOW_N_REGS == 3
147 # define CASE_MFF_REGS \
148 case MFF_REG0: case MFF_REG1: case MFF_REG2
149 #elif FLOW_N_REGS == 4
150 # define CASE_MFF_REGS \
151 case MFF_REG0: case MFF_REG1: case MFF_REG2: case MFF_REG3
152 #elif FLOW_N_REGS == 5
153 # define CASE_MFF_REGS \
154 case MFF_REG0: case MFF_REG1: case MFF_REG2: case MFF_REG3: \
156 #elif FLOW_N_REGS == 6
157 # define CASE_MFF_REGS \
158 case MFF_REG0: case MFF_REG1: case MFF_REG2: case MFF_REG3: \
159 case MFF_REG4: case MFF_REG5
160 #elif FLOW_N_REGS == 7
161 # define CASE_MFF_REGS \
162 case MFF_REG0: case MFF_REG1: case MFF_REG2: case MFF_REG3: \
163 case MFF_REG4: case MFF_REG5: case MFF_REG6
164 #elif FLOW_N_REGS == 8
165 # define CASE_MFF_REGS \
166 case MFF_REG0: case MFF_REG1: case MFF_REG2: case MFF_REG3: \
167 case MFF_REG4: case MFF_REG5: case MFF_REG6: case MFF_REG7
172 /* Prerequisites for matching a field.
174 * A field may only be matched if the correct lower-level protocols are also
175 * matched. For example, the TCP port may be matched only if the Ethernet type
176 * matches ETH_TYPE_IP and the IP protocol matches IPPROTO_TCP. */
180 /* L2 requirements. */
187 /* L2.5 requirements. */
190 /* L2+L3 requirements. */
191 MFP_TCP, /* On IPv4 or IPv6. */
192 MFP_UDP, /* On IPv4 or IPv6. */
196 /* L2+L3+L4 requirements. */
202 /* Forms of partial-field masking allowed for a field.
204 * Every field may be masked as a whole. */
206 MFM_NONE, /* No sub-field masking. */
207 MFM_FULLY, /* Every bit is individually maskable. */
210 /* How to format or parse a field's value. */
214 * The particular MFS_* constant sets the output format. On input, either
215 * decimal or hexadecimal (prefixed with 0x) is accepted. */
223 MFS_OFP_PORT, /* An OpenFlow port number or name. */
224 MFS_OFP_PORT_OXM, /* An OpenFlow port number or name (32-bit). */
225 MFS_FRAG, /* no, yes, first, later, not_later */
226 MFS_TNL_FLAGS, /* FLOW_TNL_F_* flags */
230 /* Identification. */
231 enum mf_field_id id; /* MFF_*. */
232 const char *name; /* Name of this field, e.g. "eth_type". */
233 const char *extra_name; /* Alternate name, e.g. "dl_type", or NULL. */
237 * Most fields have n_bytes * 8 == n_bits. There are a few exceptions:
239 * - "dl_vlan" is 2 bytes but only 12 bits.
240 * - "dl_vlan_pcp" is 1 byte but only 3 bits.
241 * - "is_frag" is 1 byte but only 2 bits.
242 * - "ipv6_label" is 4 bytes but only 20 bits.
243 * - "mpls_label" is 4 bytes but only 20 bits.
244 * - "mpls_tc" is 1 byte but only 3 bits.
245 * - "mpls_bos" is 1 byte but only 1 bit.
247 unsigned int n_bytes; /* Width of the field in bytes. */
248 unsigned int n_bits; /* Number of significant bits in field. */
251 enum mf_maskable maskable;
252 enum mf_string string;
253 enum mf_prereqs prereqs;
254 bool writable; /* May be written by actions? */
256 /* NXM and OXM properties.
258 * There are the following possibilities for these members for a given
261 * - Neither NXM nor OXM defines such a field: these members will all be
264 * - NXM and OXM both define such a field: nxm_header and oxm_header will
265 * both be nonzero and different, similarly for nxm_name and oxm_name.
267 * - Only NXM or only OXM defines such a field: nxm_header and oxm_header
268 * will both have the same value (either an OXM_* or NXM_* value) and
269 * similarly for nxm_name and oxm_name.
271 * Thus, 'nxm_header' is the appropriate header to use when outputting an
272 * NXM formatted match, since it will be an NXM_* constant when possible
273 * for compatibility with OpenFlow implementations that expect that, with
274 * OXM_* constants used for fields that OXM adds. Conversely, 'oxm_header'
275 * is the header to use when outputting an OXM formatted match. */
276 uint32_t nxm_header; /* An NXM_* (or OXM_*) constant. */
277 const char *nxm_name; /* The nxm_header constant's name. */
278 uint32_t oxm_header; /* An OXM_* (or NXM_*) constant. */
279 const char *oxm_name; /* The oxm_header constant's name */
282 /* The representation of a field's value. */
288 uint8_t mac[ETH_ADDR_LEN];
289 struct in6_addr ipv6;
291 BUILD_ASSERT_DECL(sizeof(union mf_value) == 16);
293 /* Part of a field. */
295 const struct mf_field *field;
296 unsigned int ofs; /* Bit offset. */
297 unsigned int n_bits; /* Number of bits. */
300 /* Data for some part of an mf_field.
302 * The data is stored "right-justified". For example, if "union mf_subvalue
303 * value" contains NXM_OF_VLAN_TCI[0..11], then one could access the
304 * corresponding data in value.be16[7] as the bits in the mask htons(0xfff). */
311 BUILD_ASSERT_DECL(sizeof(union mf_value) == sizeof (union mf_subvalue));
313 /* Finding mf_fields. */
314 const struct mf_field *mf_from_id(enum mf_field_id);
315 const struct mf_field *mf_from_name(const char *name);
316 const struct mf_field *mf_from_nxm_header(uint32_t nxm_header);
317 const struct mf_field *mf_from_nxm_name(const char *nxm_name);
319 /* Inspecting wildcarded bits. */
320 bool mf_is_all_wild(const struct mf_field *, const struct flow_wildcards *);
322 bool mf_is_mask_valid(const struct mf_field *, const union mf_value *mask);
323 void mf_get_mask(const struct mf_field *, const struct flow_wildcards *,
324 union mf_value *mask);
327 bool mf_are_prereqs_ok(const struct mf_field *, const struct flow *);
328 void mf_force_prereqs(const struct mf_field *, struct match *);
331 bool mf_is_value_valid(const struct mf_field *, const union mf_value *value);
333 void mf_get_value(const struct mf_field *, const struct flow *,
334 union mf_value *value);
335 void mf_set_value(const struct mf_field *, const union mf_value *value,
337 void mf_set_flow_value(const struct mf_field *, const union mf_value *value,
339 bool mf_is_zero(const struct mf_field *, const struct flow *);
341 void mf_get(const struct mf_field *, const struct match *,
342 union mf_value *value, union mf_value *mask);
343 void mf_set(const struct mf_field *,
344 const union mf_value *value, const union mf_value *mask,
347 void mf_set_wild(const struct mf_field *, struct match *);
349 void mf_random_value(const struct mf_field *, union mf_value *value);
352 void mf_write_subfield_flow(const struct mf_subfield *,
353 const union mf_subvalue *, struct flow *);
354 void mf_write_subfield(const struct mf_subfield *, const union mf_subvalue *,
357 void mf_read_subfield(const struct mf_subfield *, const struct flow *,
358 union mf_subvalue *);
359 uint64_t mf_get_subfield(const struct mf_subfield *, const struct flow *);
362 void mf_format_subfield(const struct mf_subfield *, struct ds *);
363 char *mf_parse_subfield__(struct mf_subfield *sf, const char **s);
364 const char *mf_parse_subfield(struct mf_subfield *, const char *);
366 enum ofperr mf_check_src(const struct mf_subfield *, const struct flow *);
367 enum ofperr mf_check_dst(const struct mf_subfield *, const struct flow *);
369 /* Parsing and formatting. */
370 char *mf_parse(const struct mf_field *, const char *,
371 union mf_value *value, union mf_value *mask);
372 char *mf_parse_value(const struct mf_field *, const char *, union mf_value *);
373 void mf_format(const struct mf_field *,
374 const union mf_value *value, const union mf_value *mask,
376 void mf_format_subvalue(const union mf_subvalue *subvalue, struct ds *s);
378 #endif /* meta-flow.h */