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_IN_PORT, /* be16 */
66 MFF_ETH_SRC, /* mac */
67 MFF_ETH_DST, /* mac */
68 MFF_ETH_TYPE, /* be16 */
70 MFF_VLAN_TCI, /* be16 */
71 MFF_VLAN_VID, /* be16 */
72 MFF_VLAN_PCP, /* u8 */
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 /* Prerequisites for matching a field.
117 * A field may only be matched if the correct lower-level protocols are also
118 * matched. For example, the TCP port may be matched only if the Ethernet type
119 * matches ETH_TYPE_IP and the IP protocol matches IPPROTO_TCP. */
123 /* L2 requirements. */
129 /* L2+L3 requirements. */
130 MFP_TCP, /* On IPv4 or IPv6. */
131 MFP_UDP, /* On IPv4 or IPv6. */
135 /* L2+L3+L4 requirements. */
141 /* Forms of partial-field masking allowed for a field.
143 * Every field may be masked as a whole. */
145 MFM_NONE, /* No sub-field masking. */
146 MFM_FULLY, /* Every bit is individually maskable. */
149 /* How to format or parse a field's value. */
153 * The particular MFS_* constant sets the output format. On input, either
154 * decimal or hexadecimal (prefixed with 0x) is accepted. */
162 MFS_OFP_PORT, /* An OpenFlow port number or name. */
163 MFS_FRAG /* no, yes, first, later, not_later */
167 /* Identification. */
168 enum mf_field_id id; /* MFF_*. */
169 const char *name; /* Name of this field, e.g. "eth_type". */
170 const char *extra_name; /* Alternate name, e.g. "dl_type", or NULL. */
174 * Most fields have n_bytes * 8 == n_bits. There are a few exceptions:
176 * - "dl_vlan" is 2 bytes but only 12 bits.
177 * - "dl_vlan_pcp" is 1 byte but only 3 bits.
178 * - "is_frag" is 1 byte but only 2 bits.
179 * - "ipv6_label" is 4 bytes but only 20 bits.
181 unsigned int n_bytes; /* Width of the field in bytes. */
182 unsigned int n_bits; /* Number of significant bits in field. */
185 enum mf_maskable maskable;
186 flow_wildcards_t fww_bit; /* Either 0 or exactly one FWW_* bit. */
187 enum mf_string string;
188 enum mf_prereqs prereqs;
189 bool writable; /* May be written by actions? */
193 * A few "mf_field"s don't correspond to NXM fields. Those have 0 and
194 * NULL for the following members, respectively. */
195 uint32_t nxm_header; /* An NXM_* constant (a few fields have 0). */
196 const char *nxm_name; /* The "NXM_*" constant's name. */
199 uint32_t oxm_header; /* Field id in the OXM basic class,
201 * Ignored if oxm_name is NULL */
202 const char *oxm_name; /* The OXM_* constant's name,
203 * NULL if the field is not present
204 * in the OXM basic class */
208 /* The representation of a field's value. */
214 uint8_t mac[ETH_ADDR_LEN];
215 struct in6_addr ipv6;
217 BUILD_ASSERT_DECL(sizeof(union mf_value) == 16);
219 /* Part of a field. */
221 const struct mf_field *field;
222 unsigned int ofs; /* Bit offset. */
223 unsigned int n_bits; /* Number of bits. */
226 /* Data for some part of an mf_field.
228 * The data is stored "right-justified". For example, if "union mf_subvalue
229 * value" contains NXM_OF_VLAN_TCI[0..11], then one could access the
230 * corresponding data in value.be16[7] as the bits in the mask htons(0xfff). */
237 BUILD_ASSERT_DECL(sizeof(union mf_value) == sizeof (union mf_subvalue));
239 /* Finding mf_fields. */
240 const struct mf_field *mf_from_id(enum mf_field_id);
241 const struct mf_field *mf_from_name(const char *name);
242 const struct mf_field *mf_from_nxm_header(uint32_t nxm_header);
243 const struct mf_field *mf_from_nxm_name(const char *nxm_name);
245 /* Inspecting wildcarded bits. */
246 bool mf_is_all_wild(const struct mf_field *, const struct flow_wildcards *);
248 bool mf_is_mask_valid(const struct mf_field *, const union mf_value *mask);
249 void mf_get_mask(const struct mf_field *, const struct flow_wildcards *,
250 union mf_value *mask);
253 bool mf_are_prereqs_ok(const struct mf_field *, const struct flow *);
254 void mf_force_prereqs(const struct mf_field *, struct cls_rule *);
257 bool mf_is_value_valid(const struct mf_field *, const union mf_value *value);
259 void mf_get_value(const struct mf_field *, const struct flow *,
260 union mf_value *value);
261 void mf_set_value(const struct mf_field *, const union mf_value *value,
263 void mf_set_flow_value(const struct mf_field *, const union mf_value *value,
265 bool mf_is_zero(const struct mf_field *, const struct flow *);
267 void mf_get(const struct mf_field *, const struct cls_rule *,
268 union mf_value *value, union mf_value *mask);
269 void mf_set(const struct mf_field *,
270 const union mf_value *value, const union mf_value *mask,
273 void mf_set_wild(const struct mf_field *, struct cls_rule *);
275 void mf_random_value(const struct mf_field *, union mf_value *value);
278 void mf_write_subfield(const struct mf_subfield *, const union mf_subvalue *,
280 void mf_set_subfield(const struct mf_subfield *, uint64_t value,
282 void mf_set_subfield_value(const struct mf_subfield *, uint64_t value,
285 void mf_read_subfield(const struct mf_subfield *, const struct flow *,
286 union mf_subvalue *);
287 uint64_t mf_get_subfield(const struct mf_subfield *, const struct flow *);
290 void mf_format_subfield(const struct mf_subfield *, struct ds *);
291 char *mf_parse_subfield__(struct mf_subfield *sf, const char **s);
292 const char *mf_parse_subfield(struct mf_subfield *, const char *);
294 enum ofperr mf_check_src(const struct mf_subfield *, const struct flow *);
295 enum ofperr mf_check_dst(const struct mf_subfield *, const struct flow *);
297 /* Parsing and formatting. */
298 char *mf_parse(const struct mf_field *, const char *,
299 union mf_value *value, union mf_value *mask);
300 char *mf_parse_value(const struct mf_field *, const char *, union mf_value *);
301 void mf_format(const struct mf_field *,
302 const union mf_value *value, const union mf_value *mask,
305 #endif /* meta-flow.h */