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