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