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