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