2 * Copyright (c) 2011, 2012 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_METADATA, /* be64 */
36 MFF_IN_PORT, /* be16 */
64 MFF_ETH_SRC, /* mac */
65 MFF_ETH_DST, /* mac */
66 MFF_ETH_TYPE, /* be16 */
68 MFF_VLAN_TCI, /* be16 */
69 MFF_DL_VLAN, /* be16 (OpenFlow 1.0 compatibility) */
70 MFF_VLAN_VID, /* be16 (OpenFlow 1.2 compatibility) */
71 MFF_DL_VLAN_PCP, /* u8 (OpenFlow 1.0 compatibility) */
72 MFF_VLAN_PCP, /* be16 (OpenFlow 1.2 compatibility) */
75 MFF_IPV4_SRC, /* be32 */
76 MFF_IPV4_DST, /* be32 */
78 MFF_IPV6_SRC, /* ipv6 */
79 MFF_IPV6_DST, /* ipv6 */
80 MFF_IPV6_LABEL, /* be32 */
82 MFF_IP_PROTO, /* u8 (used for IPv4 or IPv6) */
83 MFF_IP_DSCP, /* u8 (used for IPv4 or IPv6) */
84 MFF_IP_ECN, /* u8 (used for IPv4 or IPv6) */
85 MFF_IP_TTL, /* u8 (used for IPv4 or IPv6) */
86 MFF_IP_FRAG, /* u8 (used for IPv4 or IPv6) */
88 MFF_ARP_OP, /* be16 */
89 MFF_ARP_SPA, /* be32 */
90 MFF_ARP_TPA, /* be32 */
91 MFF_ARP_SHA, /* mac */
92 MFF_ARP_THA, /* mac */
95 MFF_TCP_SRC, /* be16 (used for IPv4 or IPv6) */
96 MFF_TCP_DST, /* be16 (used for IPv4 or IPv6) */
98 MFF_UDP_SRC, /* be16 (used for IPv4 or IPv6) */
99 MFF_UDP_DST, /* be16 (used for IPv4 or IPv6) */
101 MFF_ICMPV4_TYPE, /* u8 */
102 MFF_ICMPV4_CODE, /* u8 */
104 MFF_ICMPV6_TYPE, /* u8 */
105 MFF_ICMPV6_CODE, /* u8 */
107 /* ICMPv6 Neighbor Discovery. */
108 MFF_ND_TARGET, /* ipv6 */
109 MFF_ND_SLL, /* mac */
110 MFF_ND_TLL, /* mac */
115 /* Use this macro as CASE_MFF_REGS: in a switch statement to choose all of the
118 # define CASE_MFF_REGS \
120 #elif FLOW_N_REGS == 2
121 # define CASE_MFF_REGS \
122 case MFF_REG0: case MFF_REG1
123 #elif FLOW_N_REGS == 3
124 # define CASE_MFF_REGS \
125 case MFF_REG0: case MFF_REG1: case MFF_REG2
126 #elif FLOW_N_REGS == 4
127 # define CASE_MFF_REGS \
128 case MFF_REG0: case MFF_REG1: case MFF_REG2: case MFF_REG3
129 #elif FLOW_N_REGS == 5
130 # define CASE_MFF_REGS \
131 case MFF_REG0: case MFF_REG1: case MFF_REG2: case MFF_REG3: \
133 #elif FLOW_N_REGS == 6
134 # define CASE_MFF_REGS \
135 case MFF_REG0: case MFF_REG1: case MFF_REG2: case MFF_REG3: \
136 case MFF_REG4: case MFF_REG5
137 #elif FLOW_N_REGS == 7
138 # define CASE_MFF_REGS \
139 case MFF_REG0: case MFF_REG1: case MFF_REG2: case MFF_REG3: \
140 case MFF_REG4: case MFF_REG5: case MFF_REG6
141 #elif FLOW_N_REGS == 8
142 # define CASE_MFF_REGS \
143 case MFF_REG0: case MFF_REG1: case MFF_REG2: case MFF_REG3: \
144 case MFF_REG4: case MFF_REG5: case MFF_REG6: case MFF_REG7
149 /* Prerequisites for matching a field.
151 * A field may only be matched if the correct lower-level protocols are also
152 * matched. For example, the TCP port may be matched only if the Ethernet type
153 * matches ETH_TYPE_IP and the IP protocol matches IPPROTO_TCP. */
157 /* L2 requirements. */
164 /* L2+L3 requirements. */
165 MFP_TCP, /* On IPv4 or IPv6. */
166 MFP_UDP, /* On IPv4 or IPv6. */
170 /* L2+L3+L4 requirements. */
176 /* Forms of partial-field masking allowed for a field.
178 * Every field may be masked as a whole. */
180 MFM_NONE, /* No sub-field masking. */
181 MFM_FULLY, /* Every bit is individually maskable. */
184 /* How to format or parse a field's value. */
188 * The particular MFS_* constant sets the output format. On input, either
189 * decimal or hexadecimal (prefixed with 0x) is accepted. */
197 MFS_OFP_PORT, /* An OpenFlow port number or name. */
198 MFS_FRAG /* no, yes, first, later, not_later */
202 /* Identification. */
203 enum mf_field_id id; /* MFF_*. */
204 const char *name; /* Name of this field, e.g. "eth_type". */
205 const char *extra_name; /* Alternate name, e.g. "dl_type", or NULL. */
209 * Most fields have n_bytes * 8 == n_bits. There are a few exceptions:
211 * - "dl_vlan" is 2 bytes but only 12 bits.
212 * - "dl_vlan_pcp" is 1 byte but only 3 bits.
213 * - "is_frag" is 1 byte but only 2 bits.
214 * - "ipv6_label" is 4 bytes but only 20 bits.
216 unsigned int n_bytes; /* Width of the field in bytes. */
217 unsigned int n_bits; /* Number of significant bits in field. */
220 enum mf_maskable maskable;
221 flow_wildcards_t fww_bit; /* Either 0 or exactly one FWW_* bit. */
222 enum mf_string string;
223 enum mf_prereqs prereqs;
224 bool writable; /* May be written by actions? */
226 /* NXM and OXM properties.
228 * There are the following possibilities for these members for a given
231 * - Neither NXM nor OXM defines such a field: these members will all be
234 * - NXM and OXM both define such a field: nxm_header and oxm_header will
235 * both be nonzero and different, similarly for nxm_name and oxm_name.
237 * - Only NXM or only OXM defines such a field: nxm_header and oxm_header
238 * will both have the same value (either an OXM_* or NXM_* value) and
239 * similarly for nxm_name and oxm_name.
241 * Thus, 'nxm_header' is the appropriate header to use when outputting an
242 * NXM formatted match, since it will be an NXM_* constant when possible
243 * for compatibility with OpenFlow implementations that expect that, with
244 * OXM_* constants used for fields that OXM adds. Conversely, 'oxm_header'
245 * is the header to use when outputting an OXM formatted match. */
246 uint32_t nxm_header; /* An NXM_* (or OXM_*) constant. */
247 const char *nxm_name; /* The nxm_header constant's name. */
248 uint32_t oxm_header; /* An OXM_* (or NXM_*) constant. */
249 const char *oxm_name; /* The oxm_header constant's name */
252 /* The representation of a field's value. */
258 uint8_t mac[ETH_ADDR_LEN];
259 struct in6_addr ipv6;
261 BUILD_ASSERT_DECL(sizeof(union mf_value) == 16);
263 /* Part of a field. */
265 const struct mf_field *field;
266 unsigned int ofs; /* Bit offset. */
267 unsigned int n_bits; /* Number of bits. */
270 /* Data for some part of an mf_field.
272 * The data is stored "right-justified". For example, if "union mf_subvalue
273 * value" contains NXM_OF_VLAN_TCI[0..11], then one could access the
274 * corresponding data in value.be16[7] as the bits in the mask htons(0xfff). */
281 BUILD_ASSERT_DECL(sizeof(union mf_value) == sizeof (union mf_subvalue));
283 /* Finding mf_fields. */
284 const struct mf_field *mf_from_id(enum mf_field_id);
285 const struct mf_field *mf_from_name(const char *name);
286 const struct mf_field *mf_from_nxm_header(uint32_t nxm_header);
287 const struct mf_field *mf_from_nxm_name(const char *nxm_name);
289 /* Inspecting wildcarded bits. */
290 bool mf_is_all_wild(const struct mf_field *, const struct flow_wildcards *);
292 bool mf_is_mask_valid(const struct mf_field *, const union mf_value *mask);
293 void mf_get_mask(const struct mf_field *, const struct flow_wildcards *,
294 union mf_value *mask);
297 bool mf_are_prereqs_ok(const struct mf_field *, const struct flow *);
298 void mf_force_prereqs(const struct mf_field *, struct cls_rule *);
301 bool mf_is_value_valid(const struct mf_field *, const union mf_value *value);
303 void mf_get_value(const struct mf_field *, const struct flow *,
304 union mf_value *value);
305 void mf_set_value(const struct mf_field *, const union mf_value *value,
307 void mf_set_flow_value(const struct mf_field *, const union mf_value *value,
309 bool mf_is_zero(const struct mf_field *, const struct flow *);
311 void mf_get(const struct mf_field *, const struct cls_rule *,
312 union mf_value *value, union mf_value *mask);
313 void mf_set(const struct mf_field *,
314 const union mf_value *value, const union mf_value *mask,
317 void mf_set_wild(const struct mf_field *, struct cls_rule *);
319 void mf_random_value(const struct mf_field *, union mf_value *value);
322 void mf_write_subfield(const struct mf_subfield *, const union mf_subvalue *,
325 void mf_read_subfield(const struct mf_subfield *, const struct flow *,
326 union mf_subvalue *);
327 uint64_t mf_get_subfield(const struct mf_subfield *, const struct flow *);
330 void mf_format_subfield(const struct mf_subfield *, struct ds *);
331 char *mf_parse_subfield__(struct mf_subfield *sf, const char **s);
332 const char *mf_parse_subfield(struct mf_subfield *, const char *);
334 enum ofperr mf_check_src(const struct mf_subfield *, const struct flow *);
335 enum ofperr mf_check_dst(const struct mf_subfield *, const struct flow *);
337 /* Parsing and formatting. */
338 char *mf_parse(const struct mf_field *, const char *,
339 union mf_value *value, union mf_value *mask);
340 char *mf_parse_value(const struct mf_field *, const char *, union mf_value *);
341 void mf_format(const struct mf_field *,
342 const union mf_value *value, const union mf_value *mask,
345 #endif /* meta-flow.h */