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