]> git.proxmox.com Git - ovs.git/blame - lib/meta-flow.c
nx-match: Use vl_mff_map to parse match field.
[ovs.git] / lib / meta-flow.c
CommitLineData
6a885fd0 1/*
04f48a68 2 * Copyright (c) 2011-2017 Nicira, Inc.
6a885fd0
BP
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#include <config.h>
18
064d7f84 19#include "openvswitch/meta-flow.h"
6a885fd0 20
6a885fd0
BP
21#include <errno.h>
22#include <limits.h>
23#include <netinet/icmp6.h>
24#include <netinet/ip6.h>
25
26#include "classifier.h"
3e8a2ad1 27#include "openvswitch/dynamic-string.h"
178742f9 28#include "nx-match.h"
f4248336 29#include "openvswitch/ofp-util.h"
04f48a68 30#include "ovs-rcu.h"
4663f9e0 31#include "ovs-thread.h"
6a885fd0
BP
32#include "packets.h"
33#include "random.h"
ee89ea7b 34#include "openvswitch/shash.h"
6a885fd0 35#include "socket-util.h"
9558d2a5 36#include "tun-metadata.h"
6a885fd0 37#include "unaligned.h"
86f35fb5 38#include "util.h"
e03c096d 39#include "openvswitch/ofp-errors.h"
e6211adc 40#include "openvswitch/vlog.h"
aafee638 41#include "vl-mff-map.h"
816fd533
BP
42
43VLOG_DEFINE_THIS_MODULE(meta_flow);
6a885fd0 44
13751fd8
JR
45#define FLOW_U32OFS(FIELD) \
46 offsetof(struct flow, FIELD) % 4 ? -1 : offsetof(struct flow, FIELD) / 4
47
6a885fd0
BP
48#define MF_FIELD_SIZES(MEMBER) \
49 sizeof ((union mf_value *)0)->MEMBER, \
50 8 * sizeof ((union mf_value *)0)->MEMBER
51
7f98c44d
JR
52extern const struct mf_field mf_fields[MFF_N_IDS]; /* Silence a warning. */
53
54const struct mf_field mf_fields[MFF_N_IDS] = {
a4ce8b25 55#include "meta-flow.inc"
6a885fd0
BP
56};
57
4663f9e0
BP
58/* Maps from an mf_field's 'name' or 'extra_name' to the mf_field. */
59static struct shash mf_by_name;
28da1f8f 60
816fd533
BP
61/* Rate limit for parse errors. These always indicate a bug in an OpenFlow
62 * controller and so there's not much point in showing a lot of them. */
63static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
64
e3e96509
JR
65#define MF_VALUE_EXACT_8 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
66#define MF_VALUE_EXACT_16 MF_VALUE_EXACT_8, MF_VALUE_EXACT_8
67#define MF_VALUE_EXACT_32 MF_VALUE_EXACT_16, MF_VALUE_EXACT_16
68#define MF_VALUE_EXACT_64 MF_VALUE_EXACT_32, MF_VALUE_EXACT_32
69#define MF_VALUE_EXACT_128 MF_VALUE_EXACT_64, MF_VALUE_EXACT_64
70#define MF_VALUE_EXACT_INITIALIZER { .tun_metadata = { MF_VALUE_EXACT_128 } }
71
72const union mf_value exact_match_mask = MF_VALUE_EXACT_INITIALIZER;
73
4663f9e0 74static void nxm_init(void);
b5e5143b 75
6a885fd0
BP
76/* Returns the field with the given 'name', or a null pointer if no field has
77 * that name. */
78const struct mf_field *
79mf_from_name(const char *name)
80{
4663f9e0 81 nxm_init();
6a885fd0
BP
82 return shash_find_data(&mf_by_name, name);
83}
84
21b2fa61
JR
85/* Returns the field with the given 'name' (which is 'len' bytes long), or a
86 * null pointer if no field has that name. */
87const struct mf_field *
88mf_from_name_len(const char *name, size_t len)
89{
90 nxm_init();
91
92 struct shash_node *node = shash_find_len(&mf_by_name, name, len);
93 return node ? node->data : NULL;
94}
95
28da1f8f 96static void
4663f9e0 97nxm_do_init(void)
28da1f8f 98{
216a32dd 99 int i;
28da1f8f 100
4663f9e0 101 shash_init(&mf_by_name);
216a32dd
JR
102 for (i = 0; i < MFF_N_IDS; i++) {
103 const struct mf_field *mf = &mf_fields[i];
104
105 ovs_assert(mf->id == i); /* Fields must be in the enum order. */
106
4663f9e0
BP
107 shash_add_once(&mf_by_name, mf->name, mf);
108 if (mf->extra_name) {
109 shash_add_once(&mf_by_name, mf->extra_name, mf);
110 }
28da1f8f 111 }
28da1f8f
BP
112}
113
4663f9e0
BP
114static void
115nxm_init(void)
116{
117 static pthread_once_t once = PTHREAD_ONCE_INIT;
118 pthread_once(&once, nxm_do_init);
119}
120
35c1f798
BP
121/* Consider the two value/mask pairs 'a_value/a_mask' and 'b_value/b_mask' as
122 * restrictions on a field's value. Then, this function initializes
123 * 'dst_value/dst_mask' such that it combines the restrictions of both pairs.
124 * This is not always possible, i.e. if one pair insists on a value of 0 in
125 * some bit and the other pair insists on a value of 1 in that bit. This
126 * function returns false in a case where the combined restriction is
127 * impossible (in which case 'dst_value/dst_mask' is not fully initialized),
128 * true otherwise.
129 *
130 * (As usually true for value/mask pairs in OVS, any 1-bit in a value must have
131 * a corresponding 1-bit in its mask.) */
132bool
133mf_subvalue_intersect(const union mf_subvalue *a_value,
134 const union mf_subvalue *a_mask,
135 const union mf_subvalue *b_value,
136 const union mf_subvalue *b_mask,
137 union mf_subvalue *dst_value,
138 union mf_subvalue *dst_mask)
139{
140 for (int i = 0; i < ARRAY_SIZE(a_value->be64); i++) {
141 ovs_be64 av = a_value->be64[i];
142 ovs_be64 am = a_mask->be64[i];
143 ovs_be64 bv = b_value->be64[i];
144 ovs_be64 bm = b_mask->be64[i];
145 ovs_be64 *dv = &dst_value->be64[i];
146 ovs_be64 *dm = &dst_mask->be64[i];
147
148 if ((av ^ bv) & (am & bm)) {
149 return false;
150 }
151 *dv = av | bv;
152 *dm = am | bm;
153 }
154 return true;
155}
156
157/* Returns the "number of bits" in 'v', e.g. 1 if only the lowest-order bit is
158 * set, 2 if the second-lowest-order bit is set, and so on. */
159int
160mf_subvalue_width(const union mf_subvalue *v)
161{
162 return 1 + bitwise_rscan(v, sizeof *v, true, sizeof *v * 8 - 1, -1);
163}
164
165/* For positive 'n', shifts the bits in 'value' 'n' bits to the left, and for
166 * negative 'n', shifts the bits '-n' bits to the right. */
167void
168mf_subvalue_shift(union mf_subvalue *value, int n)
169{
170 if (n) {
171 union mf_subvalue tmp;
172 memset(&tmp, 0, sizeof tmp);
173
174 if (n > 0 && n < 8 * sizeof tmp) {
175 bitwise_copy(value, sizeof *value, 0,
176 &tmp, sizeof tmp, n,
177 8 * sizeof tmp - n);
178 } else if (n < 0 && n > -8 * sizeof tmp) {
179 bitwise_copy(value, sizeof *value, -n,
180 &tmp, sizeof tmp, 0,
181 8 * sizeof tmp + n);
182 }
183 *value = tmp;
184 }
185}
186
77ab5fd2
BP
187/* Appends a formatted representation of 'sv' to 's'. */
188void
189mf_subvalue_format(const union mf_subvalue *sv, struct ds *s)
190{
191 ds_put_hex(s, sv, sizeof *sv);
192}
193
6a885fd0
BP
194/* Returns true if 'wc' wildcards all the bits in field 'mf', false if 'wc'
195 * specifies at least one bit in the field.
196 *
197 * The caller is responsible for ensuring that 'wc' corresponds to a flow that
198 * meets 'mf''s prerequisites. */
199bool
200mf_is_all_wild(const struct mf_field *mf, const struct flow_wildcards *wc)
201{
202 switch (mf->id) {
a79f29f2
AZ
203 case MFF_DP_HASH:
204 return !wc->masks.dp_hash;
205 case MFF_RECIRC_ID:
206 return !wc->masks.recirc_id;
18080541
BP
207 case MFF_CONJ_ID:
208 return !wc->masks.conj_id;
4fe3445a 209 case MFF_TUN_SRC:
0ad90c84 210 return !wc->masks.tunnel.ip_src;
4fe3445a 211 case MFF_TUN_DST:
0ad90c84 212 return !wc->masks.tunnel.ip_dst;
7dad8e9a
TLSC
213 case MFF_TUN_IPV6_SRC:
214 return ipv6_mask_is_any(&wc->masks.tunnel.ipv6_src);
215 case MFF_TUN_IPV6_DST:
216 return ipv6_mask_is_any(&wc->masks.tunnel.ipv6_dst);
0ad90c84 217 case MFF_TUN_ID:
b666962b 218 return !wc->masks.tunnel.tun_id;
4fe3445a 219 case MFF_TUN_TOS:
b666962b 220 return !wc->masks.tunnel.ip_tos;
4fe3445a 221 case MFF_TUN_TTL:
b666962b 222 return !wc->masks.tunnel.ip_ttl;
4fe3445a 223 case MFF_TUN_FLAGS:
b666962b 224 return !(wc->masks.tunnel.flags & FLOW_TNL_PUB_F_MASK);
ac6073e3
MC
225 case MFF_TUN_GBP_ID:
226 return !wc->masks.tunnel.gbp_id;
227 case MFF_TUN_GBP_FLAGS:
228 return !wc->masks.tunnel.gbp_flags;
1cb20095
JG
229 CASE_MFF_TUN_METADATA:
230 return !ULLONG_GET(wc->masks.tunnel.metadata.present.map,
231 mf->id - MFF_TUN_METADATA0);
969fc56c 232 case MFF_METADATA:
26720e24 233 return !wc->masks.metadata;
0bdc4bec 234 case MFF_IN_PORT:
72333065 235 case MFF_IN_PORT_OXM:
4e022ec0 236 return !wc->masks.in_port.ofp_port;
1b567fb9
AA
237 case MFF_SKB_PRIORITY:
238 return !wc->masks.skb_priority;
1362e248
JG
239 case MFF_PKT_MARK:
240 return !wc->masks.pkt_mark;
07659514
JS
241 case MFF_CT_STATE:
242 return !wc->masks.ct_state;
243 case MFF_CT_ZONE:
244 return !wc->masks.ct_zone;
8e53fe8c
JS
245 case MFF_CT_MARK:
246 return !wc->masks.ct_mark;
9daf2348 247 case MFF_CT_LABEL:
2ff8484b 248 return ovs_u128_is_zero(wc->masks.ct_label);
daf4d3c1
JR
249 case MFF_CT_NW_PROTO:
250 return !wc->masks.ct_nw_proto;
251 case MFF_CT_NW_SRC:
252 return !wc->masks.ct_nw_src;
253 case MFF_CT_NW_DST:
254 return !wc->masks.ct_nw_dst;
255 case MFF_CT_TP_SRC:
256 return !wc->masks.ct_tp_src;
257 case MFF_CT_TP_DST:
258 return !wc->masks.ct_tp_dst;
259 case MFF_CT_IPV6_SRC:
260 return ipv6_mask_is_any(&wc->masks.ct_ipv6_src);
261 case MFF_CT_IPV6_DST:
262 return ipv6_mask_is_any(&wc->masks.ct_ipv6_dst);
0d7e2fe4 263 CASE_MFF_REGS:
26720e24 264 return !wc->masks.regs[mf->id - MFF_REG0];
79fe0f46
BP
265 CASE_MFF_XREGS:
266 return !flow_get_xreg(&wc->masks, mf->id - MFF_XREG0);
b23ada8e
JP
267 CASE_MFF_XXREGS: {
268 ovs_u128 value = flow_get_xxreg(&wc->masks, mf->id - MFF_XXREG0);
269 return ovs_u128_is_zero(value);
270 }
c61f3870
BP
271 case MFF_ACTSET_OUTPUT:
272 return !wc->masks.actset_output;
6a885fd0 273
73c0ce34 274 case MFF_ETH_SRC:
26720e24 275 return eth_addr_is_zero(wc->masks.dl_src);
6a885fd0 276 case MFF_ETH_DST:
26720e24 277 return eth_addr_is_zero(wc->masks.dl_dst);
e2170cff 278 case MFF_ETH_TYPE:
26720e24 279 return !wc->masks.dl_type;
6a885fd0 280
e878338b
SH
281 case MFF_ARP_SHA:
282 case MFF_ND_SLL:
26720e24 283 return eth_addr_is_zero(wc->masks.arp_sha);
e878338b
SH
284
285 case MFF_ARP_THA:
286 case MFF_ND_TLL:
26720e24 287 return eth_addr_is_zero(wc->masks.arp_tha);
e878338b 288
6a885fd0 289 case MFF_VLAN_TCI:
26720e24 290 return !wc->masks.vlan_tci;
441c57a9 291 case MFF_DL_VLAN:
26720e24 292 return !(wc->masks.vlan_tci & htons(VLAN_VID_MASK));
cc34bc8c 293 case MFF_VLAN_VID:
26720e24 294 return !(wc->masks.vlan_tci & htons(VLAN_VID_MASK | VLAN_CFI));
441c57a9 295 case MFF_DL_VLAN_PCP:
cc34bc8c 296 case MFF_VLAN_PCP:
26720e24 297 return !(wc->masks.vlan_tci & htons(VLAN_PCP_MASK));
6a885fd0 298
b02475c5 299 case MFF_MPLS_LABEL:
8bfd0fda 300 return !(wc->masks.mpls_lse[0] & htonl(MPLS_LABEL_MASK));
b02475c5 301 case MFF_MPLS_TC:
60df616f 302 return !(wc->masks.mpls_lse[0] & htonl(MPLS_TC_MASK));
b02475c5 303 case MFF_MPLS_BOS:
60df616f 304 return !(wc->masks.mpls_lse[0] & htonl(MPLS_BOS_MASK));
bef3f465
BP
305 case MFF_MPLS_TTL:
306 return !(wc->masks.mpls_lse[0] & htonl(MPLS_TTL_MASK));
b02475c5 307
6a885fd0 308 case MFF_IPV4_SRC:
26720e24 309 return !wc->masks.nw_src;
6a885fd0 310 case MFF_IPV4_DST:
26720e24 311 return !wc->masks.nw_dst;
6a885fd0
BP
312
313 case MFF_IPV6_SRC:
26720e24 314 return ipv6_mask_is_any(&wc->masks.ipv6_src);
6a885fd0 315 case MFF_IPV6_DST:
26720e24 316 return ipv6_mask_is_any(&wc->masks.ipv6_dst);
6a885fd0 317
32455024 318 case MFF_IPV6_LABEL:
26720e24 319 return !wc->masks.ipv6_label;
32455024 320
851d3105 321 case MFF_IP_PROTO:
26720e24 322 return !wc->masks.nw_proto;
5d9499c4 323 case MFF_IP_DSCP:
1638b906 324 case MFF_IP_DSCP_SHIFTED:
26720e24 325 return !(wc->masks.nw_tos & IP_DSCP_MASK);
5d9499c4 326 case MFF_IP_ECN:
26720e24 327 return !(wc->masks.nw_tos & IP_ECN_MASK);
3840c406 328 case MFF_IP_TTL:
26720e24 329 return !wc->masks.nw_ttl;
5d9499c4 330
47284b1f 331 case MFF_ND_TARGET:
26720e24 332 return ipv6_mask_is_any(&wc->masks.nd_target);
47284b1f 333
7257b535 334 case MFF_IP_FRAG:
26720e24 335 return !(wc->masks.nw_frag & FLOW_NW_FRAG_MASK);
7257b535 336
851d3105 337 case MFF_ARP_OP:
26720e24 338 return !wc->masks.nw_proto;
6a885fd0 339 case MFF_ARP_SPA:
26720e24 340 return !wc->masks.nw_src;
6a885fd0 341 case MFF_ARP_TPA:
26720e24 342 return !wc->masks.nw_dst;
6a885fd0 343
73f33563
BP
344 case MFF_TCP_SRC:
345 case MFF_UDP_SRC:
0d56eaf2 346 case MFF_SCTP_SRC:
73f33563
BP
347 case MFF_ICMPV4_TYPE:
348 case MFF_ICMPV6_TYPE:
26720e24 349 return !wc->masks.tp_src;
73f33563
BP
350 case MFF_TCP_DST:
351 case MFF_UDP_DST:
0d56eaf2 352 case MFF_SCTP_DST:
73f33563
BP
353 case MFF_ICMPV4_CODE:
354 case MFF_ICMPV6_CODE:
26720e24 355 return !wc->masks.tp_dst;
dc235f7f
JR
356 case MFF_TCP_FLAGS:
357 return !wc->masks.tcp_flags;
73f33563 358
6a885fd0
BP
359 case MFF_N_IDS:
360 default:
428b2edd 361 OVS_NOT_REACHED();
6a885fd0
BP
362 }
363}
364
365/* Initializes 'mask' with the wildcard bit pattern for field 'mf' within 'wc'.
366 * Each bit in 'mask' will be set to 1 if the bit is significant for matching
367 * purposes, or to 0 if it is wildcarded.
368 *
369 * The caller is responsible for ensuring that 'wc' corresponds to a flow that
370 * meets 'mf''s prerequisites. */
371void
372mf_get_mask(const struct mf_field *mf, const struct flow_wildcards *wc,
373 union mf_value *mask)
374{
fb15feb0 375 mf_get_value(mf, &wc->masks, mask);
6a885fd0
BP
376}
377
378/* Tests whether 'mask' is a valid wildcard bit pattern for 'mf'. Returns true
379 * if the mask is valid, false otherwise. */
380bool
381mf_is_mask_valid(const struct mf_field *mf, const union mf_value *mask)
382{
383 switch (mf->maskable) {
384 case MFM_NONE:
53cb9c3e
JR
385 return (is_all_zeros(mask, mf->n_bytes) ||
386 is_all_ones(mask, mf->n_bytes));
6a885fd0
BP
387
388 case MFM_FULLY:
389 return true;
6a885fd0
BP
390 }
391
428b2edd 392 OVS_NOT_REACHED();
6a885fd0
BP
393}
394
aff49b8c 395/* Returns true if 'flow' meets the prerequisites for 'mf', false otherwise.
67210a55 396 * If a non-NULL 'mask' is passed, zero-valued matches can also be verified.
aff49b8c 397 * Sets inspected bits in 'wc', if non-NULL. */
67210a55
JR
398static bool
399mf_are_prereqs_ok__(const struct mf_field *mf, const struct flow *flow,
daf4d3c1 400 const struct flow_wildcards *mask,
67210a55 401 struct flow_wildcards *wc)
6a885fd0
BP
402{
403 switch (mf->prereqs) {
404 case MFP_NONE:
405 return true;
6a885fd0 406 case MFP_ARP:
aff49b8c
JR
407 return (flow->dl_type == htons(ETH_TYPE_ARP) ||
408 flow->dl_type == htons(ETH_TYPE_RARP));
6a885fd0
BP
409 case MFP_IPV4:
410 return flow->dl_type == htons(ETH_TYPE_IP);
411 case MFP_IPV6:
412 return flow->dl_type == htons(ETH_TYPE_IPV6);
8069b0da 413 case MFP_VLAN_VID:
aff49b8c 414 return is_vlan(flow, wc);
b02475c5
SH
415 case MFP_MPLS:
416 return eth_type_mpls(flow->dl_type);
6a885fd0
BP
417 case MFP_IP_ANY:
418 return is_ip_any(flow);
daf4d3c1
JR
419 case MFP_CT_VALID:
420 return is_ct_valid(flow, mask, wc);
421 case MFP_CTV4_VALID:
422 return flow->dl_type == htons(ETH_TYPE_IP)
423 && is_ct_valid(flow, mask, wc);
424 case MFP_CTV6_VALID:
425 return flow->dl_type == htons(ETH_TYPE_IPV6)
426 && is_ct_valid(flow, mask, wc);
6a885fd0 427 case MFP_TCP:
67210a55 428 /* Matching !FRAG_LATER is not enforced (mask is not checked). */
aff49b8c 429 return is_tcp(flow, wc) && !(flow->nw_frag & FLOW_NW_FRAG_LATER);
6a885fd0 430 case MFP_UDP:
aff49b8c 431 return is_udp(flow, wc) && !(flow->nw_frag & FLOW_NW_FRAG_LATER);
0d56eaf2 432 case MFP_SCTP:
aff49b8c 433 return is_sctp(flow, wc) && !(flow->nw_frag & FLOW_NW_FRAG_LATER);
268a95e0 434 case MFP_ICMPV4:
aff49b8c 435 return is_icmpv4(flow, wc);
6a885fd0 436 case MFP_ICMPV6:
aff49b8c 437 return is_icmpv6(flow, wc);
6a885fd0 438 case MFP_ND:
aff49b8c 439 return is_nd(flow, wc);
6a885fd0 440 case MFP_ND_SOLICIT:
aff49b8c 441 return is_nd(flow, wc) && flow->tp_src == htons(ND_NEIGHBOR_SOLICIT);
6a885fd0 442 case MFP_ND_ADVERT:
aff49b8c 443 return is_nd(flow, wc) && flow->tp_src == htons(ND_NEIGHBOR_ADVERT);
6a885fd0
BP
444 }
445
428b2edd 446 OVS_NOT_REACHED();
6a885fd0
BP
447}
448
67210a55
JR
449/* Returns true if 'flow' meets the prerequisites for 'mf', false otherwise.
450 * Sets inspected bits in 'wc', if non-NULL. */
451bool
452mf_are_prereqs_ok(const struct mf_field *mf, const struct flow *flow,
453 struct flow_wildcards *wc)
454{
455 return mf_are_prereqs_ok__(mf, flow, NULL, wc);
456}
457
458/* Returns true if 'match' meets the prerequisites for 'mf', false otherwise.
459 */
460bool
461mf_are_match_prereqs_ok(const struct mf_field *mf, const struct match *match)
462{
463 return mf_are_prereqs_ok__(mf, &match->flow, &match->wc, NULL);
464}
465
6a885fd0
BP
466/* Returns true if 'value' may be a valid value *as part of a masked match*,
467 * false otherwise.
468 *
469 * A value is not rejected just because it is not valid for the field in
470 * question, but only if it doesn't make sense to test the bits in question at
471 * all. For example, the MFF_VLAN_TCI field will never have a nonzero value
472 * without the VLAN_CFI bit being set, but we can't reject those values because
473 * it is still legitimate to test just for those bits (see the documentation
0da61033 474 * for NXM_OF_VLAN_TCI in meta-flow.h). On the other hand, there is never a
530180fd 475 * reason to set the low bit of MFF_IP_DSCP to 1, so we reject that. */
6a885fd0
BP
476bool
477mf_is_value_valid(const struct mf_field *mf, const union mf_value *value)
478{
479 switch (mf->id) {
a79f29f2
AZ
480 case MFF_DP_HASH:
481 case MFF_RECIRC_ID:
18080541 482 case MFF_CONJ_ID:
6a885fd0 483 case MFF_TUN_ID:
4fe3445a
PS
484 case MFF_TUN_SRC:
485 case MFF_TUN_DST:
7dad8e9a
TLSC
486 case MFF_TUN_IPV6_SRC:
487 case MFF_TUN_IPV6_DST:
4fe3445a
PS
488 case MFF_TUN_TOS:
489 case MFF_TUN_TTL:
ac6073e3
MC
490 case MFF_TUN_GBP_ID:
491 case MFF_TUN_GBP_FLAGS:
9558d2a5 492 CASE_MFF_TUN_METADATA:
969fc56c 493 case MFF_METADATA:
6a885fd0 494 case MFF_IN_PORT:
1b567fb9 495 case MFF_SKB_PRIORITY:
1362e248 496 case MFF_PKT_MARK:
07659514 497 case MFF_CT_ZONE:
8e53fe8c 498 case MFF_CT_MARK:
9daf2348 499 case MFF_CT_LABEL:
daf4d3c1
JR
500 case MFF_CT_NW_PROTO:
501 case MFF_CT_NW_SRC:
502 case MFF_CT_NW_DST:
503 case MFF_CT_IPV6_SRC:
504 case MFF_CT_IPV6_DST:
505 case MFF_CT_TP_SRC:
506 case MFF_CT_TP_DST:
0d7e2fe4 507 CASE_MFF_REGS:
79fe0f46 508 CASE_MFF_XREGS:
b23ada8e 509 CASE_MFF_XXREGS:
6a885fd0
BP
510 case MFF_ETH_SRC:
511 case MFF_ETH_DST:
512 case MFF_ETH_TYPE:
513 case MFF_VLAN_TCI:
bef3f465 514 case MFF_MPLS_TTL:
6a885fd0
BP
515 case MFF_IPV4_SRC:
516 case MFF_IPV4_DST:
517 case MFF_IPV6_SRC:
518 case MFF_IPV6_DST:
519 case MFF_IP_PROTO:
a61680c6 520 case MFF_IP_TTL:
6a885fd0
BP
521 case MFF_ARP_SPA:
522 case MFF_ARP_TPA:
523 case MFF_ARP_SHA:
524 case MFF_ARP_THA:
525 case MFF_TCP_SRC:
526 case MFF_TCP_DST:
527 case MFF_UDP_SRC:
528 case MFF_UDP_DST:
0d56eaf2
JS
529 case MFF_SCTP_SRC:
530 case MFF_SCTP_DST:
268a95e0
BP
531 case MFF_ICMPV4_TYPE:
532 case MFF_ICMPV4_CODE:
533 case MFF_ICMPV6_TYPE:
534 case MFF_ICMPV6_CODE:
6a885fd0
BP
535 case MFF_ND_TARGET:
536 case MFF_ND_SLL:
537 case MFF_ND_TLL:
538 return true;
539
c61f3870
BP
540 case MFF_IN_PORT_OXM:
541 case MFF_ACTSET_OUTPUT: {
4e022ec0 542 ofp_port_t port;
72333065
JR
543 return !ofputil_port_from_ofp11(value->be32, &port);
544 }
545
530180fd 546 case MFF_IP_DSCP:
7257b535 547 return !(value->u8 & ~IP_DSCP_MASK);
1638b906
BP
548 case MFF_IP_DSCP_SHIFTED:
549 return !(value->u8 & (~IP_DSCP_MASK >> 2));
530180fd
JP
550 case MFF_IP_ECN:
551 return !(value->u8 & ~IP_ECN_MASK);
7257b535 552 case MFF_IP_FRAG:
eadef313 553 return !(value->u8 & ~FLOW_NW_FRAG_MASK);
dc235f7f
JR
554 case MFF_TCP_FLAGS:
555 return !(value->be16 & ~htons(0x0fff));
6a885fd0
BP
556
557 case MFF_ARP_OP:
558 return !(value->be16 & htons(0xff00));
559
441c57a9 560 case MFF_DL_VLAN:
6a885fd0 561 return !(value->be16 & htons(VLAN_CFI | VLAN_PCP_MASK));
cc34bc8c
BP
562 case MFF_VLAN_VID:
563 return !(value->be16 & htons(VLAN_PCP_MASK));
6a885fd0 564
441c57a9 565 case MFF_DL_VLAN_PCP:
cc34bc8c 566 case MFF_VLAN_PCP:
24016f5a 567 return !(value->u8 & ~(VLAN_PCP_MASK >> VLAN_PCP_SHIFT));
6a885fd0 568
fa8223b7
JP
569 case MFF_IPV6_LABEL:
570 return !(value->be32 & ~htonl(IPV6_LABEL_MASK));
571
b02475c5
SH
572 case MFF_MPLS_LABEL:
573 return !(value->be32 & ~htonl(MPLS_LABEL_MASK >> MPLS_LABEL_SHIFT));
574
575 case MFF_MPLS_TC:
576 return !(value->u8 & ~(MPLS_TC_MASK >> MPLS_TC_SHIFT));
577
578 case MFF_MPLS_BOS:
579 return !(value->u8 & ~(MPLS_BOS_MASK >> MPLS_BOS_SHIFT));
580
b666962b
JG
581 case MFF_TUN_FLAGS:
582 return !(value->be16 & ~htons(FLOW_TNL_PUB_F_MASK));
583
07659514
JS
584 case MFF_CT_STATE:
585 return !(value->be32 & ~htonl(CS_SUPPORTED_MASK));
586
6a885fd0
BP
587 case MFF_N_IDS:
588 default:
428b2edd 589 OVS_NOT_REACHED();
6a885fd0
BP
590 }
591}
592
593/* Copies the value of field 'mf' from 'flow' into 'value'. The caller is
594 * responsible for ensuring that 'flow' meets 'mf''s prerequisites. */
595void
596mf_get_value(const struct mf_field *mf, const struct flow *flow,
597 union mf_value *value)
598{
599 switch (mf->id) {
a79f29f2
AZ
600 case MFF_DP_HASH:
601 value->be32 = htonl(flow->dp_hash);
602 break;
603 case MFF_RECIRC_ID:
604 value->be32 = htonl(flow->recirc_id);
605 break;
18080541
BP
606 case MFF_CONJ_ID:
607 value->be32 = htonl(flow->conj_id);
608 break;
6a885fd0 609 case MFF_TUN_ID:
296e07ac 610 value->be64 = flow->tunnel.tun_id;
6a885fd0 611 break;
4fe3445a
PS
612 case MFF_TUN_SRC:
613 value->be32 = flow->tunnel.ip_src;
614 break;
615 case MFF_TUN_DST:
616 value->be32 = flow->tunnel.ip_dst;
617 break;
7dad8e9a
TLSC
618 case MFF_TUN_IPV6_SRC:
619 value->ipv6 = flow->tunnel.ipv6_src;
620 break;
621 case MFF_TUN_IPV6_DST:
622 value->ipv6 = flow->tunnel.ipv6_dst;
623 break;
4fe3445a 624 case MFF_TUN_FLAGS:
b666962b 625 value->be16 = htons(flow->tunnel.flags & FLOW_TNL_PUB_F_MASK);
4fe3445a 626 break;
ac6073e3
MC
627 case MFF_TUN_GBP_ID:
628 value->be16 = flow->tunnel.gbp_id;
629 break;
630 case MFF_TUN_GBP_FLAGS:
631 value->u8 = flow->tunnel.gbp_flags;
632 break;
4fe3445a
PS
633 case MFF_TUN_TTL:
634 value->u8 = flow->tunnel.ip_ttl;
635 break;
636 case MFF_TUN_TOS:
637 value->u8 = flow->tunnel.ip_tos;
638 break;
9558d2a5 639 CASE_MFF_TUN_METADATA:
6728d578 640 tun_metadata_read(&flow->tunnel, mf, value);
9558d2a5 641 break;
4fe3445a 642
969fc56c
JS
643 case MFF_METADATA:
644 value->be64 = flow->metadata;
645 break;
6a885fd0
BP
646
647 case MFF_IN_PORT:
4e022ec0 648 value->be16 = htons(ofp_to_u16(flow->in_port.ofp_port));
6a885fd0 649 break;
72333065 650 case MFF_IN_PORT_OXM:
4e022ec0 651 value->be32 = ofputil_port_to_ofp11(flow->in_port.ofp_port);
72333065 652 break;
c61f3870
BP
653 case MFF_ACTSET_OUTPUT:
654 value->be32 = ofputil_port_to_ofp11(flow->actset_output);
655 break;
72333065 656
1b567fb9 657 case MFF_SKB_PRIORITY:
a81f0a44 658 value->be32 = htonl(flow->skb_priority);
1b567fb9
AA
659 break;
660
1362e248
JG
661 case MFF_PKT_MARK:
662 value->be32 = htonl(flow->pkt_mark);
1b567fb9
AA
663 break;
664
07659514
JS
665 case MFF_CT_STATE:
666 value->be32 = htonl(flow->ct_state);
667 break;
668
669 case MFF_CT_ZONE:
670 value->be16 = htons(flow->ct_zone);
671 break;
672
8e53fe8c
JS
673 case MFF_CT_MARK:
674 value->be32 = htonl(flow->ct_mark);
675 break;
676
9daf2348 677 case MFF_CT_LABEL:
32ea15f6 678 value->be128 = hton128(flow->ct_label);
9daf2348
JS
679 break;
680
daf4d3c1
JR
681 case MFF_CT_NW_PROTO:
682 value->u8 = flow->ct_nw_proto;
683 break;
684
685 case MFF_CT_NW_SRC:
686 value->be32 = flow->ct_nw_src;
687 break;
688
689 case MFF_CT_NW_DST:
690 value->be32 = flow->ct_nw_dst;
691 break;
692
693 case MFF_CT_IPV6_SRC:
694 value->ipv6 = flow->ct_ipv6_src;
695 break;
696
697 case MFF_CT_IPV6_DST:
698 value->ipv6 = flow->ct_ipv6_dst;
699 break;
700
701 case MFF_CT_TP_SRC:
702 value->be16 = flow->ct_tp_src;
703 break;
704
705 case MFF_CT_TP_DST:
706 value->be16 = flow->ct_tp_dst;
707 break;
708
0d7e2fe4 709 CASE_MFF_REGS:
2f98b0b7 710 value->be32 = htonl(flow->regs[mf->id - MFF_REG0]);
6a885fd0
BP
711 break;
712
79fe0f46
BP
713 CASE_MFF_XREGS:
714 value->be64 = htonll(flow_get_xreg(flow, mf->id - MFF_XREG0));
715 break;
716
b23ada8e
JP
717 CASE_MFF_XXREGS:
718 value->be128 = hton128(flow_get_xxreg(flow, mf->id - MFF_XXREG0));
719 break;
720
6a885fd0 721 case MFF_ETH_SRC:
74ff3298 722 value->mac = flow->dl_src;
6a885fd0
BP
723 break;
724
725 case MFF_ETH_DST:
74ff3298 726 value->mac = flow->dl_dst;
6a885fd0
BP
727 break;
728
729 case MFF_ETH_TYPE:
730 value->be16 = flow->dl_type;
731 break;
732
733 case MFF_VLAN_TCI:
734 value->be16 = flow->vlan_tci;
735 break;
736
441c57a9 737 case MFF_DL_VLAN:
6a885fd0
BP
738 value->be16 = flow->vlan_tci & htons(VLAN_VID_MASK);
739 break;
cc34bc8c
BP
740 case MFF_VLAN_VID:
741 value->be16 = flow->vlan_tci & htons(VLAN_VID_MASK | VLAN_CFI);
742 break;
6a885fd0 743
441c57a9 744 case MFF_DL_VLAN_PCP:
cc34bc8c 745 case MFF_VLAN_PCP:
6a885fd0
BP
746 value->u8 = vlan_tci_to_pcp(flow->vlan_tci);
747 break;
748
b02475c5 749 case MFF_MPLS_LABEL:
8bfd0fda 750 value->be32 = htonl(mpls_lse_to_label(flow->mpls_lse[0]));
b02475c5
SH
751 break;
752
753 case MFF_MPLS_TC:
8bfd0fda 754 value->u8 = mpls_lse_to_tc(flow->mpls_lse[0]);
b02475c5
SH
755 break;
756
757 case MFF_MPLS_BOS:
8bfd0fda
BP
758 value->u8 = mpls_lse_to_bos(flow->mpls_lse[0]);
759 break;
b02475c5 760
bef3f465
BP
761 case MFF_MPLS_TTL:
762 value->u8 = mpls_lse_to_ttl(flow->mpls_lse[0]);
763 break;
764
6a885fd0
BP
765 case MFF_IPV4_SRC:
766 value->be32 = flow->nw_src;
767 break;
768
769 case MFF_IPV4_DST:
770 value->be32 = flow->nw_dst;
771 break;
772
773 case MFF_IPV6_SRC:
774 value->ipv6 = flow->ipv6_src;
775 break;
776
777 case MFF_IPV6_DST:
778 value->ipv6 = flow->ipv6_dst;
779 break;
780
fa8223b7
JP
781 case MFF_IPV6_LABEL:
782 value->be32 = flow->ipv6_label;
783 break;
784
6a885fd0
BP
785 case MFF_IP_PROTO:
786 value->u8 = flow->nw_proto;
787 break;
788
530180fd 789 case MFF_IP_DSCP:
eadef313 790 value->u8 = flow->nw_tos & IP_DSCP_MASK;
7257b535
BP
791 break;
792
1638b906
BP
793 case MFF_IP_DSCP_SHIFTED:
794 value->u8 = flow->nw_tos >> 2;
795 break;
796
530180fd 797 case MFF_IP_ECN:
eadef313 798 value->u8 = flow->nw_tos & IP_ECN_MASK;
530180fd
JP
799 break;
800
a61680c6
JP
801 case MFF_IP_TTL:
802 value->u8 = flow->nw_ttl;
803 break;
804
7257b535 805 case MFF_IP_FRAG:
eadef313 806 value->u8 = flow->nw_frag;
6a885fd0
BP
807 break;
808
809 case MFF_ARP_OP:
810 value->be16 = htons(flow->nw_proto);
811 break;
812
813 case MFF_ARP_SPA:
814 value->be32 = flow->nw_src;
815 break;
816
817 case MFF_ARP_TPA:
818 value->be32 = flow->nw_dst;
819 break;
820
821 case MFF_ARP_SHA:
822 case MFF_ND_SLL:
74ff3298 823 value->mac = flow->arp_sha;
6a885fd0
BP
824 break;
825
826 case MFF_ARP_THA:
827 case MFF_ND_TLL:
74ff3298 828 value->mac = flow->arp_tha;
6a885fd0
BP
829 break;
830
831 case MFF_TCP_SRC:
6a885fd0 832 case MFF_UDP_SRC:
0d56eaf2 833 case MFF_SCTP_SRC:
6a885fd0
BP
834 value->be16 = flow->tp_src;
835 break;
836
71baec06 837 case MFF_TCP_DST:
6a885fd0 838 case MFF_UDP_DST:
0d56eaf2 839 case MFF_SCTP_DST:
6a885fd0
BP
840 value->be16 = flow->tp_dst;
841 break;
842
dc235f7f
JR
843 case MFF_TCP_FLAGS:
844 value->be16 = flow->tcp_flags;
845 break;
846
268a95e0
BP
847 case MFF_ICMPV4_TYPE:
848 case MFF_ICMPV6_TYPE:
6a885fd0
BP
849 value->u8 = ntohs(flow->tp_src);
850 break;
851
268a95e0
BP
852 case MFF_ICMPV4_CODE:
853 case MFF_ICMPV6_CODE:
6a885fd0
BP
854 value->u8 = ntohs(flow->tp_dst);
855 break;
856
857 case MFF_ND_TARGET:
858 value->ipv6 = flow->nd_target;
859 break;
860
861 case MFF_N_IDS:
862 default:
428b2edd 863 OVS_NOT_REACHED();
6a885fd0
BP
864 }
865}
866
81a76618
BP
867/* Makes 'match' match field 'mf' exactly, with the value matched taken from
868 * 'value'. The caller is responsible for ensuring that 'match' meets 'mf''s
4f7b100c
JG
869 * prerequisites.
870 *
871 * If non-NULL, 'err_str' returns a malloc'ed string describing any errors
872 * with the request or NULL if there is no error. The caller is reponsible
873 * for freeing the string. */
6a885fd0
BP
874void
875mf_set_value(const struct mf_field *mf,
4f7b100c 876 const union mf_value *value, struct match *match, char **err_str)
6a885fd0 877{
4f7b100c
JG
878 if (err_str) {
879 *err_str = NULL;
880 }
881
6a885fd0 882 switch (mf->id) {
a79f29f2
AZ
883 case MFF_DP_HASH:
884 match_set_dp_hash(match, ntohl(value->be32));
885 break;
886 case MFF_RECIRC_ID:
887 match_set_recirc_id(match, ntohl(value->be32));
888 break;
18080541
BP
889 case MFF_CONJ_ID:
890 match_set_conj_id(match, ntohl(value->be32));
891 break;
6a885fd0 892 case MFF_TUN_ID:
81a76618 893 match_set_tun_id(match, value->be64);
6a885fd0 894 break;
4fe3445a
PS
895 case MFF_TUN_SRC:
896 match_set_tun_src(match, value->be32);
897 break;
898 case MFF_TUN_DST:
899 match_set_tun_dst(match, value->be32);
900 break;
7dad8e9a
TLSC
901 case MFF_TUN_IPV6_SRC:
902 match_set_tun_ipv6_src(match, &value->ipv6);
903 break;
904 case MFF_TUN_IPV6_DST:
905 match_set_tun_ipv6_dst(match, &value->ipv6);
906 break;
4fe3445a
PS
907 case MFF_TUN_FLAGS:
908 match_set_tun_flags(match, ntohs(value->be16));
909 break;
ac6073e3
MC
910 case MFF_TUN_GBP_ID:
911 match_set_tun_gbp_id(match, value->be16);
912 break;
913 case MFF_TUN_GBP_FLAGS:
914 match_set_tun_gbp_flags(match, value->u8);
915 break;
4fe3445a
PS
916 case MFF_TUN_TOS:
917 match_set_tun_tos(match, value->u8);
918 break;
919 case MFF_TUN_TTL:
920 match_set_tun_ttl(match, value->u8);
921 break;
9558d2a5 922 CASE_MFF_TUN_METADATA:
4f7b100c 923 tun_metadata_set_match(mf, value, NULL, match, err_str);
9558d2a5 924 break;
4fe3445a 925
969fc56c 926 case MFF_METADATA:
81a76618 927 match_set_metadata(match, value->be64);
969fc56c 928 break;
6a885fd0
BP
929
930 case MFF_IN_PORT:
4e022ec0 931 match_set_in_port(match, u16_to_ofp(ntohs(value->be16)));
6a885fd0
BP
932 break;
933
72333065 934 case MFF_IN_PORT_OXM: {
4e022ec0 935 ofp_port_t port;
bc146369 936 ofputil_port_from_ofp11(value->be32, &port);
72333065
JR
937 match_set_in_port(match, port);
938 break;
939 }
c61f3870
BP
940 case MFF_ACTSET_OUTPUT: {
941 ofp_port_t port;
942 ofputil_port_from_ofp11(value->be32, &port);
943 match_set_actset_output(match, port);
944 break;
945 }
72333065 946
1b567fb9
AA
947 case MFF_SKB_PRIORITY:
948 match_set_skb_priority(match, ntohl(value->be32));
949 break;
950
1362e248
JG
951 case MFF_PKT_MARK:
952 match_set_pkt_mark(match, ntohl(value->be32));
1b567fb9
AA
953 break;
954
07659514
JS
955 case MFF_CT_STATE:
956 match_set_ct_state(match, ntohl(value->be32));
957 break;
958
959 case MFF_CT_ZONE:
960 match_set_ct_zone(match, ntohs(value->be16));
961 break;
962
8e53fe8c
JS
963 case MFF_CT_MARK:
964 match_set_ct_mark(match, ntohl(value->be32));
965 break;
966
32ea15f6
JP
967 case MFF_CT_LABEL:
968 match_set_ct_label(match, ntoh128(value->be128));
9daf2348 969 break;
9daf2348 970
daf4d3c1
JR
971 case MFF_CT_NW_PROTO:
972 match_set_ct_nw_proto(match, value->u8);
973 break;
974
975 case MFF_CT_NW_SRC:
976 match_set_ct_nw_src(match, value->be32);
977 break;
978
979 case MFF_CT_NW_DST:
980 match_set_ct_nw_dst(match, value->be32);
981 break;
982
983 case MFF_CT_IPV6_SRC:
984 match_set_ct_ipv6_src(match, &value->ipv6);
985 break;
986
987 case MFF_CT_IPV6_DST:
988 match_set_ct_ipv6_dst(match, &value->ipv6);
989 break;
990
991 case MFF_CT_TP_SRC:
992 match_set_ct_tp_src(match, value->be16);
993 break;
994
995 case MFF_CT_TP_DST:
996 match_set_ct_tp_dst(match, value->be16);
997 break;
998
0d7e2fe4 999 CASE_MFF_REGS:
81a76618 1000 match_set_reg(match, mf->id - MFF_REG0, ntohl(value->be32));
6a885fd0 1001 break;
6a885fd0 1002
79fe0f46
BP
1003 CASE_MFF_XREGS:
1004 match_set_xreg(match, mf->id - MFF_XREG0, ntohll(value->be64));
1005 break;
1006
b23ada8e
JP
1007 CASE_MFF_XXREGS:
1008 match_set_xxreg(match, mf->id - MFF_XXREG0, ntoh128(value->be128));
1009 break;
1010
6a885fd0 1011 case MFF_ETH_SRC:
81a76618 1012 match_set_dl_src(match, value->mac);
6a885fd0
BP
1013 break;
1014
1015 case MFF_ETH_DST:
81a76618 1016 match_set_dl_dst(match, value->mac);
6a885fd0
BP
1017 break;
1018
1019 case MFF_ETH_TYPE:
81a76618 1020 match_set_dl_type(match, value->be16);
6a885fd0
BP
1021 break;
1022
1023 case MFF_VLAN_TCI:
81a76618 1024 match_set_dl_tci(match, value->be16);
6a885fd0
BP
1025 break;
1026
441c57a9 1027 case MFF_DL_VLAN:
81a76618 1028 match_set_dl_vlan(match, value->be16);
6a885fd0 1029 break;
cc34bc8c 1030 case MFF_VLAN_VID:
81a76618 1031 match_set_vlan_vid(match, value->be16);
cc34bc8c 1032 break;
6a885fd0 1033
441c57a9 1034 case MFF_DL_VLAN_PCP:
cc34bc8c 1035 case MFF_VLAN_PCP:
81a76618 1036 match_set_dl_vlan_pcp(match, value->u8);
6a885fd0
BP
1037 break;
1038
b02475c5 1039 case MFF_MPLS_LABEL:
8bfd0fda 1040 match_set_mpls_label(match, 0, value->be32);
b02475c5
SH
1041 break;
1042
1043 case MFF_MPLS_TC:
8bfd0fda 1044 match_set_mpls_tc(match, 0, value->u8);
b02475c5
SH
1045 break;
1046
1047 case MFF_MPLS_BOS:
8bfd0fda
BP
1048 match_set_mpls_bos(match, 0, value->u8);
1049 break;
b02475c5 1050
bef3f465
BP
1051 case MFF_MPLS_TTL:
1052 match_set_mpls_ttl(match, 0, value->u8);
1053 break;
1054
6a885fd0 1055 case MFF_IPV4_SRC:
81a76618 1056 match_set_nw_src(match, value->be32);
6a885fd0
BP
1057 break;
1058
1059 case MFF_IPV4_DST:
81a76618 1060 match_set_nw_dst(match, value->be32);
6a885fd0
BP
1061 break;
1062
1063 case MFF_IPV6_SRC:
81a76618 1064 match_set_ipv6_src(match, &value->ipv6);
6a885fd0
BP
1065 break;
1066
1067 case MFF_IPV6_DST:
81a76618 1068 match_set_ipv6_dst(match, &value->ipv6);
6a885fd0
BP
1069 break;
1070
fa8223b7 1071 case MFF_IPV6_LABEL:
81a76618 1072 match_set_ipv6_label(match, value->be32);
fa8223b7
JP
1073 break;
1074
6a885fd0 1075 case MFF_IP_PROTO:
81a76618 1076 match_set_nw_proto(match, value->u8);
6a885fd0
BP
1077 break;
1078
530180fd 1079 case MFF_IP_DSCP:
81a76618 1080 match_set_nw_dscp(match, value->u8);
530180fd
JP
1081 break;
1082
1638b906
BP
1083 case MFF_IP_DSCP_SHIFTED:
1084 match_set_nw_dscp(match, value->u8 << 2);
1085 break;
1086
530180fd 1087 case MFF_IP_ECN:
81a76618 1088 match_set_nw_ecn(match, value->u8);
6a885fd0
BP
1089 break;
1090
a61680c6 1091 case MFF_IP_TTL:
81a76618 1092 match_set_nw_ttl(match, value->u8);
a61680c6
JP
1093 break;
1094
7257b535 1095 case MFF_IP_FRAG:
81a76618 1096 match_set_nw_frag(match, value->u8);
7257b535
BP
1097 break;
1098
6a885fd0 1099 case MFF_ARP_OP:
81a76618 1100 match_set_nw_proto(match, ntohs(value->be16));
6a885fd0
BP
1101 break;
1102
1103 case MFF_ARP_SPA:
81a76618 1104 match_set_nw_src(match, value->be32);
6a885fd0
BP
1105 break;
1106
1107 case MFF_ARP_TPA:
81a76618 1108 match_set_nw_dst(match, value->be32);
6a885fd0
BP
1109 break;
1110
1111 case MFF_ARP_SHA:
1112 case MFF_ND_SLL:
81a76618 1113 match_set_arp_sha(match, value->mac);
6a885fd0
BP
1114 break;
1115
1116 case MFF_ARP_THA:
1117 case MFF_ND_TLL:
81a76618 1118 match_set_arp_tha(match, value->mac);
6a885fd0
BP
1119 break;
1120
1121 case MFF_TCP_SRC:
6a885fd0 1122 case MFF_UDP_SRC:
0d56eaf2 1123 case MFF_SCTP_SRC:
81a76618 1124 match_set_tp_src(match, value->be16);
6a885fd0
BP
1125 break;
1126
71baec06 1127 case MFF_TCP_DST:
6a885fd0 1128 case MFF_UDP_DST:
0d56eaf2 1129 case MFF_SCTP_DST:
81a76618 1130 match_set_tp_dst(match, value->be16);
6a885fd0
BP
1131 break;
1132
dc235f7f
JR
1133 case MFF_TCP_FLAGS:
1134 match_set_tcp_flags(match, value->be16);
1135 break;
1136
268a95e0
BP
1137 case MFF_ICMPV4_TYPE:
1138 case MFF_ICMPV6_TYPE:
81a76618 1139 match_set_icmp_type(match, value->u8);
6a885fd0
BP
1140 break;
1141
268a95e0
BP
1142 case MFF_ICMPV4_CODE:
1143 case MFF_ICMPV6_CODE:
81a76618 1144 match_set_icmp_code(match, value->u8);
6a885fd0
BP
1145 break;
1146
1147 case MFF_ND_TARGET:
81a76618 1148 match_set_nd_target(match, &value->ipv6);
6a885fd0
BP
1149 break;
1150
1151 case MFF_N_IDS:
1152 default:
428b2edd 1153 OVS_NOT_REACHED();
6a885fd0
BP
1154 }
1155}
1156
5cd4ead5
JR
1157/* Unwildcard the bits in 'mask' of the 'wc' member field described by 'mf'.
1158 * The caller is responsible for ensuring that 'wc' meets 'mf''s
1159 * prerequisites. */
5a0a5702 1160void
5cd4ead5
JR
1161mf_mask_field_masked(const struct mf_field *mf, const union mf_value *mask,
1162 struct flow_wildcards *wc)
5a0a5702 1163{
5cd4ead5
JR
1164 union mf_value temp_mask;
1165 /* For MFF_DL_VLAN, we cannot send a all 1's to flow_set_dl_vlan() as that
1166 * will be considered as OFP10_VLAN_NONE. So make sure the mask only has
1167 * valid bits in this case. */
5a0a5702 1168 if (mf->id == MFF_DL_VLAN) {
5cd4ead5
JR
1169 temp_mask.be16 = htons(VLAN_VID_MASK) & mask->be16;
1170 mask = &temp_mask;
5a0a5702 1171 }
5cd4ead5
JR
1172
1173 union mf_value mask_value;
1174
1175 mf_get_value(mf, &wc->masks, &mask_value);
1176 for (size_t i = 0; i < mf->n_bytes; i++) {
1177 mask_value.b[i] |= mask->b[i];
1178 }
1179 mf_set_flow_value(mf, &mask_value, &wc->masks);
1180}
1181
1182/* Unwildcard 'wc' member field described by 'mf'. The caller is
1183 * responsible for ensuring that 'mask' meets 'mf''s prerequisites. */
1184void
1185mf_mask_field(const struct mf_field *mf, struct flow_wildcards *wc)
1186{
1187 mf_mask_field_masked(mf, &exact_match_mask, wc);
5a0a5702
GS
1188}
1189
4ede8c79
JG
1190static int
1191field_len(const struct mf_field *mf, const union mf_value *value_)
1192{
1193 const uint8_t *value = &value_->u8;
1194 int i;
1195
1196 if (!mf->variable_len) {
1197 return mf->n_bytes;
1198 }
1199
1200 if (!value) {
1201 return 0;
1202 }
1203
1204 for (i = 0; i < mf->n_bytes; i++) {
1205 if (value[i] != 0) {
1206 break;
1207 }
1208 }
1209
1210 return mf->n_bytes - i;
1211}
1212
1213/* Returns the effective length of the field. For fixed length fields,
1214 * this is just the defined length. For variable length fields, it is
1215 * the minimum size encoding that retains the same meaning (i.e.
1cb20095
JG
1216 * discarding leading zeros).
1217 *
1218 * 'is_masked' returns (if non-NULL) whether the original contained
1219 * a mask. Otherwise, a mask that is the same length as the value
1220 * might be misinterpreted as an exact match. */
4ede8c79
JG
1221int
1222mf_field_len(const struct mf_field *mf, const union mf_value *value,
1cb20095 1223 const union mf_value *mask, bool *is_masked_)
4ede8c79
JG
1224{
1225 int len, mask_len;
1cb20095 1226 bool is_masked = mask && !is_all_ones(mask, mf->n_bytes);
4ede8c79
JG
1227
1228 len = field_len(mf, value);
1cb20095 1229 if (is_masked) {
4ede8c79
JG
1230 mask_len = field_len(mf, mask);
1231 len = MAX(len, mask_len);
1232 }
1233
1cb20095
JG
1234 if (is_masked_) {
1235 *is_masked_ = is_masked;
1236 }
1237
4ede8c79
JG
1238 return len;
1239}
1240
910fb1d8
JP
1241/* Sets 'flow' member field described by 'mf' to 'value'. The caller is
1242 * responsible for ensuring that 'flow' meets 'mf''s prerequisites.*/
28da1f8f
BP
1243void
1244mf_set_flow_value(const struct mf_field *mf,
1245 const union mf_value *value, struct flow *flow)
1246{
1247 switch (mf->id) {
a79f29f2
AZ
1248 case MFF_DP_HASH:
1249 flow->dp_hash = ntohl(value->be32);
1250 break;
1251 case MFF_RECIRC_ID:
1252 flow->recirc_id = ntohl(value->be32);
1253 break;
18080541
BP
1254 case MFF_CONJ_ID:
1255 flow->conj_id = ntohl(value->be32);
1256 break;
28da1f8f 1257 case MFF_TUN_ID:
296e07ac 1258 flow->tunnel.tun_id = value->be64;
28da1f8f 1259 break;
4fe3445a
PS
1260 case MFF_TUN_SRC:
1261 flow->tunnel.ip_src = value->be32;
1262 break;
1263 case MFF_TUN_DST:
1264 flow->tunnel.ip_dst = value->be32;
1265 break;
7dad8e9a
TLSC
1266 case MFF_TUN_IPV6_SRC:
1267 flow->tunnel.ipv6_src = value->ipv6;
1268 break;
1269 case MFF_TUN_IPV6_DST:
1270 flow->tunnel.ipv6_dst = value->ipv6;
1271 break;
4fe3445a 1272 case MFF_TUN_FLAGS:
b666962b
JG
1273 flow->tunnel.flags = (flow->tunnel.flags & ~FLOW_TNL_PUB_F_MASK) |
1274 ntohs(value->be16);
4fe3445a 1275 break;
ac6073e3
MC
1276 case MFF_TUN_GBP_ID:
1277 flow->tunnel.gbp_id = value->be16;
1278 break;
1279 case MFF_TUN_GBP_FLAGS:
1280 flow->tunnel.gbp_flags = value->u8;
1281 break;
4fe3445a
PS
1282 case MFF_TUN_TOS:
1283 flow->tunnel.ip_tos = value->u8;
1284 break;
1285 case MFF_TUN_TTL:
1286 flow->tunnel.ip_ttl = value->u8;
1287 break;
9558d2a5 1288 CASE_MFF_TUN_METADATA:
6728d578 1289 tun_metadata_write(&flow->tunnel, mf, value);
1aaf0ada 1290 break;
969fc56c
JS
1291 case MFF_METADATA:
1292 flow->metadata = value->be64;
1293 break;
28da1f8f
BP
1294
1295 case MFF_IN_PORT:
4e022ec0 1296 flow->in_port.ofp_port = u16_to_ofp(ntohs(value->be16));
28da1f8f
BP
1297 break;
1298
c61f3870
BP
1299 case MFF_IN_PORT_OXM:
1300 ofputil_port_from_ofp11(value->be32, &flow->in_port.ofp_port);
1301 break;
1302 case MFF_ACTSET_OUTPUT:
1303 ofputil_port_from_ofp11(value->be32, &flow->actset_output);
72333065 1304 break;
72333065 1305
1b567fb9
AA
1306 case MFF_SKB_PRIORITY:
1307 flow->skb_priority = ntohl(value->be32);
1308 break;
1309
1362e248
JG
1310 case MFF_PKT_MARK:
1311 flow->pkt_mark = ntohl(value->be32);
1b567fb9
AA
1312 break;
1313
07659514
JS
1314 case MFF_CT_STATE:
1315 flow->ct_state = ntohl(value->be32);
1316 break;
1317
1318 case MFF_CT_ZONE:
1319 flow->ct_zone = ntohs(value->be16);
1320 break;
1321
8e53fe8c
JS
1322 case MFF_CT_MARK:
1323 flow->ct_mark = ntohl(value->be32);
1324 break;
1325
9daf2348 1326 case MFF_CT_LABEL:
32ea15f6 1327 flow->ct_label = ntoh128(value->be128);
9daf2348
JS
1328 break;
1329
daf4d3c1
JR
1330 case MFF_CT_NW_PROTO:
1331 flow->ct_nw_proto = value->u8;
1332 break;
1333
1334 case MFF_CT_NW_SRC:
1335 flow->ct_nw_src = value->be32;
1336 break;
1337
1338 case MFF_CT_NW_DST:
1339 flow->ct_nw_dst = value->be32;
1340 break;
1341
1342 case MFF_CT_IPV6_SRC:
1343 flow->ct_ipv6_src = value->ipv6;
1344 break;
1345
1346 case MFF_CT_IPV6_DST:
1347 flow->ct_ipv6_dst = value->ipv6;
1348 break;
1349
1350 case MFF_CT_TP_SRC:
1351 flow->ct_tp_src = value->be16;
1352 break;
1353
1354 case MFF_CT_TP_DST:
1355 flow->ct_tp_dst = value->be16;
1356 break;
1357
0d7e2fe4 1358 CASE_MFF_REGS:
28da1f8f
BP
1359 flow->regs[mf->id - MFF_REG0] = ntohl(value->be32);
1360 break;
28da1f8f 1361
79fe0f46
BP
1362 CASE_MFF_XREGS:
1363 flow_set_xreg(flow, mf->id - MFF_XREG0, ntohll(value->be64));
1364 break;
1365
b23ada8e
JP
1366 CASE_MFF_XXREGS:
1367 flow_set_xxreg(flow, mf->id - MFF_XXREG0, ntoh128(value->be128));
1368 break;
1369
28da1f8f 1370 case MFF_ETH_SRC:
74ff3298 1371 flow->dl_src = value->mac;
28da1f8f
BP
1372 break;
1373
1374 case MFF_ETH_DST:
74ff3298 1375 flow->dl_dst = value->mac;
28da1f8f
BP
1376 break;
1377
1378 case MFF_ETH_TYPE:
1379 flow->dl_type = value->be16;
1380 break;
1381
1382 case MFF_VLAN_TCI:
1383 flow->vlan_tci = value->be16;
1384 break;
1385
441c57a9 1386 case MFF_DL_VLAN:
fb0451d9 1387 flow_set_dl_vlan(flow, value->be16);
28da1f8f 1388 break;
cc34bc8c
BP
1389 case MFF_VLAN_VID:
1390 flow_set_vlan_vid(flow, value->be16);
1391 break;
28da1f8f 1392
441c57a9 1393 case MFF_DL_VLAN_PCP:
cc34bc8c 1394 case MFF_VLAN_PCP:
28da1f8f
BP
1395 flow_set_vlan_pcp(flow, value->u8);
1396 break;
1397
b02475c5 1398 case MFF_MPLS_LABEL:
8bfd0fda 1399 flow_set_mpls_label(flow, 0, value->be32);
b02475c5
SH
1400 break;
1401
1402 case MFF_MPLS_TC:
8bfd0fda 1403 flow_set_mpls_tc(flow, 0, value->u8);
b02475c5
SH
1404 break;
1405
1406 case MFF_MPLS_BOS:
8bfd0fda
BP
1407 flow_set_mpls_bos(flow, 0, value->u8);
1408 break;
b02475c5 1409
bef3f465
BP
1410 case MFF_MPLS_TTL:
1411 flow_set_mpls_ttl(flow, 0, value->u8);
1412 break;
1413
28da1f8f
BP
1414 case MFF_IPV4_SRC:
1415 flow->nw_src = value->be32;
1416 break;
1417
1418 case MFF_IPV4_DST:
1419 flow->nw_dst = value->be32;
1420 break;
1421
1422 case MFF_IPV6_SRC:
1423 flow->ipv6_src = value->ipv6;
1424 break;
1425
1426 case MFF_IPV6_DST:
1427 flow->ipv6_dst = value->ipv6;
1428 break;
1429
1430 case MFF_IPV6_LABEL:
88cc95c1 1431 flow->ipv6_label = value->be32 & htonl(IPV6_LABEL_MASK);
28da1f8f
BP
1432 break;
1433
1434 case MFF_IP_PROTO:
1435 flow->nw_proto = value->u8;
1436 break;
1437
1438 case MFF_IP_DSCP:
1439 flow->nw_tos &= ~IP_DSCP_MASK;
1440 flow->nw_tos |= value->u8 & IP_DSCP_MASK;
1441 break;
1442
1638b906
BP
1443 case MFF_IP_DSCP_SHIFTED:
1444 flow->nw_tos &= ~IP_DSCP_MASK;
1445 flow->nw_tos |= value->u8 << 2;
1446 break;
1447
28da1f8f
BP
1448 case MFF_IP_ECN:
1449 flow->nw_tos &= ~IP_ECN_MASK;
1450 flow->nw_tos |= value->u8 & IP_ECN_MASK;
1451 break;
1452
1453 case MFF_IP_TTL:
1454 flow->nw_ttl = value->u8;
1455 break;
1456
1457 case MFF_IP_FRAG:
c8abb8af 1458 flow->nw_frag = value->u8 & FLOW_NW_FRAG_MASK;
28da1f8f
BP
1459 break;
1460
1461 case MFF_ARP_OP:
1462 flow->nw_proto = ntohs(value->be16);
1463 break;
1464
1465 case MFF_ARP_SPA:
1466 flow->nw_src = value->be32;
1467 break;
1468
1469 case MFF_ARP_TPA:
1470 flow->nw_dst = value->be32;
1471 break;
1472
1473 case MFF_ARP_SHA:
1474 case MFF_ND_SLL:
74ff3298 1475 flow->arp_sha = value->mac;
28da1f8f
BP
1476 break;
1477
1478 case MFF_ARP_THA:
1479 case MFF_ND_TLL:
74ff3298 1480 flow->arp_tha = value->mac;
28da1f8f
BP
1481 break;
1482
1483 case MFF_TCP_SRC:
1484 case MFF_UDP_SRC:
0d56eaf2 1485 case MFF_SCTP_SRC:
28da1f8f
BP
1486 flow->tp_src = value->be16;
1487 break;
1488
1489 case MFF_TCP_DST:
1490 case MFF_UDP_DST:
0d56eaf2 1491 case MFF_SCTP_DST:
28da1f8f
BP
1492 flow->tp_dst = value->be16;
1493 break;
1494
dc235f7f
JR
1495 case MFF_TCP_FLAGS:
1496 flow->tcp_flags = value->be16;
1497 break;
1498
28da1f8f
BP
1499 case MFF_ICMPV4_TYPE:
1500 case MFF_ICMPV6_TYPE:
1501 flow->tp_src = htons(value->u8);
1502 break;
1503
1504 case MFF_ICMPV4_CODE:
1505 case MFF_ICMPV6_CODE:
1506 flow->tp_dst = htons(value->u8);
1507 break;
1508
1509 case MFF_ND_TARGET:
1510 flow->nd_target = value->ipv6;
1511 break;
1512
1513 case MFF_N_IDS:
1514 default:
428b2edd 1515 OVS_NOT_REACHED();
28da1f8f
BP
1516 }
1517}
1518
7eb4b1f1
BP
1519/* Consider each of 'src', 'mask', and 'dst' as if they were arrays of 8*n
1520 * bits. Then, for each 0 <= i < 8 * n such that mask[i] == 1, sets dst[i] =
1521 * src[i]. */
1522static void
1523apply_mask(const uint8_t *src, const uint8_t *mask, uint8_t *dst, size_t n)
1524{
1525 size_t i;
1526
1527 for (i = 0; i < n; i++) {
1528 dst[i] = (src[i] & mask[i]) | (dst[i] & ~mask[i]);
1529 }
1530}
1531
1532/* Sets 'flow' member field described by 'field' to 'value', except that bits
1533 * for which 'mask' has a 0-bit keep their existing values. The caller is
1534 * responsible for ensuring that 'flow' meets 'field''s prerequisites.*/
1535void
1536mf_set_flow_value_masked(const struct mf_field *field,
1537 const union mf_value *value,
1538 const union mf_value *mask,
1539 struct flow *flow)
1540{
1541 union mf_value tmp;
1542
1543 mf_get_value(field, flow, &tmp);
1544 apply_mask((const uint8_t *) value, (const uint8_t *) mask,
1545 (uint8_t *) &tmp, field->n_bytes);
1546 mf_set_flow_value(field, &tmp, flow);
1547}
1548
1cb20095
JG
1549bool
1550mf_is_tun_metadata(const struct mf_field *mf)
1551{
1552 return mf->id >= MFF_TUN_METADATA0 &&
1553 mf->id < MFF_TUN_METADATA0 + TUN_METADATA_NUM_OPTS;
1554}
1555
1556/* Returns true if 'mf' has previously been set in 'flow', false if
1557 * it contains a non-default value.
ccbe50f8
BP
1558 *
1559 * The caller is responsible for ensuring that 'flow' meets 'mf''s
1560 * prerequisites. */
1561bool
1cb20095 1562mf_is_set(const struct mf_field *mf, const struct flow *flow)
ccbe50f8 1563{
1cb20095
JG
1564 if (!mf_is_tun_metadata(mf)) {
1565 union mf_value value;
ccbe50f8 1566
1cb20095
JG
1567 mf_get_value(mf, flow, &value);
1568 return !is_all_zeros(&value, mf->n_bytes);
1569 } else {
1570 return ULLONG_GET(flow->tunnel.metadata.present.map,
1571 mf->id - MFF_TUN_METADATA0);
1572 }
ccbe50f8
BP
1573}
1574
81a76618 1575/* Makes 'match' wildcard field 'mf'.
6a885fd0 1576 *
81a76618 1577 * The caller is responsible for ensuring that 'match' meets 'mf''s
4f7b100c
JG
1578 * prerequisites.
1579 *
1580 * If non-NULL, 'err_str' returns a malloc'ed string describing any errors
1581 * with the request or NULL if there is no error. The caller is reponsible
1582 * for freeing the string. */
6a885fd0 1583void
4f7b100c 1584mf_set_wild(const struct mf_field *mf, struct match *match, char **err_str)
6a885fd0 1585{
4f7b100c
JG
1586 if (err_str) {
1587 *err_str = NULL;
1588 }
1589
6a885fd0 1590 switch (mf->id) {
a79f29f2
AZ
1591 case MFF_DP_HASH:
1592 match->flow.dp_hash = 0;
1593 match->wc.masks.dp_hash = 0;
1594 break;
1595 case MFF_RECIRC_ID:
1596 match->flow.recirc_id = 0;
1597 match->wc.masks.recirc_id = 0;
1598 break;
18080541
BP
1599 case MFF_CONJ_ID:
1600 match->flow.conj_id = 0;
1601 match->wc.masks.conj_id = 0;
1602 break;
6a885fd0 1603 case MFF_TUN_ID:
81a76618 1604 match_set_tun_id_masked(match, htonll(0), htonll(0));
6a885fd0 1605 break;
4fe3445a
PS
1606 case MFF_TUN_SRC:
1607 match_set_tun_src_masked(match, htonl(0), htonl(0));
1608 break;
1609 case MFF_TUN_DST:
1610 match_set_tun_dst_masked(match, htonl(0), htonl(0));
1611 break;
7dad8e9a
TLSC
1612 case MFF_TUN_IPV6_SRC:
1613 memset(&match->wc.masks.tunnel.ipv6_src, 0,
1614 sizeof match->wc.masks.tunnel.ipv6_src);
1615 memset(&match->flow.tunnel.ipv6_src, 0,
1616 sizeof match->flow.tunnel.ipv6_src);
1617 break;
1618 case MFF_TUN_IPV6_DST:
1619 memset(&match->wc.masks.tunnel.ipv6_dst, 0,
1620 sizeof match->wc.masks.tunnel.ipv6_dst);
1621 memset(&match->flow.tunnel.ipv6_dst, 0,
1622 sizeof match->flow.tunnel.ipv6_dst);
1623 break;
4fe3445a
PS
1624 case MFF_TUN_FLAGS:
1625 match_set_tun_flags_masked(match, 0, 0);
1626 break;
ac6073e3
MC
1627 case MFF_TUN_GBP_ID:
1628 match_set_tun_gbp_id_masked(match, 0, 0);
1629 break;
1630 case MFF_TUN_GBP_FLAGS:
1631 match_set_tun_gbp_flags_masked(match, 0, 0);
1632 break;
4fe3445a
PS
1633 case MFF_TUN_TOS:
1634 match_set_tun_tos_masked(match, 0, 0);
1635 break;
1636 case MFF_TUN_TTL:
1637 match_set_tun_ttl_masked(match, 0, 0);
1638 break;
9558d2a5 1639 CASE_MFF_TUN_METADATA:
4f7b100c 1640 tun_metadata_set_match(mf, NULL, NULL, match, err_str);
9558d2a5 1641 break;
4fe3445a 1642
969fc56c 1643 case MFF_METADATA:
81a76618 1644 match_set_metadata_masked(match, htonll(0), htonll(0));
801d2b2a 1645 break;
6a885fd0
BP
1646
1647 case MFF_IN_PORT:
72333065 1648 case MFF_IN_PORT_OXM:
4e022ec0
AW
1649 match->flow.in_port.ofp_port = 0;
1650 match->wc.masks.in_port.ofp_port = 0;
6a885fd0 1651 break;
c61f3870
BP
1652 case MFF_ACTSET_OUTPUT:
1653 match->flow.actset_output = 0;
1654 match->wc.masks.actset_output = 0;
1655 break;
6a885fd0 1656
1b567fb9
AA
1657 case MFF_SKB_PRIORITY:
1658 match->flow.skb_priority = 0;
1659 match->wc.masks.skb_priority = 0;
1660 break;
1661
1362e248
JG
1662 case MFF_PKT_MARK:
1663 match->flow.pkt_mark = 0;
1664 match->wc.masks.pkt_mark = 0;
1b567fb9
AA
1665 break;
1666
07659514
JS
1667 case MFF_CT_STATE:
1668 match->flow.ct_state = 0;
1669 match->wc.masks.ct_state = 0;
1670 break;
1671
1672 case MFF_CT_ZONE:
1673 match->flow.ct_zone = 0;
1674 match->wc.masks.ct_zone = 0;
1675 break;
1676
8e53fe8c
JS
1677 case MFF_CT_MARK:
1678 match->flow.ct_mark = 0;
1679 match->wc.masks.ct_mark = 0;
1680 break;
1681
9daf2348
JS
1682 case MFF_CT_LABEL:
1683 memset(&match->flow.ct_label, 0, sizeof(match->flow.ct_label));
1684 memset(&match->wc.masks.ct_label, 0, sizeof(match->wc.masks.ct_label));
1685 break;
1686
daf4d3c1
JR
1687 case MFF_CT_NW_PROTO:
1688 match->flow.ct_nw_proto = 0;
1689 match->wc.masks.ct_nw_proto = 0;
1690 break;
1691
1692 case MFF_CT_NW_SRC:
1693 match->flow.ct_nw_src = 0;
1694 match->wc.masks.ct_nw_src = 0;
1695 break;
1696
1697 case MFF_CT_NW_DST:
1698 match->flow.ct_nw_dst = 0;
1699 match->wc.masks.ct_nw_dst = 0;
1700 break;
1701
1702 case MFF_CT_IPV6_SRC:
1703 memset(&match->flow.ct_ipv6_src, 0, sizeof(match->flow.ct_ipv6_src));
1704 WC_UNMASK_FIELD(&match->wc, ct_ipv6_src);
1705 break;
1706
1707 case MFF_CT_IPV6_DST:
1708 memset(&match->flow.ct_ipv6_dst, 0, sizeof(match->flow.ct_ipv6_dst));
1709 WC_UNMASK_FIELD(&match->wc, ct_ipv6_dst);
1710 break;
1711
1712 case MFF_CT_TP_SRC:
1713 match->flow.ct_tp_src = 0;
1714 match->wc.masks.ct_tp_src = 0;
1715 break;
1716
1717 case MFF_CT_TP_DST:
1718 match->flow.ct_tp_dst = 0;
1719 match->wc.masks.ct_tp_dst = 0;
1720 break;
1721
0d7e2fe4 1722 CASE_MFF_REGS:
81a76618 1723 match_set_reg_masked(match, mf->id - MFF_REG0, 0, 0);
e9358af6 1724 break;
6a885fd0 1725
79fe0f46
BP
1726 CASE_MFF_XREGS:
1727 match_set_xreg_masked(match, mf->id - MFF_XREG0, 0, 0);
1728 break;
1729
b23ada8e
JP
1730 CASE_MFF_XXREGS: {
1731 match_set_xxreg_masked(match, mf->id - MFF_XXREG0, OVS_U128_ZERO,
1732 OVS_U128_ZERO);
1733 break;
1734 }
1735
6a885fd0 1736 case MFF_ETH_SRC:
74ff3298
JR
1737 match->flow.dl_src = eth_addr_zero;
1738 match->wc.masks.dl_src = eth_addr_zero;
6a885fd0
BP
1739 break;
1740
1741 case MFF_ETH_DST:
74ff3298
JR
1742 match->flow.dl_dst = eth_addr_zero;
1743 match->wc.masks.dl_dst = eth_addr_zero;
6a885fd0
BP
1744 break;
1745
1746 case MFF_ETH_TYPE:
81a76618
BP
1747 match->flow.dl_type = htons(0);
1748 match->wc.masks.dl_type = htons(0);
6a885fd0
BP
1749 break;
1750
1751 case MFF_VLAN_TCI:
81a76618 1752 match_set_dl_tci_masked(match, htons(0), htons(0));
6a885fd0
BP
1753 break;
1754
441c57a9 1755 case MFF_DL_VLAN:
cc34bc8c 1756 case MFF_VLAN_VID:
81a76618 1757 match_set_any_vid(match);
6a885fd0
BP
1758 break;
1759
441c57a9 1760 case MFF_DL_VLAN_PCP:
cc34bc8c 1761 case MFF_VLAN_PCP:
81a76618 1762 match_set_any_pcp(match);
6a885fd0
BP
1763 break;
1764
b02475c5 1765 case MFF_MPLS_LABEL:
8bfd0fda 1766 match_set_any_mpls_label(match, 0);
b02475c5
SH
1767 break;
1768
1769 case MFF_MPLS_TC:
8bfd0fda 1770 match_set_any_mpls_tc(match, 0);
b02475c5
SH
1771 break;
1772
1773 case MFF_MPLS_BOS:
8bfd0fda
BP
1774 match_set_any_mpls_bos(match, 0);
1775 break;
b02475c5 1776
bef3f465
BP
1777 case MFF_MPLS_TTL:
1778 match_set_any_mpls_ttl(match, 0);
1779 break;
1780
6a885fd0
BP
1781 case MFF_IPV4_SRC:
1782 case MFF_ARP_SPA:
81a76618 1783 match_set_nw_src_masked(match, htonl(0), htonl(0));
6a885fd0
BP
1784 break;
1785
1786 case MFF_IPV4_DST:
1787 case MFF_ARP_TPA:
81a76618 1788 match_set_nw_dst_masked(match, htonl(0), htonl(0));
6a885fd0
BP
1789 break;
1790
1791 case MFF_IPV6_SRC:
81a76618
BP
1792 memset(&match->wc.masks.ipv6_src, 0, sizeof match->wc.masks.ipv6_src);
1793 memset(&match->flow.ipv6_src, 0, sizeof match->flow.ipv6_src);
6a885fd0
BP
1794 break;
1795
1796 case MFF_IPV6_DST:
81a76618
BP
1797 memset(&match->wc.masks.ipv6_dst, 0, sizeof match->wc.masks.ipv6_dst);
1798 memset(&match->flow.ipv6_dst, 0, sizeof match->flow.ipv6_dst);
6a885fd0
BP
1799 break;
1800
fa8223b7 1801 case MFF_IPV6_LABEL:
81a76618
BP
1802 match->wc.masks.ipv6_label = htonl(0);
1803 match->flow.ipv6_label = htonl(0);
fa8223b7
JP
1804 break;
1805
6a885fd0 1806 case MFF_IP_PROTO:
81a76618
BP
1807 match->wc.masks.nw_proto = 0;
1808 match->flow.nw_proto = 0;
6a885fd0
BP
1809 break;
1810
530180fd 1811 case MFF_IP_DSCP:
1638b906 1812 case MFF_IP_DSCP_SHIFTED:
81a76618
BP
1813 match->wc.masks.nw_tos &= ~IP_DSCP_MASK;
1814 match->flow.nw_tos &= ~IP_DSCP_MASK;
7257b535
BP
1815 break;
1816
530180fd 1817 case MFF_IP_ECN:
81a76618
BP
1818 match->wc.masks.nw_tos &= ~IP_ECN_MASK;
1819 match->flow.nw_tos &= ~IP_ECN_MASK;
530180fd
JP
1820 break;
1821
a61680c6 1822 case MFF_IP_TTL:
81a76618
BP
1823 match->wc.masks.nw_ttl = 0;
1824 match->flow.nw_ttl = 0;
a61680c6
JP
1825 break;
1826
7257b535 1827 case MFF_IP_FRAG:
6ca49d5e 1828 match->wc.masks.nw_frag &= ~FLOW_NW_FRAG_MASK;
81a76618 1829 match->flow.nw_frag &= ~FLOW_NW_FRAG_MASK;
6a885fd0
BP
1830 break;
1831
1832 case MFF_ARP_OP:
81a76618
BP
1833 match->wc.masks.nw_proto = 0;
1834 match->flow.nw_proto = 0;
6a885fd0
BP
1835 break;
1836
1837 case MFF_ARP_SHA:
1838 case MFF_ND_SLL:
74ff3298
JR
1839 match->flow.arp_sha = eth_addr_zero;
1840 match->wc.masks.arp_sha = eth_addr_zero;
6a885fd0
BP
1841 break;
1842
1843 case MFF_ARP_THA:
1844 case MFF_ND_TLL:
74ff3298
JR
1845 match->flow.arp_tha = eth_addr_zero;
1846 match->wc.masks.arp_tha = eth_addr_zero;
6a885fd0
BP
1847 break;
1848
1849 case MFF_TCP_SRC:
1850 case MFF_UDP_SRC:
0d56eaf2 1851 case MFF_SCTP_SRC:
268a95e0
BP
1852 case MFF_ICMPV4_TYPE:
1853 case MFF_ICMPV6_TYPE:
81a76618
BP
1854 match->wc.masks.tp_src = htons(0);
1855 match->flow.tp_src = htons(0);
6a885fd0
BP
1856 break;
1857
1858 case MFF_TCP_DST:
1859 case MFF_UDP_DST:
0d56eaf2 1860 case MFF_SCTP_DST:
268a95e0
BP
1861 case MFF_ICMPV4_CODE:
1862 case MFF_ICMPV6_CODE:
81a76618
BP
1863 match->wc.masks.tp_dst = htons(0);
1864 match->flow.tp_dst = htons(0);
6a885fd0
BP
1865 break;
1866
dc235f7f
JR
1867 case MFF_TCP_FLAGS:
1868 match->wc.masks.tcp_flags = htons(0);
1869 match->flow.tcp_flags = htons(0);
1870 break;
1871
6a885fd0 1872 case MFF_ND_TARGET:
81a76618
BP
1873 memset(&match->wc.masks.nd_target, 0,
1874 sizeof match->wc.masks.nd_target);
1875 memset(&match->flow.nd_target, 0, sizeof match->flow.nd_target);
6a885fd0
BP
1876 break;
1877
1878 case MFF_N_IDS:
1879 default:
428b2edd 1880 OVS_NOT_REACHED();
6a885fd0
BP
1881 }
1882}
1883
81a76618 1884/* Makes 'match' match field 'mf' with the specified 'value' and 'mask'.
6a885fd0
BP
1885 * 'value' specifies a value to match and 'mask' specifies a wildcard pattern,
1886 * with a 1-bit indicating that the corresponding value bit must match and a
1887 * 0-bit indicating a don't-care.
1888 *
1889 * If 'mask' is NULL or points to all-1-bits, then this call is equivalent to
81a76618
BP
1890 * mf_set_value(mf, value, match). If 'mask' points to all-0-bits, then this
1891 * call is equivalent to mf_set_wild(mf, match).
6a885fd0
BP
1892 *
1893 * 'mask' must be a valid mask for 'mf' (see mf_is_mask_valid()). The caller
4f7b100c
JG
1894 * is responsible for ensuring that 'match' meets 'mf''s prerequisites.
1895 *
1896 * If non-NULL, 'err_str' returns a malloc'ed string describing any errors
1897 * with the request or NULL if there is no error. The caller is reponsible
1fd58977
JR
1898 * for freeing the string.
1899 *
1900 * Return a set of enum ofputil_protocol bits (as an uint32_t to avoid circular
1901 * dependency on enum ofputil_protocol definition) indicating which OpenFlow
1902 * protocol versions can support this functionality. */
1903uint32_t
6a885fd0
BP
1904mf_set(const struct mf_field *mf,
1905 const union mf_value *value, const union mf_value *mask,
4f7b100c 1906 struct match *match, char **err_str)
6a885fd0 1907{
53cb9c3e 1908 if (!mask || is_all_ones(mask, mf->n_bytes)) {
4f7b100c 1909 mf_set_value(mf, value, match, err_str);
a4ce8b25 1910 return mf->usable_protocols_exact;
1cb20095
JG
1911 } else if (is_all_zeros(mask, mf->n_bytes) && !mf_is_tun_metadata(mf)) {
1912 /* Tunnel metadata matches on the existence of the field itself, so
1913 * it still needs to be encoded even if the value is wildcarded. */
4f7b100c 1914 mf_set_wild(mf, match, err_str);
db0b6c29 1915 return OFPUTIL_P_ANY;
6a885fd0
BP
1916 }
1917
4f7b100c
JG
1918 if (err_str) {
1919 *err_str = NULL;
1920 }
1921
6a885fd0 1922 switch (mf->id) {
07659514 1923 case MFF_CT_ZONE:
daf4d3c1
JR
1924 case MFF_CT_NW_PROTO:
1925 case MFF_CT_NW_SRC:
1926 case MFF_CT_NW_DST:
1927 case MFF_CT_IPV6_SRC:
1928 case MFF_CT_IPV6_DST:
1929 case MFF_CT_TP_SRC:
1930 case MFF_CT_TP_DST:
a79f29f2 1931 case MFF_RECIRC_ID:
18080541 1932 case MFF_CONJ_ID:
6a885fd0 1933 case MFF_IN_PORT:
72333065 1934 case MFF_IN_PORT_OXM:
c61f3870 1935 case MFF_ACTSET_OUTPUT:
1b567fb9 1936 case MFF_SKB_PRIORITY:
6a885fd0 1937 case MFF_ETH_TYPE:
441c57a9
BP
1938 case MFF_DL_VLAN:
1939 case MFF_DL_VLAN_PCP:
cc34bc8c 1940 case MFF_VLAN_PCP:
b02475c5
SH
1941 case MFF_MPLS_LABEL:
1942 case MFF_MPLS_TC:
1943 case MFF_MPLS_BOS:
bef3f465 1944 case MFF_MPLS_TTL:
6a885fd0 1945 case MFF_IP_PROTO:
a61680c6 1946 case MFF_IP_TTL:
530180fd 1947 case MFF_IP_DSCP:
1638b906 1948 case MFF_IP_DSCP_SHIFTED:
530180fd 1949 case MFF_IP_ECN:
6a885fd0 1950 case MFF_ARP_OP:
268a95e0
BP
1951 case MFF_ICMPV4_TYPE:
1952 case MFF_ICMPV4_CODE:
1953 case MFF_ICMPV6_TYPE:
1954 case MFF_ICMPV6_CODE:
af69bf19 1955 return OFPUTIL_P_NONE;
6a885fd0 1956
a79f29f2
AZ
1957 case MFF_DP_HASH:
1958 match_set_dp_hash_masked(match, ntohl(value->be32), ntohl(mask->be32));
1959 break;
6a885fd0 1960 case MFF_TUN_ID:
81a76618 1961 match_set_tun_id_masked(match, value->be64, mask->be64);
6a885fd0 1962 break;
4fe3445a
PS
1963 case MFF_TUN_SRC:
1964 match_set_tun_src_masked(match, value->be32, mask->be32);
1965 break;
1966 case MFF_TUN_DST:
1967 match_set_tun_dst_masked(match, value->be32, mask->be32);
1968 break;
7dad8e9a
TLSC
1969 case MFF_TUN_IPV6_SRC:
1970 match_set_tun_ipv6_src_masked(match, &value->ipv6, &mask->ipv6);
1971 break;
1972 case MFF_TUN_IPV6_DST:
1973 match_set_tun_ipv6_dst_masked(match, &value->ipv6, &mask->ipv6);
1974 break;
4fe3445a
PS
1975 case MFF_TUN_FLAGS:
1976 match_set_tun_flags_masked(match, ntohs(value->be16), ntohs(mask->be16));
1977 break;
ac6073e3
MC
1978 case MFF_TUN_GBP_ID:
1979 match_set_tun_gbp_id_masked(match, value->be16, mask->be16);
1980 break;
1981 case MFF_TUN_GBP_FLAGS:
1982 match_set_tun_gbp_flags_masked(match, value->u8, mask->u8);
1983 break;
4fe3445a
PS
1984 case MFF_TUN_TTL:
1985 match_set_tun_ttl_masked(match, value->u8, mask->u8);
1986 break;
1987 case MFF_TUN_TOS:
1988 match_set_tun_tos_masked(match, value->u8, mask->u8);
1989 break;
9558d2a5 1990 CASE_MFF_TUN_METADATA:
4f7b100c 1991 tun_metadata_set_match(mf, value, mask, match, err_str);
9558d2a5 1992 break;
4fe3445a 1993
969fc56c 1994 case MFF_METADATA:
81a76618 1995 match_set_metadata_masked(match, value->be64, mask->be64);
969fc56c 1996 break;
6a885fd0 1997
0d7e2fe4 1998 CASE_MFF_REGS:
81a76618
BP
1999 match_set_reg_masked(match, mf->id - MFF_REG0,
2000 ntohl(value->be32), ntohl(mask->be32));
6a885fd0 2001 break;
ac923e91 2002
79fe0f46
BP
2003 CASE_MFF_XREGS:
2004 match_set_xreg_masked(match, mf->id - MFF_XREG0,
2005 ntohll(value->be64), ntohll(mask->be64));
2006 break;
2007
b23ada8e
JP
2008 CASE_MFF_XXREGS: {
2009 match_set_xxreg_masked(match, mf->id - MFF_XXREG0,
2010 ntoh128(value->be128), ntoh128(mask->be128));
2011 break;
2012 }
2013
ac923e91
JG
2014 case MFF_PKT_MARK:
2015 match_set_pkt_mark_masked(match, ntohl(value->be32),
2016 ntohl(mask->be32));
2017 break;
6a885fd0 2018
07659514
JS
2019 case MFF_CT_STATE:
2020 match_set_ct_state_masked(match, ntohl(value->be32), ntohl(mask->be32));
2021 break;
2022
8e53fe8c
JS
2023 case MFF_CT_MARK:
2024 match_set_ct_mark_masked(match, ntohl(value->be32), ntohl(mask->be32));
2025 break;
2026
32ea15f6
JP
2027 case MFF_CT_LABEL:
2028 match_set_ct_label_masked(match, ntoh128(value->be128),
2029 mask ? ntoh128(mask->be128) : OVS_U128_MAX);
9daf2348 2030 break;
9daf2348 2031
6a885fd0 2032 case MFF_ETH_DST:
81a76618 2033 match_set_dl_dst_masked(match, value->mac, mask->mac);
73c0ce34
JS
2034 break;
2035
2036 case MFF_ETH_SRC:
81a76618 2037 match_set_dl_src_masked(match, value->mac, mask->mac);
6a885fd0
BP
2038 break;
2039
e878338b
SH
2040 case MFF_ARP_SHA:
2041 case MFF_ND_SLL:
81a76618 2042 match_set_arp_sha_masked(match, value->mac, mask->mac);
e878338b
SH
2043 break;
2044
2045 case MFF_ARP_THA:
2046 case MFF_ND_TLL:
81a76618 2047 match_set_arp_tha_masked(match, value->mac, mask->mac);
e878338b
SH
2048 break;
2049
6a885fd0 2050 case MFF_VLAN_TCI:
81a76618 2051 match_set_dl_tci_masked(match, value->be16, mask->be16);
6a885fd0
BP
2052 break;
2053
cc34bc8c 2054 case MFF_VLAN_VID:
81a76618 2055 match_set_vlan_vid_masked(match, value->be16, mask->be16);
cc34bc8c
BP
2056 break;
2057
6a885fd0 2058 case MFF_IPV4_SRC:
81a76618 2059 match_set_nw_src_masked(match, value->be32, mask->be32);
a4ce8b25 2060 break;
6a885fd0
BP
2061
2062 case MFF_IPV4_DST:
81a76618 2063 match_set_nw_dst_masked(match, value->be32, mask->be32);
a4ce8b25 2064 break;
6a885fd0
BP
2065
2066 case MFF_IPV6_SRC:
81a76618 2067 match_set_ipv6_src_masked(match, &value->ipv6, &mask->ipv6);
6a885fd0
BP
2068 break;
2069
2070 case MFF_IPV6_DST:
81a76618 2071 match_set_ipv6_dst_masked(match, &value->ipv6, &mask->ipv6);
6a885fd0
BP
2072 break;
2073
32455024
SH
2074 case MFF_IPV6_LABEL:
2075 if ((mask->be32 & htonl(IPV6_LABEL_MASK)) == htonl(IPV6_LABEL_MASK)) {
4f7b100c 2076 mf_set_value(mf, value, match, err_str);
32455024 2077 } else {
81a76618 2078 match_set_ipv6_label_masked(match, value->be32, mask->be32);
32455024
SH
2079 }
2080 break;
2081
47284b1f 2082 case MFF_ND_TARGET:
81a76618 2083 match_set_nd_target_masked(match, &value->ipv6, &mask->ipv6);
47284b1f
AA
2084 break;
2085
7257b535 2086 case MFF_IP_FRAG:
81a76618 2087 match_set_nw_frag_masked(match, value->u8, mask->u8);
7257b535
BP
2088 break;
2089
6a885fd0 2090 case MFF_ARP_SPA:
81a76618 2091 match_set_nw_src_masked(match, value->be32, mask->be32);
a4ce8b25 2092 break;
6a885fd0
BP
2093
2094 case MFF_ARP_TPA:
81a76618 2095 match_set_nw_dst_masked(match, value->be32, mask->be32);
a4ce8b25 2096 break;
6a885fd0 2097
73f33563
BP
2098 case MFF_TCP_SRC:
2099 case MFF_UDP_SRC:
0d56eaf2 2100 case MFF_SCTP_SRC:
81a76618 2101 match_set_tp_src_masked(match, value->be16, mask->be16);
73f33563
BP
2102 break;
2103
2104 case MFF_TCP_DST:
2105 case MFF_UDP_DST:
0d56eaf2 2106 case MFF_SCTP_DST:
81a76618 2107 match_set_tp_dst_masked(match, value->be16, mask->be16);
73f33563
BP
2108 break;
2109
dc235f7f
JR
2110 case MFF_TCP_FLAGS:
2111 match_set_tcp_flags_masked(match, value->be16, mask->be16);
2112 break;
2113
6a885fd0
BP
2114 case MFF_N_IDS:
2115 default:
428b2edd 2116 OVS_NOT_REACHED();
6a885fd0 2117 }
db0b6c29 2118
a4ce8b25
BP
2119 return ((mf->usable_protocols_bitwise == mf->usable_protocols_cidr
2120 || ip_is_cidr(mask->be32))
2121 ? mf->usable_protocols_cidr
2122 : mf->usable_protocols_bitwise);
6a885fd0
BP
2123}
2124
816fd533 2125static enum ofperr
67210a55 2126mf_check__(const struct mf_subfield *sf, const struct match *match,
816fd533 2127 const char *type)
6a885fd0 2128{
816fd533
BP
2129 if (!sf->field) {
2130 VLOG_WARN_RL(&rl, "unknown %s field", type);
9e404d1e 2131 return OFPERR_OFPBAC_BAD_SET_TYPE;
816fd533
BP
2132 } else if (!sf->n_bits) {
2133 VLOG_WARN_RL(&rl, "zero bit %s field %s", type, sf->field->name);
9e404d1e 2134 return OFPERR_OFPBAC_BAD_SET_LEN;
816fd533
BP
2135 } else if (sf->ofs >= sf->field->n_bits) {
2136 VLOG_WARN_RL(&rl, "bit offset %d exceeds %d-bit width of %s field %s",
2137 sf->ofs, sf->field->n_bits, type, sf->field->name);
9e404d1e 2138 return OFPERR_OFPBAC_BAD_SET_LEN;
816fd533
BP
2139 } else if (sf->ofs + sf->n_bits > sf->field->n_bits) {
2140 VLOG_WARN_RL(&rl, "bit offset %d and width %d exceeds %d-bit width "
2141 "of %s field %s", sf->ofs, sf->n_bits,
2142 sf->field->n_bits, type, sf->field->name);
9e404d1e 2143 return OFPERR_OFPBAC_BAD_SET_LEN;
67210a55 2144 } else if (match && !mf_are_match_prereqs_ok(sf->field, match)) {
816fd533
BP
2145 VLOG_WARN_RL(&rl, "%s field %s lacks correct prerequisites",
2146 type, sf->field->name);
9e404d1e 2147 return OFPERR_OFPBAC_MATCH_INCONSISTENT;
6a885fd0 2148 } else {
816fd533
BP
2149 return 0;
2150 }
816fd533 2151}
6a885fd0 2152
67842eba
BP
2153/* Sets all the bits in 'sf' to 1 within 'wc', if 'wc' is nonnull. */
2154static void
2155unwildcard_subfield(const struct mf_subfield *sf, struct flow_wildcards *wc)
2156{
2157 if (wc) {
2158 union mf_value mask;
2159
2160 memset(&mask, 0, sizeof mask);
2161 bitwise_one(&mask, sf->field->n_bytes, sf->ofs, sf->n_bits);
2162 mf_mask_field_masked(sf->field, &mask, wc);
2163 }
2164}
2165
2166/* Copies 'src' into 'dst' within 'flow', and sets all the bits in 'src' and
2167 * 'dst' to 1s in 'wc', if 'wc' is nonnull.
2168 *
2169 * 'src' and 'dst' may overlap. */
2170void
2171mf_subfield_copy(const struct mf_subfield *src,
2172 const struct mf_subfield *dst,
2173 struct flow *flow, struct flow_wildcards *wc)
2174{
2175 ovs_assert(src->n_bits == dst->n_bits);
2176 if (mf_are_prereqs_ok(dst->field, flow, wc)
2177 && mf_are_prereqs_ok(src->field, flow, wc)) {
2178 unwildcard_subfield(src, wc);
2179 unwildcard_subfield(dst, wc);
2180
2181 union mf_value src_value;
2182 union mf_value dst_value;
2183 mf_get_value(dst->field, flow, &dst_value);
2184 mf_get_value(src->field, flow, &src_value);
2185 bitwise_copy(&src_value, src->field->n_bytes, src->ofs,
2186 &dst_value, dst->field->n_bytes, dst->ofs,
2187 src->n_bits);
2188 mf_set_flow_value(dst->field, &dst_value, flow);
2189 }
2190}
2191
2192/* Swaps the bits in 'src' and 'dst' within 'flow', and sets all the bits in
2193 * 'src' and 'dst' to 1s in 'wc', if 'wc' is nonnull.
2194 *
2195 * 'src' and 'dst' may overlap. */
2196void
2197mf_subfield_swap(const struct mf_subfield *a,
2198 const struct mf_subfield *b,
2199 struct flow *flow, struct flow_wildcards *wc)
2200{
2201 ovs_assert(a->n_bits == b->n_bits);
2202 if (mf_are_prereqs_ok(a->field, flow, wc)
2203 && mf_are_prereqs_ok(b->field, flow, wc)) {
2204 unwildcard_subfield(a, wc);
2205 unwildcard_subfield(b, wc);
2206
2207 union mf_value a_value;
2208 union mf_value b_value;
2209 mf_get_value(a->field, flow, &a_value);
2210 mf_get_value(b->field, flow, &b_value);
2211 union mf_value b2_value = b_value;
2212
2213 /* Copy 'a' into 'b'. */
2214 bitwise_copy(&a_value, a->field->n_bytes, a->ofs,
2215 &b_value, b->field->n_bytes, b->ofs,
2216 a->n_bits);
2217 mf_set_flow_value(b->field, &b_value, flow);
2218
2219 /* Copy original 'b' into 'a'. */
2220 bitwise_copy(&b2_value, b->field->n_bytes, b->ofs,
2221 &a_value, a->field->n_bytes, a->ofs,
2222 b->n_bits);
2223 mf_set_flow_value(a->field, &a_value, flow);
2224 }
2225}
2226
816fd533
BP
2227/* Checks whether 'sf' is valid for reading a subfield out of 'flow'. Returns
2228 * 0 if so, otherwise an OpenFlow error code (e.g. as returned by
2229 * ofp_mkerr()). */
2230enum ofperr
67210a55 2231mf_check_src(const struct mf_subfield *sf, const struct match *match)
816fd533 2232{
67210a55 2233 return mf_check__(sf, match, "source");
816fd533 2234}
6a885fd0 2235
816fd533
BP
2236/* Checks whether 'sf' is valid for writing a subfield into 'flow'. Returns 0
2237 * if so, otherwise an OpenFlow error code (e.g. as returned by
2238 * ofp_mkerr()). */
2239enum ofperr
67210a55 2240mf_check_dst(const struct mf_subfield *sf, const struct match *match)
816fd533 2241{
67210a55 2242 int error = mf_check__(sf, match, "destination");
816fd533
BP
2243 if (!error && !sf->field->writable) {
2244 VLOG_WARN_RL(&rl, "destination field %s is not writable",
2245 sf->field->name);
9e404d1e 2246 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
6a885fd0 2247 }
816fd533 2248 return error;
6a885fd0
BP
2249}
2250
81a76618 2251/* Copies the value and wildcard bit pattern for 'mf' from 'match' into the
6a885fd0
BP
2252 * 'value' and 'mask', respectively. */
2253void
81a76618 2254mf_get(const struct mf_field *mf, const struct match *match,
6a885fd0
BP
2255 union mf_value *value, union mf_value *mask)
2256{
81a76618
BP
2257 mf_get_value(mf, &match->flow, value);
2258 mf_get_mask(mf, &match->wc, mask);
6a885fd0
BP
2259}
2260
6a885fd0
BP
2261static char *
2262mf_from_integer_string(const struct mf_field *mf, const char *s,
2263 uint8_t *valuep, uint8_t *maskp)
2264{
6a885fd0 2265 char *tail;
1734bf29
JG
2266 const char *err_str = "";
2267 int err;
6a885fd0 2268
1734bf29
JG
2269 err = parse_int_string(s, valuep, mf->n_bytes, &tail);
2270 if (err || (*tail != '\0' && *tail != '/')) {
2271 err_str = "value";
6a885fd0
BP
2272 goto syntax_error;
2273 }
2274
2275 if (*tail == '/') {
1734bf29
JG
2276 err = parse_int_string(tail + 1, maskp, mf->n_bytes, &tail);
2277 if (err || *tail != '\0') {
2278 err_str = "mask";
6a885fd0
BP
2279 goto syntax_error;
2280 }
2281 } else {
1734bf29 2282 memset(maskp, 0xff, mf->n_bytes);
6a885fd0
BP
2283 }
2284
6a885fd0
BP
2285 return NULL;
2286
2287syntax_error:
1734bf29
JG
2288 if (err == ERANGE) {
2289 return xasprintf("%s: %s too large for %u-byte field %s",
2290 s, err_str, mf->n_bytes, mf->name);
2291 } else {
2292 return xasprintf("%s: bad syntax for %s %s", s, mf->name, err_str);
2293 }
6a885fd0
BP
2294}
2295
2296static char *
2297mf_from_ethernet_string(const struct mf_field *mf, const char *s,
74ff3298 2298 struct eth_addr *mac, struct eth_addr *mask)
6a885fd0 2299{
279fb135 2300 int n;
6a885fd0 2301
279fb135 2302 ovs_assert(mf->n_bytes == ETH_ADDR_LEN);
6a885fd0 2303
279fb135 2304 n = -1;
74ff3298 2305 if (ovs_scan(s, ETH_ADDR_SCAN_FMT"%n", ETH_ADDR_SCAN_ARGS(*mac), &n)
279fb135 2306 && n == strlen(s)) {
74ff3298 2307 *mask = eth_addr_exact;
6a885fd0 2308 return NULL;
279fb135 2309 }
6a885fd0 2310
279fb135 2311 n = -1;
c2c28dfd 2312 if (ovs_scan(s, ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT"%n",
74ff3298 2313 ETH_ADDR_SCAN_ARGS(*mac), ETH_ADDR_SCAN_ARGS(*mask), &n)
279fb135
BP
2314 && n == strlen(s)) {
2315 return NULL;
6a885fd0 2316 }
279fb135
BP
2317
2318 return xasprintf("%s: invalid Ethernet address", s);
6a885fd0
BP
2319}
2320
2321static char *
2322mf_from_ipv4_string(const struct mf_field *mf, const char *s,
2323 ovs_be32 *ip, ovs_be32 *mask)
2324{
cb22974d 2325 ovs_assert(mf->n_bytes == sizeof *ip);
61440451 2326 return ip_parse_masked(s, ip, mask);
6a885fd0
BP
2327}
2328
2329static char *
2330mf_from_ipv6_string(const struct mf_field *mf, const char *s,
10f61b6d 2331 struct in6_addr *ipv6, struct in6_addr *mask)
6a885fd0 2332{
10f61b6d
JP
2333 ovs_assert(mf->n_bytes == sizeof *ipv6);
2334 return ipv6_parse_masked(s, ipv6, mask);
6a885fd0
BP
2335}
2336
2337static char *
2338mf_from_ofp_port_string(const struct mf_field *mf, const char *s,
2339 ovs_be16 *valuep, ovs_be16 *maskp)
2340{
4e022ec0 2341 ofp_port_t port;
6a885fd0 2342
cb22974d 2343 ovs_assert(mf->n_bytes == sizeof(ovs_be16));
33ab38d9
JR
2344
2345 if (ofputil_port_from_string(s, &port)) {
4e022ec0 2346 *valuep = htons(ofp_to_u16(port));
b8266395 2347 *maskp = OVS_BE16_MAX;
6a885fd0 2348 return NULL;
6a885fd0 2349 }
33ab38d9 2350 return xasprintf("%s: port value out of range for %s", s, mf->name);
6a885fd0
BP
2351}
2352
72333065
JR
2353static char *
2354mf_from_ofp_port_string32(const struct mf_field *mf, const char *s,
2355 ovs_be32 *valuep, ovs_be32 *maskp)
2356{
4e022ec0 2357 ofp_port_t port;
72333065
JR
2358
2359 ovs_assert(mf->n_bytes == sizeof(ovs_be32));
2360 if (ofputil_port_from_string(s, &port)) {
2361 *valuep = ofputil_port_to_ofp11(port);
b8266395 2362 *maskp = OVS_BE32_MAX;
72333065
JR
2363 return NULL;
2364 }
2365 return xasprintf("%s: port value out of range for %s", s, mf->name);
2366}
2367
7257b535
BP
2368struct frag_handling {
2369 const char *name;
2370 uint8_t mask;
2371 uint8_t value;
2372};
2373
2374static const struct frag_handling all_frags[] = {
eadef313
JP
2375#define A FLOW_NW_FRAG_ANY
2376#define L FLOW_NW_FRAG_LATER
7257b535
BP
2377 /* name mask value */
2378
2379 { "no", A|L, 0 },
2380 { "first", A|L, A },
2381 { "later", A|L, A|L },
2382
2383 { "no", A, 0 },
2384 { "yes", A, A },
2385
2386 { "not_later", L, 0 },
2387 { "later", L, L },
2388#undef A
2389#undef L
2390};
2391
2392static char *
2393mf_from_frag_string(const char *s, uint8_t *valuep, uint8_t *maskp)
2394{
2395 const struct frag_handling *h;
2396
2397 for (h = all_frags; h < &all_frags[ARRAY_SIZE(all_frags)]; h++) {
2398 if (!strcasecmp(s, h->name)) {
2399 /* We force the upper bits of the mask on to make mf_parse_value()
2400 * happy (otherwise it will never think it's an exact match.) */
eadef313 2401 *maskp = h->mask | ~FLOW_NW_FRAG_MASK;
7257b535
BP
2402 *valuep = h->value;
2403 return NULL;
2404 }
2405 }
2406
2407 return xasprintf("%s: unknown fragment type (valid types are \"no\", "
2408 "\"yes\", \"first\", \"later\", \"not_first\"", s);
2409}
2410
8e4c1621
JG
2411static char *
2412parse_mf_flags(const char *s, const char *(*bit_to_string)(uint32_t),
2413 const char *field_name, ovs_be16 *flagsp, ovs_be16 allowed,
2414 ovs_be16 *maskp)
4fe3445a 2415{
8e4c1621
JG
2416 int err;
2417 char *err_str;
2418 uint32_t flags, mask;
4fe3445a 2419
8e4c1621
JG
2420 err = parse_flags(s, bit_to_string, '\0', field_name, &err_str,
2421 &flags, ntohs(allowed), maskp ? &mask : NULL);
2422 if (err < 0) {
2423 return err_str;
2424 }
4fe3445a 2425
8e4c1621
JG
2426 *flagsp = htons(flags);
2427 if (maskp) {
2428 *maskp = htons(mask);
4fe3445a
PS
2429 }
2430
8e4c1621 2431 return NULL;
4fe3445a
PS
2432}
2433
2434static char *
8e4c1621 2435mf_from_tcp_flags_string(const char *s, ovs_be16 *flagsp, ovs_be16 *maskp)
4fe3445a 2436{
8e4c1621
JG
2437 return parse_mf_flags(s, packet_tcp_flag_to_string, "TCP", flagsp,
2438 TCP_FLAGS_BE16(OVS_BE16_MAX), maskp);
4fe3445a
PS
2439}
2440
61bf6666 2441static char *
8e4c1621 2442mf_from_tun_flags_string(const char *s, ovs_be16 *flagsp, ovs_be16 *maskp)
61bf6666 2443{
8e4c1621 2444 return parse_mf_flags(s, flow_tun_flag_to_string, "tunnel", flagsp,
b666962b 2445 htons(FLOW_TNL_PUB_F_MASK), maskp);
61bf6666
JR
2446}
2447
07659514
JS
2448static char *
2449mf_from_ct_state_string(const char *s, ovs_be32 *flagsp, ovs_be32 *maskp)
2450{
2451 int err;
2452 char *err_str;
2453 uint32_t flags, mask;
2454
2455 err = parse_flags(s, ct_state_to_string, '\0', "ct_state", &err_str,
2456 &flags, CS_SUPPORTED_MASK, maskp ? &mask : NULL);
2457 if (err < 0) {
2458 return err_str;
2459 }
2460
2461 *flagsp = htonl(flags);
2462 if (maskp) {
2463 *maskp = htonl(mask);
2464 }
2465
2466 return NULL;
2467}
2468
6a885fd0
BP
2469/* Parses 's', a string value for field 'mf', into 'value' and 'mask'. Returns
2470 * NULL if successful, otherwise a malloc()'d string describing the error. */
2471char *
2472mf_parse(const struct mf_field *mf, const char *s,
2473 union mf_value *value, union mf_value *mask)
2474{
3634dff8
BP
2475 char *error;
2476
bedde04c 2477 if (!strcmp(s, "*")) {
6a885fd0
BP
2478 memset(value, 0, mf->n_bytes);
2479 memset(mask, 0, mf->n_bytes);
2480 return NULL;
2481 }
2482
2483 switch (mf->string) {
2484 case MFS_DECIMAL:
2485 case MFS_HEXADECIMAL:
3634dff8
BP
2486 error = mf_from_integer_string(mf, s,
2487 (uint8_t *) value, (uint8_t *) mask);
2488 break;
6a885fd0 2489
07659514
JS
2490 case MFS_CT_STATE:
2491 ovs_assert(mf->n_bytes == sizeof(ovs_be32));
2492 error = mf_from_ct_state_string(s, &value->be32, &mask->be32);
2493 break;
2494
6a885fd0 2495 case MFS_ETHERNET:
74ff3298 2496 error = mf_from_ethernet_string(mf, s, &value->mac, &mask->mac);
3634dff8 2497 break;
6a885fd0
BP
2498
2499 case MFS_IPV4:
3634dff8
BP
2500 error = mf_from_ipv4_string(mf, s, &value->be32, &mask->be32);
2501 break;
6a885fd0
BP
2502
2503 case MFS_IPV6:
3634dff8
BP
2504 error = mf_from_ipv6_string(mf, s, &value->ipv6, &mask->ipv6);
2505 break;
6a885fd0
BP
2506
2507 case MFS_OFP_PORT:
3634dff8
BP
2508 error = mf_from_ofp_port_string(mf, s, &value->be16, &mask->be16);
2509 break;
7257b535 2510
72333065 2511 case MFS_OFP_PORT_OXM:
3634dff8
BP
2512 error = mf_from_ofp_port_string32(mf, s, &value->be32, &mask->be32);
2513 break;
72333065 2514
7257b535 2515 case MFS_FRAG:
3634dff8
BP
2516 error = mf_from_frag_string(s, &value->u8, &mask->u8);
2517 break;
4fe3445a
PS
2518
2519 case MFS_TNL_FLAGS:
cb22974d 2520 ovs_assert(mf->n_bytes == sizeof(ovs_be16));
3634dff8
BP
2521 error = mf_from_tun_flags_string(s, &value->be16, &mask->be16);
2522 break;
2523
61bf6666
JR
2524 case MFS_TCP_FLAGS:
2525 ovs_assert(mf->n_bytes == sizeof(ovs_be16));
2526 error = mf_from_tcp_flags_string(s, &value->be16, &mask->be16);
2527 break;
2528
3634dff8 2529 default:
428b2edd 2530 OVS_NOT_REACHED();
6a885fd0 2531 }
3634dff8
BP
2532
2533 if (!error && !mf_is_mask_valid(mf, mask)) {
2534 error = xasprintf("%s: invalid mask for field %s", s, mf->name);
2535 }
2536 return error;
6a885fd0
BP
2537}
2538
2539/* Parses 's', a string value for field 'mf', into 'value'. Returns NULL if
2540 * successful, otherwise a malloc()'d string describing the error. */
2541char *
2542mf_parse_value(const struct mf_field *mf, const char *s, union mf_value *value)
2543{
2544 union mf_value mask;
2545 char *error;
2546
2547 error = mf_parse(mf, s, value, &mask);
2548 if (error) {
2549 return error;
2550 }
2551
2552 if (!is_all_ones((const uint8_t *) &mask, mf->n_bytes)) {
2553 return xasprintf("%s: wildcards not allowed here", s);
2554 }
2555 return NULL;
2556}
2557
2558static void
2559mf_format_integer_string(const struct mf_field *mf, const uint8_t *valuep,
2560 const uint8_t *maskp, struct ds *s)
2561{
6a885fd0 2562 if (mf->string == MFS_HEXADECIMAL) {
1734bf29 2563 ds_put_hex(s, valuep, mf->n_bytes);
6a885fd0 2564 } else {
1734bf29
JG
2565 unsigned long long int integer = 0;
2566 int i;
6a885fd0 2567
1734bf29 2568 ovs_assert(mf->n_bytes <= 8);
6a885fd0 2569 for (i = 0; i < mf->n_bytes; i++) {
1734bf29 2570 integer = (integer << 8) | valuep[i];
6a885fd0 2571 }
1734bf29
JG
2572 ds_put_format(s, "%lld", integer);
2573 }
6a885fd0 2574
1734bf29 2575 if (maskp) {
6a885fd0
BP
2576 /* I guess we could write the mask in decimal for MFS_DECIMAL but I'm
2577 * not sure that that a bit-mask written in decimal is ever easier to
2578 * understand than the same bit-mask written in hexadecimal. */
1734bf29
JG
2579 ds_put_char(s, '/');
2580 ds_put_hex(s, maskp, mf->n_bytes);
6a885fd0
BP
2581 }
2582}
2583
7257b535 2584static void
8f75dea3 2585mf_format_frag_string(uint8_t value, uint8_t mask, struct ds *s)
7257b535
BP
2586{
2587 const struct frag_handling *h;
7257b535 2588
eadef313 2589 mask &= FLOW_NW_FRAG_MASK;
8f75dea3 2590 value &= mask;
7257b535
BP
2591
2592 for (h = all_frags; h < &all_frags[ARRAY_SIZE(all_frags)]; h++) {
2593 if (value == h->value && mask == h->mask) {
2594 ds_put_cstr(s, h->name);
2595 return;
2596 }
2597 }
2598 ds_put_cstr(s, "<error>");
2599}
2600
4fe3445a 2601static void
b666962b 2602mf_format_tnl_flags_string(ovs_be16 value, ovs_be16 mask, struct ds *s)
4fe3445a 2603{
b666962b
JG
2604 format_flags_masked(s, NULL, flow_tun_flag_to_string, ntohs(value),
2605 ntohs(mask) & FLOW_TNL_PUB_F_MASK, FLOW_TNL_PUB_F_MASK);
4fe3445a
PS
2606}
2607
61bf6666
JR
2608static void
2609mf_format_tcp_flags_string(ovs_be16 value, ovs_be16 mask, struct ds *s)
2610{
2611 format_flags_masked(s, NULL, packet_tcp_flag_to_string, ntohs(value),
8e4c1621 2612 TCP_FLAGS(mask), TCP_FLAGS(OVS_BE16_MAX));
61bf6666
JR
2613}
2614
07659514
JS
2615static void
2616mf_format_ct_state_string(ovs_be32 value, ovs_be32 mask, struct ds *s)
2617{
2618 format_flags_masked(s, NULL, ct_state_to_string, ntohl(value),
2619 ntohl(mask), UINT16_MAX);
2620}
2621
6a885fd0
BP
2622/* Appends to 's' a string representation of field 'mf' whose value is in
2623 * 'value' and 'mask'. 'mask' may be NULL to indicate an exact match. */
2624void
2625mf_format(const struct mf_field *mf,
2626 const union mf_value *value, const union mf_value *mask,
2627 struct ds *s)
2628{
2629 if (mask) {
53cb9c3e 2630 if (is_all_zeros(mask, mf->n_bytes)) {
6a885fd0
BP
2631 ds_put_cstr(s, "ANY");
2632 return;
53cb9c3e 2633 } else if (is_all_ones(mask, mf->n_bytes)) {
6a885fd0
BP
2634 mask = NULL;
2635 }
2636 }
2637
2638 switch (mf->string) {
72333065
JR
2639 case MFS_OFP_PORT_OXM:
2640 if (!mask) {
4e022ec0 2641 ofp_port_t port;
bc146369 2642 ofputil_port_from_ofp11(value->be32, &port);
72333065
JR
2643 ofputil_format_port(port, s);
2644 break;
2645 }
2646 /* fall through */
6a885fd0
BP
2647 case MFS_OFP_PORT:
2648 if (!mask) {
4e022ec0 2649 ofputil_format_port(u16_to_ofp(ntohs(value->be16)), s);
6a885fd0
BP
2650 break;
2651 }
2652 /* fall through */
2653 case MFS_DECIMAL:
2654 case MFS_HEXADECIMAL:
2655 mf_format_integer_string(mf, (uint8_t *) value, (uint8_t *) mask, s);
2656 break;
2657
07659514
JS
2658 case MFS_CT_STATE:
2659 mf_format_ct_state_string(value->be32,
2660 mask ? mask->be32 : OVS_BE32_MAX, s);
2661 break;
2662
6a885fd0 2663 case MFS_ETHERNET:
74ff3298 2664 eth_format_masked(value->mac, mask ? &mask->mac : NULL, s);
6a885fd0
BP
2665 break;
2666
2667 case MFS_IPV4:
b8266395 2668 ip_format_masked(value->be32, mask ? mask->be32 : OVS_BE32_MAX, s);
6a885fd0
BP
2669 break;
2670
2671 case MFS_IPV6:
ac6d120f 2672 ipv6_format_masked(&value->ipv6, mask ? &mask->ipv6 : NULL, s);
6a885fd0
BP
2673 break;
2674
7257b535 2675 case MFS_FRAG:
8f75dea3 2676 mf_format_frag_string(value->u8, mask ? mask->u8 : UINT8_MAX, s);
7257b535
BP
2677 break;
2678
4fe3445a 2679 case MFS_TNL_FLAGS:
b666962b
JG
2680 mf_format_tnl_flags_string(value->be16,
2681 mask ? mask->be16 : OVS_BE16_MAX, s);
4fe3445a
PS
2682 break;
2683
61bf6666
JR
2684 case MFS_TCP_FLAGS:
2685 mf_format_tcp_flags_string(value->be16,
2686 mask ? mask->be16 : OVS_BE16_MAX, s);
2687 break;
2688
6a885fd0 2689 default:
428b2edd 2690 OVS_NOT_REACHED();
6a885fd0
BP
2691 }
2692}
816fd533 2693\f
9bab681f
IY
2694/* Makes subfield 'sf' within 'flow' exactly match the 'sf->n_bits'
2695 * least-significant bits in 'x'.
2696 */
2697void
2698mf_write_subfield_flow(const struct mf_subfield *sf,
2699 const union mf_subvalue *x, struct flow *flow)
2700{
2701 const struct mf_field *field = sf->field;
2702 union mf_value value;
2703
2704 mf_get_value(field, flow, &value);
158edc8d 2705 bitwise_copy(x, sizeof *x, 0, &value, field->n_bytes,
9bab681f
IY
2706 sf->ofs, sf->n_bits);
2707 mf_set_flow_value(field, &value, flow);
2708}
2709
81a76618 2710/* Makes subfield 'sf' within 'match' exactly match the 'sf->n_bits'
1b35df45 2711 * least-significant bits in 'x'.
615660a9 2712 */
1b35df45
BP
2713void
2714mf_write_subfield(const struct mf_subfield *sf, const union mf_subvalue *x,
81a76618 2715 struct match *match)
1b35df45
BP
2716{
2717 const struct mf_field *field = sf->field;
2718 union mf_value value, mask;
2719
81a76618 2720 mf_get(field, match, &value, &mask);
1b35df45
BP
2721 bitwise_copy(x, sizeof *x, 0, &value, field->n_bytes, sf->ofs, sf->n_bits);
2722 bitwise_one ( &mask, field->n_bytes, sf->ofs, sf->n_bits);
4f7b100c 2723 mf_set(field, &value, &mask, match, NULL);
1b35df45
BP
2724}
2725
dfe191d5
JR
2726void
2727mf_write_subfield_value(const struct mf_subfield *sf, const void *src,
2728 struct match *match)
2729{
2730 const struct mf_field *field = sf->field;
2731 union mf_value value, mask;
2732 unsigned int size = DIV_ROUND_UP(sf->n_bits, 8);
2733
2734 mf_get(field, match, &value, &mask);
2735 bitwise_copy(src, size, 0, &value, field->n_bytes, sf->ofs, sf->n_bits);
2736 bitwise_one ( &mask, field->n_bytes, sf->ofs, sf->n_bits);
2737 mf_set(field, &value, &mask, match, NULL);
2738}
2739
35c1f798
BP
2740/* 'v' and 'm' correspond to values of 'field'. This function copies them into
2741 * 'match' in the correspond positions. */
2742void
2743mf_mask_subfield(const struct mf_field *field,
2744 const union mf_subvalue *v,
2745 const union mf_subvalue *m,
2746 struct match *match)
2747{
2748 union mf_value value, mask;
2749
2750 mf_get(field, match, &value, &mask);
2751 bitwise_copy(v, sizeof *v, 0, &value, field->n_bytes, 0, field->n_bits);
2752 bitwise_copy(m, sizeof *m, 0, &mask, field->n_bytes, 0, field->n_bits);
4f7b100c 2753 mf_set(field, &value, &mask, match, NULL);
35c1f798
BP
2754}
2755
1b35df45
BP
2756/* Initializes 'x' to the value of 'sf' within 'flow'. 'sf' must be valid for
2757 * reading 'flow', e.g. as checked by mf_check_src(). */
2758void
2759mf_read_subfield(const struct mf_subfield *sf, const struct flow *flow,
2760 union mf_subvalue *x)
2761{
2762 union mf_value value;
2763
2764 mf_get_value(sf->field, flow, &value);
2765
2766 memset(x, 0, sizeof *x);
2767 bitwise_copy(&value, sf->field->n_bytes, sf->ofs,
2768 x, sizeof *x, 0,
2769 sf->n_bits);
2770}
2771
816fd533
BP
2772/* Returns the value of 'sf' within 'flow'. 'sf' must be valid for reading
2773 * 'flow', e.g. as checked by mf_check_src() and sf->n_bits must be 64 or
2774 * less. */
2775uint64_t
2776mf_get_subfield(const struct mf_subfield *sf, const struct flow *flow)
2777{
2778 union mf_value value;
2779
2780 mf_get_value(sf->field, flow, &value);
2781 return bitwise_get(&value, sf->field->n_bytes, sf->ofs, sf->n_bits);
2782}
2783
9bab681f
IY
2784void
2785mf_format_subvalue(const union mf_subvalue *subvalue, struct ds *s)
2786{
e7ae59f9 2787 ds_put_hex(s, subvalue->u8, sizeof subvalue->u8);
9bab681f 2788}
bc65c25a
SH
2789
2790void
2791field_array_set(enum mf_field_id id, const union mf_value *value,
2792 struct field_array *fa)
2793{
e8dba719
JR
2794 size_t i, offset = 0;
2795
bc65c25a 2796 ovs_assert(id < MFF_N_IDS);
e8dba719
JR
2797
2798 /* Find the spot for 'id'. */
2799 BITMAP_FOR_EACH_1 (i, id, fa->used.bm) {
2800 offset += mf_from_id(i)->n_bytes;
2801 }
2802
2803 size_t value_size = mf_from_id(id)->n_bytes;
2804
2805 /* make room if necessary. */
2806 if (!bitmap_is_set(fa->used.bm, id)) {
2807 fa->values = xrealloc(fa->values, fa->values_size + value_size);
2808 /* Move remainder forward, if any. */
2809 if (offset < fa->values_size) {
2810 memmove(fa->values + offset + value_size, fa->values + offset,
2811 fa->values_size - offset);
2812 }
2813 fa->values_size += value_size;
2814 }
bc65c25a 2815 bitmap_set1(fa->used.bm, id);
e8dba719
JR
2816
2817 memcpy(fa->values + offset, value, value_size);
bc65c25a 2818}
04f48a68 2819
aafee638
YHW
2820/* A wrapper for variable length mf_fields that is maintained by
2821 * struct vl_mff_map.*/
2822struct vl_mf_field {
2823 struct mf_field mf;
2824 struct cmap_node cmap_node; /* In ofproto->vl_mff_map->cmap. */
2825};
2826
04f48a68
YHW
2827static inline uint32_t
2828mf_field_hash(uint32_t key)
2829{
2830 return hash_int(key, 0);
2831}
2832
2833void
2834mf_vl_mff_map_clear(struct vl_mff_map *vl_mff_map)
2835 OVS_REQUIRES(vl_mff_map->mutex)
2836{
aafee638 2837 struct vl_mf_field *vmf;
04f48a68 2838
aafee638
YHW
2839 CMAP_FOR_EACH (vmf, cmap_node, &vl_mff_map->cmap) {
2840 cmap_remove(&vl_mff_map->cmap, &vmf->cmap_node,
2841 mf_field_hash(vmf->mf.id));
2842 ovsrcu_postpone(free, vmf);
04f48a68
YHW
2843 }
2844}
2845
aafee638 2846static struct vl_mf_field *
04f48a68
YHW
2847mf_get_vl_mff__(uint32_t id, const struct vl_mff_map *vl_mff_map)
2848{
aafee638 2849 struct vl_mf_field *vmf;
04f48a68 2850
aafee638 2851 CMAP_FOR_EACH_WITH_HASH (vmf, cmap_node, mf_field_hash(id),
04f48a68 2852 &vl_mff_map->cmap) {
aafee638
YHW
2853 if (vmf->mf.id == id) {
2854 return vmf;
04f48a68
YHW
2855 }
2856 }
2857
2858 return NULL;
2859}
2860
2861/* If 'mff' is a variable length field, looks up 'vl_mff_map', returns a
2862 * pointer to the variable length meta-flow field corresponding to 'mff'.
2863 * Returns NULL if no mapping is existed for 'mff'. */
2864const struct mf_field *
2865mf_get_vl_mff(const struct mf_field *mff,
2866 const struct vl_mff_map *vl_mff_map)
2867{
2868 if (mff && mff->variable_len && vl_mff_map) {
aafee638 2869 return &mf_get_vl_mff__(mff->id, vl_mff_map)->mf;
04f48a68
YHW
2870 }
2871
2872 return NULL;
2873}
2874
2875/* Updates the tun_metadata mf_field in 'vl_mff_map' according to 'ttm'.
2876 * This function is supposed to be invoked after tun_metadata_table_mod(). */
2877enum ofperr
2878mf_vl_mff_map_mod_from_tun_metadata(struct vl_mff_map *vl_mff_map,
2879 const struct ofputil_tlv_table_mod *ttm)
2880 OVS_REQUIRES(vl_mff_map->mutex)
2881{
2882 struct ofputil_tlv_map *tlv_map;
2883
2884 if (ttm->command == NXTTMC_CLEAR) {
2885 mf_vl_mff_map_clear(vl_mff_map);
2886 return 0;
2887 }
2888
2889 LIST_FOR_EACH (tlv_map, list_node, &ttm->mappings) {
2890 unsigned int idx = MFF_TUN_METADATA0 + tlv_map->index;
aafee638 2891 struct vl_mf_field *vmf;
04f48a68
YHW
2892
2893 if (idx >= MFF_TUN_METADATA0 + TUN_METADATA_NUM_OPTS) {
2894 return OFPERR_NXTTMFC_BAD_FIELD_IDX;
2895 }
2896
2897 switch (ttm->command) {
2898 case NXTTMC_ADD:
aafee638
YHW
2899 vmf = xmalloc(sizeof *vmf);
2900 vmf->mf = mf_fields[idx];
2901 vmf->mf.n_bytes = tlv_map->option_len;
2902 vmf->mf.n_bits = tlv_map->option_len * 8;
2903 vmf->mf.mapped = true;
2904
2905 cmap_insert(&vl_mff_map->cmap, &vmf->cmap_node,
2906 mf_field_hash(idx));
04f48a68
YHW
2907 break;
2908
2909 case NXTTMC_DELETE:
aafee638
YHW
2910 vmf = mf_get_vl_mff__(idx, vl_mff_map);
2911 if (vmf) {
2912 cmap_remove(&vl_mff_map->cmap, &vmf->cmap_node,
04f48a68 2913 mf_field_hash(idx));
aafee638 2914 ovsrcu_postpone(free, vmf);
04f48a68
YHW
2915 }
2916 break;
2917
2918 case NXTTMC_CLEAR:
2919 default:
2920 OVS_NOT_REACHED();
2921 }
2922 }
2923
2924 return 0;
2925}
2926
2927/* Returns true if a variable length meta-flow field 'mff' is not mapped in
2928 * the 'vl_mff_map'. */
2929bool
2930mf_vl_mff_invalid(const struct mf_field *mff, const struct vl_mff_map *map)
2931{
2932 return map && mff && mff->variable_len && !mff->mapped;
2933}