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"
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 {
36 MFF_TUN_ID, /* be64 */
37 MFF_TUN_SRC, /* be32 */
38 MFF_TUN_DST, /* be32 */
39 MFF_TUN_FLAGS, /* be16 */
42 MFF_METADATA, /* be64 */
43 MFF_IN_PORT, /* be16 */
44 MFF_IN_PORT_OXM, /* be32 */
45 MFF_SKB_PRIORITY, /* be32 */
46 MFF_PKT_MARK, /* be32 */
74 MFF_ETH_SRC, /* mac */
75 MFF_ETH_DST, /* mac */
76 MFF_ETH_TYPE, /* be16 */
78 MFF_VLAN_TCI, /* be16 */
79 MFF_DL_VLAN, /* be16 (OpenFlow 1.0 compatibility) */
80 MFF_VLAN_VID, /* be16 (OpenFlow 1.2 compatibility) */
81 MFF_DL_VLAN_PCP, /* u8 (OpenFlow 1.0 compatibility) */
82 MFF_VLAN_PCP, /* be16 (OpenFlow 1.2 compatibility) */
85 MFF_MPLS_LABEL, /* be32 */
87 MFF_MPLS_BOS, /* u8 */
90 MFF_IPV4_SRC, /* be32 */
91 MFF_IPV4_DST, /* be32 */
93 MFF_IPV6_SRC, /* ipv6 */
94 MFF_IPV6_DST, /* ipv6 */
95 MFF_IPV6_LABEL, /* be32 */
97 /* The IPv4/IPv6 DSCP field has two different views:
99 * - MFF_IP_DSCP has the DSCP in bits 2-7, their bit positions in the
100 * IPv4 and IPv6 "traffic class" field, as used in OpenFlow 1.0 and 1.1
101 * flow format and in NXM's NXM_OF_IP_TOS
103 * - MFF_IP_DSCP has the DSCP in bits 0-5, shifted right two bits from
104 * their positions in the IPv4 and IPv6 "traffic class" field, as used
105 * in OpenFlow 1.2+ OXM's OXM_OF_IP_DSCP. */
106 MFF_IP_PROTO, /* u8 (used for IPv4 or IPv6) */
107 MFF_IP_DSCP, /* u8 (used for IPv4 or IPv6) */
108 MFF_IP_DSCP_SHIFTED, /* u8 (used for IPv4 or IPv6) (OF1.2 compat) */
109 MFF_IP_ECN, /* u8 (used for IPv4 or IPv6) */
110 MFF_IP_TTL, /* u8 (used for IPv4 or IPv6) */
111 MFF_IP_FRAG, /* u8 (used for IPv4 or IPv6) */
113 MFF_ARP_OP, /* be16 */
114 MFF_ARP_SPA, /* be32 */
115 MFF_ARP_TPA, /* be32 */
116 MFF_ARP_SHA, /* mac */
117 MFF_ARP_THA, /* mac */
120 MFF_TCP_SRC, /* be16 (used for IPv4 or IPv6) */
121 MFF_TCP_DST, /* be16 (used for IPv4 or IPv6) */
122 MFF_TCP_FLAGS, /* be16, 12 bits (4 MSB zeroed,
123 * used for IPv4 or IPv6) */
125 MFF_UDP_SRC, /* be16 (used for IPv4 or IPv6) */
126 MFF_UDP_DST, /* be16 (used for IPv4 or IPv6) */
128 MFF_SCTP_SRC, /* be16 (used for IPv4 or IPv6) */
129 MFF_SCTP_DST, /* be16 (used for IPv4 or IPv6) */
131 MFF_ICMPV4_TYPE, /* u8 */
132 MFF_ICMPV4_CODE, /* u8 */
134 MFF_ICMPV6_TYPE, /* u8 */
135 MFF_ICMPV6_CODE, /* u8 */
137 /* ICMPv6 Neighbor Discovery. */
138 MFF_ND_TARGET, /* ipv6 */
139 MFF_ND_SLL, /* mac */
140 MFF_ND_TLL, /* mac */
145 /* Use this macro as CASE_MFF_REGS: in a switch statement to choose all of the
148 # define CASE_MFF_REGS \
150 #elif FLOW_N_REGS == 2
151 # define CASE_MFF_REGS \
152 case MFF_REG0: case MFF_REG1
153 #elif FLOW_N_REGS == 3
154 # define CASE_MFF_REGS \
155 case MFF_REG0: case MFF_REG1: case MFF_REG2
156 #elif FLOW_N_REGS == 4
157 # define CASE_MFF_REGS \
158 case MFF_REG0: case MFF_REG1: case MFF_REG2: case MFF_REG3
159 #elif FLOW_N_REGS == 5
160 # define CASE_MFF_REGS \
161 case MFF_REG0: case MFF_REG1: case MFF_REG2: case MFF_REG3: \
163 #elif FLOW_N_REGS == 6
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
167 #elif FLOW_N_REGS == 7
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
171 #elif FLOW_N_REGS == 8
172 # define CASE_MFF_REGS \
173 case MFF_REG0: case MFF_REG1: case MFF_REG2: case MFF_REG3: \
174 case MFF_REG4: case MFF_REG5: case MFF_REG6: case MFF_REG7
179 /* Prerequisites for matching a field.
181 * A field may only be matched if the correct lower-level protocols are also
182 * matched. For example, the TCP port may be matched only if the Ethernet type
183 * matches ETH_TYPE_IP and the IP protocol matches IPPROTO_TCP. */
184 enum OVS_PACKED_ENUM mf_prereqs {
187 /* L2 requirements. */
194 /* L2.5 requirements. */
197 /* L2+L3 requirements. */
198 MFP_TCP, /* On IPv4 or IPv6. */
199 MFP_UDP, /* On IPv4 or IPv6. */
200 MFP_SCTP, /* On IPv4 or IPv6. */
204 /* L2+L3+L4 requirements. */
210 /* Forms of partial-field masking allowed for a field.
212 * Every field may be masked as a whole. */
213 enum OVS_PACKED_ENUM mf_maskable {
214 MFM_NONE, /* No sub-field masking. */
215 MFM_FULLY, /* Every bit is individually maskable. */
218 /* How to format or parse a field's value. */
219 enum OVS_PACKED_ENUM mf_string {
222 * The particular MFS_* constant sets the output format. On input, either
223 * decimal or hexadecimal (prefixed with 0x) is accepted. */
231 MFS_OFP_PORT, /* An OpenFlow port number or name. */
232 MFS_OFP_PORT_OXM, /* An OpenFlow port number or name (32-bit). */
233 MFS_FRAG, /* no, yes, first, later, not_later */
234 MFS_TNL_FLAGS, /* FLOW_TNL_F_* flags */
235 MFS_TCP_FLAGS, /* TCP_* flags */
239 /* Identification. */
240 enum mf_field_id id; /* MFF_*. */
241 const char *name; /* Name of this field, e.g. "eth_type". */
242 const char *extra_name; /* Alternate name, e.g. "dl_type", or NULL. */
246 * Most fields have n_bytes * 8 == n_bits. There are a few exceptions:
248 * - "dl_vlan" is 2 bytes but only 12 bits.
249 * - "dl_vlan_pcp" is 1 byte but only 3 bits.
250 * - "is_frag" is 1 byte but only 2 bits.
251 * - "ipv6_label" is 4 bytes but only 20 bits.
252 * - "mpls_label" is 4 bytes but only 20 bits.
253 * - "mpls_tc" is 1 byte but only 3 bits.
254 * - "mpls_bos" is 1 byte but only 1 bit.
256 unsigned int n_bytes; /* Width of the field in bytes. */
257 unsigned int n_bits; /* Number of significant bits in field. */
260 enum mf_maskable maskable;
261 enum mf_string string;
262 enum mf_prereqs prereqs;
263 bool writable; /* May be written by actions? */
265 /* NXM and OXM properties.
267 * There are the following possibilities for these members for a given
270 * - Neither NXM nor OXM defines such a field: these members will all be
273 * - NXM and OXM both define such a field: nxm_header and oxm_header will
274 * both be nonzero and different, similarly for nxm_name and oxm_name.
276 * - Only NXM or only OXM defines such a field: nxm_header and oxm_header
277 * will both have the same value (either an OXM_* or NXM_* value) and
278 * similarly for nxm_name and oxm_name.
280 * Thus, 'nxm_header' is the appropriate header to use when outputting an
281 * NXM formatted match, since it will be an NXM_* constant when possible
282 * for compatibility with OpenFlow implementations that expect that, with
283 * OXM_* constants used for fields that OXM adds. Conversely, 'oxm_header'
284 * is the header to use when outputting an OXM formatted match. */
285 uint32_t nxm_header; /* An NXM_* (or OXM_*) constant. */
286 const char *nxm_name; /* The nxm_header constant's name. */
287 uint32_t oxm_header; /* An OXM_* (or NXM_*) constant. */
288 const char *oxm_name; /* The oxm_header constant's name */
291 * NXM and OXM are extensible, allowing later extensions to be sent in
292 * earlier protocol versions, so this does not necessarily correspond to
293 * the OpenFlow protocol version the field was introduced in.
294 * Also, some field types are tranparently mapped to each other via the
295 * struct flow (like vlan and dscp/tos fields), so each variant supports
297 enum ofputil_protocol usable_protocols; /* If fully/cidr masked. */
298 /* If partially/non-cidr masked. */
299 enum ofputil_protocol usable_protocols_bitwise;
301 int flow_be32ofs; /* Field's be32 offset in "struct flow", if prefix tree
302 * lookup is supported for the field, or -1. */
305 /* The representation of a field's value. */
307 struct in6_addr ipv6;
308 uint8_t mac[ETH_ADDR_LEN];
314 BUILD_ASSERT_DECL(sizeof(union mf_value) == 16);
316 #define MF_EXACT_MASK_INITIALIZER { IN6ADDR_EXACT_INIT }
318 /* Part of a field. */
320 const struct mf_field *field;
321 unsigned int ofs; /* Bit offset. */
322 unsigned int n_bits; /* Number of bits. */
325 /* Data for some part of an mf_field.
327 * The data is stored "right-justified". For example, if "union mf_subvalue
328 * value" contains NXM_OF_VLAN_TCI[0..11], then one could access the
329 * corresponding data in value.be16[7] as the bits in the mask htons(0xfff). */
336 BUILD_ASSERT_DECL(sizeof(union mf_value) == sizeof (union mf_subvalue));
338 /* Finding mf_fields. */
339 const struct mf_field *mf_from_name(const char *name);
340 const struct mf_field *mf_from_nxm_header(uint32_t nxm_header);
341 const struct mf_field *mf_from_nxm_name(const char *nxm_name);
343 static inline const struct mf_field *
344 mf_from_id(enum mf_field_id id)
346 extern const struct mf_field mf_fields[MFF_N_IDS];
347 ovs_assert((unsigned int) id < MFF_N_IDS);
348 return &mf_fields[id];
351 /* Inspecting wildcarded bits. */
352 bool mf_is_all_wild(const struct mf_field *, const struct flow_wildcards *);
354 bool mf_is_mask_valid(const struct mf_field *, const union mf_value *mask);
355 void mf_get_mask(const struct mf_field *, const struct flow_wildcards *,
356 union mf_value *mask);
359 bool mf_are_prereqs_ok(const struct mf_field *, const struct flow *);
360 void mf_mask_field_and_prereqs(const struct mf_field *, struct flow *mask);
363 bool mf_is_value_valid(const struct mf_field *, const union mf_value *value);
365 void mf_get_value(const struct mf_field *, const struct flow *,
366 union mf_value *value);
367 void mf_set_value(const struct mf_field *, const union mf_value *value,
369 void mf_set_flow_value(const struct mf_field *, const union mf_value *value,
371 bool mf_is_zero(const struct mf_field *, const struct flow *);
372 void mf_mask_field(const struct mf_field *, struct flow *);
374 void mf_get(const struct mf_field *, const struct match *,
375 union mf_value *value, union mf_value *mask);
377 /* Returns the set of usable protocols. */
378 enum ofputil_protocol mf_set(const struct mf_field *,
379 const union mf_value *value,
380 const union mf_value *mask,
383 void mf_set_wild(const struct mf_field *, struct match *);
386 void mf_write_subfield_flow(const struct mf_subfield *,
387 const union mf_subvalue *, struct flow *);
388 void mf_write_subfield(const struct mf_subfield *, const union mf_subvalue *,
391 void mf_read_subfield(const struct mf_subfield *, const struct flow *,
392 union mf_subvalue *);
393 uint64_t mf_get_subfield(const struct mf_subfield *, const struct flow *);
396 void mf_format_subfield(const struct mf_subfield *, struct ds *);
397 char *mf_parse_subfield__(struct mf_subfield *sf, const char **s)
399 char *mf_parse_subfield(struct mf_subfield *, const char *s)
402 enum ofperr mf_check_src(const struct mf_subfield *, const struct flow *);
403 enum ofperr mf_check_dst(const struct mf_subfield *, const struct flow *);
405 /* Parsing and formatting. */
406 char *mf_parse(const struct mf_field *, const char *,
407 union mf_value *value, union mf_value *mask);
408 char *mf_parse_value(const struct mf_field *, const char *, union mf_value *);
409 void mf_format(const struct mf_field *,
410 const union mf_value *value, const union mf_value *mask,
412 void mf_format_subvalue(const union mf_subvalue *subvalue, struct ds *s);
414 #endif /* meta-flow.h */