]> git.proxmox.com Git - mirror_ovs.git/blame - lib/meta-flow.c
ovsdb-idl: Fix iteration over tracked rows with no actual data.
[mirror_ovs.git] / lib / meta-flow.c
CommitLineData
6a885fd0 1/*
04f48a68 2 * Copyright (c) 2011-2017 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"
5c7c16d8 29#include "ovs-atomic.h"
04f48a68 30#include "ovs-rcu.h"
4663f9e0 31#include "ovs-thread.h"
6a885fd0
BP
32#include "packets.h"
33#include "random.h"
ee89ea7b 34#include "openvswitch/shash.h"
6a885fd0 35#include "socket-util.h"
9558d2a5 36#include "tun-metadata.h"
6a885fd0 37#include "unaligned.h"
86f35fb5 38#include "util.h"
e03c096d 39#include "openvswitch/ofp-errors.h"
0d71302e
BP
40#include "openvswitch/ofp-match.h"
41#include "openvswitch/ofp-port.h"
e6211adc 42#include "openvswitch/vlog.h"
aafee638 43#include "vl-mff-map.h"
17553f27 44#include "openvswitch/nsh.h"
816fd533
BP
45
46VLOG_DEFINE_THIS_MODULE(meta_flow);
6a885fd0 47
13751fd8
JR
48#define FLOW_U32OFS(FIELD) \
49 offsetof(struct flow, FIELD) % 4 ? -1 : offsetof(struct flow, FIELD) / 4
50
6a885fd0
BP
51#define MF_FIELD_SIZES(MEMBER) \
52 sizeof ((union mf_value *)0)->MEMBER, \
53 8 * sizeof ((union mf_value *)0)->MEMBER
54
7f98c44d
JR
55extern const struct mf_field mf_fields[MFF_N_IDS]; /* Silence a warning. */
56
57const struct mf_field mf_fields[MFF_N_IDS] = {
a4ce8b25 58#include "meta-flow.inc"
6a885fd0
BP
59};
60
4663f9e0
BP
61/* Maps from an mf_field's 'name' or 'extra_name' to the mf_field. */
62static struct shash mf_by_name;
28da1f8f 63
816fd533
BP
64/* Rate limit for parse errors. These always indicate a bug in an OpenFlow
65 * controller and so there's not much point in showing a lot of them. */
66static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
67
e3e96509
JR
68#define MF_VALUE_EXACT_8 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
69#define MF_VALUE_EXACT_16 MF_VALUE_EXACT_8, MF_VALUE_EXACT_8
70#define MF_VALUE_EXACT_32 MF_VALUE_EXACT_16, MF_VALUE_EXACT_16
71#define MF_VALUE_EXACT_64 MF_VALUE_EXACT_32, MF_VALUE_EXACT_32
72#define MF_VALUE_EXACT_128 MF_VALUE_EXACT_64, MF_VALUE_EXACT_64
73#define MF_VALUE_EXACT_INITIALIZER { .tun_metadata = { MF_VALUE_EXACT_128 } }
74
75const union mf_value exact_match_mask = MF_VALUE_EXACT_INITIALIZER;
76
4663f9e0 77static void nxm_init(void);
b5e5143b 78
6a885fd0
BP
79/* Returns the field with the given 'name', or a null pointer if no field has
80 * that name. */
81const struct mf_field *
82mf_from_name(const char *name)
83{
4663f9e0 84 nxm_init();
6a885fd0
BP
85 return shash_find_data(&mf_by_name, name);
86}
87
21b2fa61
JR
88/* Returns the field with the given 'name' (which is 'len' bytes long), or a
89 * null pointer if no field has that name. */
90const struct mf_field *
91mf_from_name_len(const char *name, size_t len)
92{
93 nxm_init();
94
95 struct shash_node *node = shash_find_len(&mf_by_name, name, len);
96 return node ? node->data : NULL;
97}
98
28da1f8f 99static void
4663f9e0 100nxm_do_init(void)
28da1f8f 101{
216a32dd 102 int i;
28da1f8f 103
4663f9e0 104 shash_init(&mf_by_name);
216a32dd
JR
105 for (i = 0; i < MFF_N_IDS; i++) {
106 const struct mf_field *mf = &mf_fields[i];
107
108 ovs_assert(mf->id == i); /* Fields must be in the enum order. */
109
4663f9e0
BP
110 shash_add_once(&mf_by_name, mf->name, mf);
111 if (mf->extra_name) {
112 shash_add_once(&mf_by_name, mf->extra_name, mf);
113 }
28da1f8f 114 }
28da1f8f
BP
115}
116
4663f9e0
BP
117static void
118nxm_init(void)
119{
120 static pthread_once_t once = PTHREAD_ONCE_INIT;
121 pthread_once(&once, nxm_do_init);
122}
123
35c1f798
BP
124/* Consider the two value/mask pairs 'a_value/a_mask' and 'b_value/b_mask' as
125 * restrictions on a field's value. Then, this function initializes
126 * 'dst_value/dst_mask' such that it combines the restrictions of both pairs.
127 * This is not always possible, i.e. if one pair insists on a value of 0 in
128 * some bit and the other pair insists on a value of 1 in that bit. This
129 * function returns false in a case where the combined restriction is
130 * impossible (in which case 'dst_value/dst_mask' is not fully initialized),
131 * true otherwise.
132 *
133 * (As usually true for value/mask pairs in OVS, any 1-bit in a value must have
134 * a corresponding 1-bit in its mask.) */
135bool
136mf_subvalue_intersect(const union mf_subvalue *a_value,
137 const union mf_subvalue *a_mask,
138 const union mf_subvalue *b_value,
139 const union mf_subvalue *b_mask,
140 union mf_subvalue *dst_value,
141 union mf_subvalue *dst_mask)
142{
143 for (int i = 0; i < ARRAY_SIZE(a_value->be64); i++) {
144 ovs_be64 av = a_value->be64[i];
145 ovs_be64 am = a_mask->be64[i];
146 ovs_be64 bv = b_value->be64[i];
147 ovs_be64 bm = b_mask->be64[i];
148 ovs_be64 *dv = &dst_value->be64[i];
149 ovs_be64 *dm = &dst_mask->be64[i];
150
151 if ((av ^ bv) & (am & bm)) {
152 return false;
153 }
154 *dv = av | bv;
155 *dm = am | bm;
156 }
157 return true;
158}
159
160/* Returns the "number of bits" in 'v', e.g. 1 if only the lowest-order bit is
161 * set, 2 if the second-lowest-order bit is set, and so on. */
162int
163mf_subvalue_width(const union mf_subvalue *v)
164{
165 return 1 + bitwise_rscan(v, sizeof *v, true, sizeof *v * 8 - 1, -1);
166}
167
168/* For positive 'n', shifts the bits in 'value' 'n' bits to the left, and for
169 * negative 'n', shifts the bits '-n' bits to the right. */
170void
171mf_subvalue_shift(union mf_subvalue *value, int n)
172{
173 if (n) {
174 union mf_subvalue tmp;
175 memset(&tmp, 0, sizeof tmp);
176
177 if (n > 0 && n < 8 * sizeof tmp) {
178 bitwise_copy(value, sizeof *value, 0,
179 &tmp, sizeof tmp, n,
180 8 * sizeof tmp - n);
181 } else if (n < 0 && n > -8 * sizeof tmp) {
182 bitwise_copy(value, sizeof *value, -n,
183 &tmp, sizeof tmp, 0,
184 8 * sizeof tmp + n);
185 }
186 *value = tmp;
187 }
188}
189
77ab5fd2
BP
190/* Appends a formatted representation of 'sv' to 's'. */
191void
192mf_subvalue_format(const union mf_subvalue *sv, struct ds *s)
193{
194 ds_put_hex(s, sv, sizeof *sv);
195}
196
6a885fd0
BP
197/* Returns true if 'wc' wildcards all the bits in field 'mf', false if 'wc'
198 * specifies at least one bit in the field.
199 *
200 * The caller is responsible for ensuring that 'wc' corresponds to a flow that
201 * meets 'mf''s prerequisites. */
202bool
203mf_is_all_wild(const struct mf_field *mf, const struct flow_wildcards *wc)
204{
205 switch (mf->id) {
a79f29f2
AZ
206 case MFF_DP_HASH:
207 return !wc->masks.dp_hash;
208 case MFF_RECIRC_ID:
209 return !wc->masks.recirc_id;
3d4b2e6e
JS
210 case MFF_PACKET_TYPE:
211 return !wc->masks.packet_type;
18080541
BP
212 case MFF_CONJ_ID:
213 return !wc->masks.conj_id;
4fe3445a 214 case MFF_TUN_SRC:
0ad90c84 215 return !wc->masks.tunnel.ip_src;
4fe3445a 216 case MFF_TUN_DST:
0ad90c84 217 return !wc->masks.tunnel.ip_dst;
7dad8e9a
TLSC
218 case MFF_TUN_IPV6_SRC:
219 return ipv6_mask_is_any(&wc->masks.tunnel.ipv6_src);
220 case MFF_TUN_IPV6_DST:
221 return ipv6_mask_is_any(&wc->masks.tunnel.ipv6_dst);
0ad90c84 222 case MFF_TUN_ID:
b666962b 223 return !wc->masks.tunnel.tun_id;
4fe3445a 224 case MFF_TUN_TOS:
b666962b 225 return !wc->masks.tunnel.ip_tos;
4fe3445a 226 case MFF_TUN_TTL:
b666962b 227 return !wc->masks.tunnel.ip_ttl;
4fe3445a 228 case MFF_TUN_FLAGS:
b666962b 229 return !(wc->masks.tunnel.flags & FLOW_TNL_PUB_F_MASK);
ac6073e3
MC
230 case MFF_TUN_GBP_ID:
231 return !wc->masks.tunnel.gbp_id;
232 case MFF_TUN_GBP_FLAGS:
233 return !wc->masks.tunnel.gbp_flags;
7dc18ae9
WT
234 case MFF_TUN_ERSPAN_VER:
235 return !wc->masks.tunnel.erspan_ver;
236 case MFF_TUN_ERSPAN_IDX:
237 return !wc->masks.tunnel.erspan_idx;
238 case MFF_TUN_ERSPAN_DIR:
239 return !wc->masks.tunnel.erspan_dir;
240 case MFF_TUN_ERSPAN_HWID:
241 return !wc->masks.tunnel.erspan_hwid;
1cb20095
JG
242 CASE_MFF_TUN_METADATA:
243 return !ULLONG_GET(wc->masks.tunnel.metadata.present.map,
244 mf->id - MFF_TUN_METADATA0);
969fc56c 245 case MFF_METADATA:
26720e24 246 return !wc->masks.metadata;
0bdc4bec 247 case MFF_IN_PORT:
72333065 248 case MFF_IN_PORT_OXM:
4e022ec0 249 return !wc->masks.in_port.ofp_port;
1b567fb9
AA
250 case MFF_SKB_PRIORITY:
251 return !wc->masks.skb_priority;
1362e248
JG
252 case MFF_PKT_MARK:
253 return !wc->masks.pkt_mark;
07659514
JS
254 case MFF_CT_STATE:
255 return !wc->masks.ct_state;
256 case MFF_CT_ZONE:
257 return !wc->masks.ct_zone;
8e53fe8c
JS
258 case MFF_CT_MARK:
259 return !wc->masks.ct_mark;
9daf2348 260 case MFF_CT_LABEL:
2ff8484b 261 return ovs_u128_is_zero(wc->masks.ct_label);
daf4d3c1
JR
262 case MFF_CT_NW_PROTO:
263 return !wc->masks.ct_nw_proto;
264 case MFF_CT_NW_SRC:
265 return !wc->masks.ct_nw_src;
266 case MFF_CT_NW_DST:
267 return !wc->masks.ct_nw_dst;
268 case MFF_CT_TP_SRC:
269 return !wc->masks.ct_tp_src;
270 case MFF_CT_TP_DST:
271 return !wc->masks.ct_tp_dst;
272 case MFF_CT_IPV6_SRC:
273 return ipv6_mask_is_any(&wc->masks.ct_ipv6_src);
274 case MFF_CT_IPV6_DST:
275 return ipv6_mask_is_any(&wc->masks.ct_ipv6_dst);
0d7e2fe4 276 CASE_MFF_REGS:
26720e24 277 return !wc->masks.regs[mf->id - MFF_REG0];
79fe0f46
BP
278 CASE_MFF_XREGS:
279 return !flow_get_xreg(&wc->masks, mf->id - MFF_XREG0);
b23ada8e
JP
280 CASE_MFF_XXREGS: {
281 ovs_u128 value = flow_get_xxreg(&wc->masks, mf->id - MFF_XXREG0);
282 return ovs_u128_is_zero(value);
283 }
c61f3870
BP
284 case MFF_ACTSET_OUTPUT:
285 return !wc->masks.actset_output;
6a885fd0 286
73c0ce34 287 case MFF_ETH_SRC:
26720e24 288 return eth_addr_is_zero(wc->masks.dl_src);
6a885fd0 289 case MFF_ETH_DST:
26720e24 290 return eth_addr_is_zero(wc->masks.dl_dst);
e2170cff 291 case MFF_ETH_TYPE:
26720e24 292 return !wc->masks.dl_type;
6a885fd0 293
e878338b
SH
294 case MFF_ARP_SHA:
295 case MFF_ND_SLL:
26720e24 296 return eth_addr_is_zero(wc->masks.arp_sha);
e878338b
SH
297
298 case MFF_ARP_THA:
299 case MFF_ND_TLL:
26720e24 300 return eth_addr_is_zero(wc->masks.arp_tha);
e878338b 301
6a885fd0 302 case MFF_VLAN_TCI:
f0fb825a 303 return !wc->masks.vlans[0].tci;
441c57a9 304 case MFF_DL_VLAN:
f0fb825a 305 return !(wc->masks.vlans[0].tci & htons(VLAN_VID_MASK));
cc34bc8c 306 case MFF_VLAN_VID:
f0fb825a 307 return !(wc->masks.vlans[0].tci & htons(VLAN_VID_MASK | VLAN_CFI));
441c57a9 308 case MFF_DL_VLAN_PCP:
cc34bc8c 309 case MFF_VLAN_PCP:
f0fb825a 310 return !(wc->masks.vlans[0].tci & htons(VLAN_PCP_MASK));
6a885fd0 311
b02475c5 312 case MFF_MPLS_LABEL:
8bfd0fda 313 return !(wc->masks.mpls_lse[0] & htonl(MPLS_LABEL_MASK));
b02475c5 314 case MFF_MPLS_TC:
60df616f 315 return !(wc->masks.mpls_lse[0] & htonl(MPLS_TC_MASK));
b02475c5 316 case MFF_MPLS_BOS:
60df616f 317 return !(wc->masks.mpls_lse[0] & htonl(MPLS_BOS_MASK));
bef3f465
BP
318 case MFF_MPLS_TTL:
319 return !(wc->masks.mpls_lse[0] & htonl(MPLS_TTL_MASK));
b02475c5 320
6a885fd0 321 case MFF_IPV4_SRC:
26720e24 322 return !wc->masks.nw_src;
6a885fd0 323 case MFF_IPV4_DST:
26720e24 324 return !wc->masks.nw_dst;
6a885fd0
BP
325
326 case MFF_IPV6_SRC:
26720e24 327 return ipv6_mask_is_any(&wc->masks.ipv6_src);
6a885fd0 328 case MFF_IPV6_DST:
26720e24 329 return ipv6_mask_is_any(&wc->masks.ipv6_dst);
6a885fd0 330
32455024 331 case MFF_IPV6_LABEL:
26720e24 332 return !wc->masks.ipv6_label;
32455024 333
851d3105 334 case MFF_IP_PROTO:
26720e24 335 return !wc->masks.nw_proto;
5d9499c4 336 case MFF_IP_DSCP:
1638b906 337 case MFF_IP_DSCP_SHIFTED:
26720e24 338 return !(wc->masks.nw_tos & IP_DSCP_MASK);
5d9499c4 339 case MFF_IP_ECN:
26720e24 340 return !(wc->masks.nw_tos & IP_ECN_MASK);
3840c406 341 case MFF_IP_TTL:
26720e24 342 return !wc->masks.nw_ttl;
5d9499c4 343
47284b1f 344 case MFF_ND_TARGET:
26720e24 345 return ipv6_mask_is_any(&wc->masks.nd_target);
47284b1f 346
9b2b8497
VDA
347 case MFF_ND_RESERVED:
348 return !wc->masks.igmp_group_ip4;
349 case MFF_ND_OPTIONS_TYPE:
350 return !wc->masks.tcp_flags;
351
7257b535 352 case MFF_IP_FRAG:
26720e24 353 return !(wc->masks.nw_frag & FLOW_NW_FRAG_MASK);
7257b535 354
851d3105 355 case MFF_ARP_OP:
26720e24 356 return !wc->masks.nw_proto;
6a885fd0 357 case MFF_ARP_SPA:
26720e24 358 return !wc->masks.nw_src;
6a885fd0 359 case MFF_ARP_TPA:
26720e24 360 return !wc->masks.nw_dst;
6a885fd0 361
73f33563
BP
362 case MFF_TCP_SRC:
363 case MFF_UDP_SRC:
0d56eaf2 364 case MFF_SCTP_SRC:
73f33563
BP
365 case MFF_ICMPV4_TYPE:
366 case MFF_ICMPV6_TYPE:
26720e24 367 return !wc->masks.tp_src;
73f33563
BP
368 case MFF_TCP_DST:
369 case MFF_UDP_DST:
0d56eaf2 370 case MFF_SCTP_DST:
73f33563
BP
371 case MFF_ICMPV4_CODE:
372 case MFF_ICMPV6_CODE:
26720e24 373 return !wc->masks.tp_dst;
dc235f7f
JR
374 case MFF_TCP_FLAGS:
375 return !wc->masks.tcp_flags;
73f33563 376
3d2fbd70
JS
377 case MFF_NSH_FLAGS:
378 return !wc->masks.nsh.flags;
17553f27
YY
379 case MFF_NSH_TTL:
380 return !wc->masks.nsh.ttl;
3d2fbd70
JS
381 case MFF_NSH_MDTYPE:
382 return !wc->masks.nsh.mdtype;
383 case MFF_NSH_NP:
384 return !wc->masks.nsh.np;
385 case MFF_NSH_SPI:
17553f27 386 return !(wc->masks.nsh.path_hdr & htonl(NSH_SPI_MASK));
3d2fbd70 387 case MFF_NSH_SI:
17553f27 388 return !(wc->masks.nsh.path_hdr & htonl(NSH_SI_MASK));
3d2fbd70
JS
389 case MFF_NSH_C1:
390 case MFF_NSH_C2:
391 case MFF_NSH_C3:
392 case MFF_NSH_C4:
f59cb331 393 return !wc->masks.nsh.context[mf->id - MFF_NSH_C1];
3c6d05a0
WT
394 case MFF_TUN_GTPU_FLAGS:
395 return !wc->masks.tunnel.gtpu_flags;
396 case MFF_TUN_GTPU_MSGTYPE:
397 return !wc->masks.tunnel.gtpu_msgtype;
3d2fbd70 398
6a885fd0
BP
399 case MFF_N_IDS:
400 default:
428b2edd 401 OVS_NOT_REACHED();
6a885fd0
BP
402 }
403}
404
405/* Initializes 'mask' with the wildcard bit pattern for field 'mf' within 'wc'.
406 * Each bit in 'mask' will be set to 1 if the bit is significant for matching
407 * purposes, or to 0 if it is wildcarded.
408 *
409 * The caller is responsible for ensuring that 'wc' corresponds to a flow that
410 * meets 'mf''s prerequisites. */
411void
412mf_get_mask(const struct mf_field *mf, const struct flow_wildcards *wc,
413 union mf_value *mask)
414{
fb15feb0 415 mf_get_value(mf, &wc->masks, mask);
6a885fd0
BP
416}
417
418/* Tests whether 'mask' is a valid wildcard bit pattern for 'mf'. Returns true
419 * if the mask is valid, false otherwise. */
420bool
421mf_is_mask_valid(const struct mf_field *mf, const union mf_value *mask)
422{
423 switch (mf->maskable) {
424 case MFM_NONE:
53cb9c3e
JR
425 return (is_all_zeros(mask, mf->n_bytes) ||
426 is_all_ones(mask, mf->n_bytes));
6a885fd0
BP
427
428 case MFM_FULLY:
429 return true;
6a885fd0
BP
430 }
431
428b2edd 432 OVS_NOT_REACHED();
6a885fd0
BP
433}
434
aff49b8c 435/* Returns true if 'flow' meets the prerequisites for 'mf', false otherwise.
67210a55 436 * If a non-NULL 'mask' is passed, zero-valued matches can also be verified.
aff49b8c 437 * Sets inspected bits in 'wc', if non-NULL. */
67210a55
JR
438static bool
439mf_are_prereqs_ok__(const struct mf_field *mf, const struct flow *flow,
daf4d3c1 440 const struct flow_wildcards *mask,
67210a55 441 struct flow_wildcards *wc)
6a885fd0 442{
3d4b2e6e
JS
443 ovs_be16 dl_type = get_dl_type(flow);
444
6a885fd0
BP
445 switch (mf->prereqs) {
446 case MFP_NONE:
447 return true;
beb75a40
JS
448 case MFP_ETHERNET:
449 return is_ethernet(flow, wc);
6a885fd0 450 case MFP_ARP:
3d4b2e6e
JS
451 return (dl_type == htons(ETH_TYPE_ARP) ||
452 dl_type == htons(ETH_TYPE_RARP));
6a885fd0 453 case MFP_IPV4:
3d4b2e6e 454 return dl_type == htons(ETH_TYPE_IP);
6a885fd0 455 case MFP_IPV6:
3d4b2e6e 456 return dl_type == htons(ETH_TYPE_IPV6);
8069b0da 457 case MFP_VLAN_VID:
aff49b8c 458 return is_vlan(flow, wc);
b02475c5 459 case MFP_MPLS:
3d4b2e6e 460 return eth_type_mpls(dl_type);
6a885fd0
BP
461 case MFP_IP_ANY:
462 return is_ip_any(flow);
3d2fbd70
JS
463 case MFP_NSH:
464 return dl_type == htons(ETH_TYPE_NSH);
daf4d3c1
JR
465 case MFP_CT_VALID:
466 return is_ct_valid(flow, mask, wc);
6a885fd0 467 case MFP_TCP:
67210a55 468 /* Matching !FRAG_LATER is not enforced (mask is not checked). */
aff49b8c 469 return is_tcp(flow, wc) && !(flow->nw_frag & FLOW_NW_FRAG_LATER);
6a885fd0 470 case MFP_UDP:
aff49b8c 471 return is_udp(flow, wc) && !(flow->nw_frag & FLOW_NW_FRAG_LATER);
0d56eaf2 472 case MFP_SCTP:
aff49b8c 473 return is_sctp(flow, wc) && !(flow->nw_frag & FLOW_NW_FRAG_LATER);
268a95e0 474 case MFP_ICMPV4:
aff49b8c 475 return is_icmpv4(flow, wc);
6a885fd0 476 case MFP_ICMPV6:
aff49b8c 477 return is_icmpv6(flow, wc);
6a885fd0 478 case MFP_ND:
aff49b8c 479 return is_nd(flow, wc);
6a885fd0 480 case MFP_ND_SOLICIT:
aff49b8c 481 return is_nd(flow, wc) && flow->tp_src == htons(ND_NEIGHBOR_SOLICIT);
6a885fd0 482 case MFP_ND_ADVERT:
aff49b8c 483 return is_nd(flow, wc) && flow->tp_src == htons(ND_NEIGHBOR_ADVERT);
6a885fd0
BP
484 }
485
428b2edd 486 OVS_NOT_REACHED();
6a885fd0
BP
487}
488
67210a55
JR
489/* Returns true if 'flow' meets the prerequisites for 'mf', false otherwise.
490 * Sets inspected bits in 'wc', if non-NULL. */
491bool
492mf_are_prereqs_ok(const struct mf_field *mf, const struct flow *flow,
493 struct flow_wildcards *wc)
494{
495 return mf_are_prereqs_ok__(mf, flow, NULL, wc);
496}
497
498/* Returns true if 'match' meets the prerequisites for 'mf', false otherwise.
499 */
500bool
501mf_are_match_prereqs_ok(const struct mf_field *mf, const struct match *match)
502{
503 return mf_are_prereqs_ok__(mf, &match->flow, &match->wc, NULL);
504}
505
6a885fd0
BP
506/* Returns true if 'value' may be a valid value *as part of a masked match*,
507 * false otherwise.
508 *
509 * A value is not rejected just because it is not valid for the field in
510 * question, but only if it doesn't make sense to test the bits in question at
511 * all. For example, the MFF_VLAN_TCI field will never have a nonzero value
512 * without the VLAN_CFI bit being set, but we can't reject those values because
513 * it is still legitimate to test just for those bits (see the documentation
0da61033 514 * for NXM_OF_VLAN_TCI in meta-flow.h). On the other hand, there is never a
530180fd 515 * reason to set the low bit of MFF_IP_DSCP to 1, so we reject that. */
6a885fd0
BP
516bool
517mf_is_value_valid(const struct mf_field *mf, const union mf_value *value)
518{
519 switch (mf->id) {
a79f29f2
AZ
520 case MFF_DP_HASH:
521 case MFF_RECIRC_ID:
3d4b2e6e 522 case MFF_PACKET_TYPE:
18080541 523 case MFF_CONJ_ID:
6a885fd0 524 case MFF_TUN_ID:
4fe3445a
PS
525 case MFF_TUN_SRC:
526 case MFF_TUN_DST:
7dad8e9a
TLSC
527 case MFF_TUN_IPV6_SRC:
528 case MFF_TUN_IPV6_DST:
4fe3445a
PS
529 case MFF_TUN_TOS:
530 case MFF_TUN_TTL:
ac6073e3
MC
531 case MFF_TUN_GBP_ID:
532 case MFF_TUN_GBP_FLAGS:
7dc18ae9
WT
533 case MFF_TUN_ERSPAN_IDX:
534 case MFF_TUN_ERSPAN_VER:
535 case MFF_TUN_ERSPAN_DIR:
536 case MFF_TUN_ERSPAN_HWID:
3c6d05a0
WT
537 case MFF_TUN_GTPU_FLAGS:
538 case MFF_TUN_GTPU_MSGTYPE:
9558d2a5 539 CASE_MFF_TUN_METADATA:
969fc56c 540 case MFF_METADATA:
6a885fd0 541 case MFF_IN_PORT:
1b567fb9 542 case MFF_SKB_PRIORITY:
1362e248 543 case MFF_PKT_MARK:
07659514 544 case MFF_CT_ZONE:
8e53fe8c 545 case MFF_CT_MARK:
9daf2348 546 case MFF_CT_LABEL:
daf4d3c1
JR
547 case MFF_CT_NW_PROTO:
548 case MFF_CT_NW_SRC:
549 case MFF_CT_NW_DST:
550 case MFF_CT_IPV6_SRC:
551 case MFF_CT_IPV6_DST:
552 case MFF_CT_TP_SRC:
553 case MFF_CT_TP_DST:
0d7e2fe4 554 CASE_MFF_REGS:
79fe0f46 555 CASE_MFF_XREGS:
b23ada8e 556 CASE_MFF_XXREGS:
6a885fd0
BP
557 case MFF_ETH_SRC:
558 case MFF_ETH_DST:
559 case MFF_ETH_TYPE:
560 case MFF_VLAN_TCI:
bef3f465 561 case MFF_MPLS_TTL:
6a885fd0
BP
562 case MFF_IPV4_SRC:
563 case MFF_IPV4_DST:
564 case MFF_IPV6_SRC:
565 case MFF_IPV6_DST:
566 case MFF_IP_PROTO:
a61680c6 567 case MFF_IP_TTL:
6a885fd0
BP
568 case MFF_ARP_SPA:
569 case MFF_ARP_TPA:
570 case MFF_ARP_SHA:
571 case MFF_ARP_THA:
572 case MFF_TCP_SRC:
573 case MFF_TCP_DST:
574 case MFF_UDP_SRC:
575 case MFF_UDP_DST:
0d56eaf2
JS
576 case MFF_SCTP_SRC:
577 case MFF_SCTP_DST:
268a95e0
BP
578 case MFF_ICMPV4_TYPE:
579 case MFF_ICMPV4_CODE:
580 case MFF_ICMPV6_TYPE:
581 case MFF_ICMPV6_CODE:
6a885fd0
BP
582 case MFF_ND_TARGET:
583 case MFF_ND_SLL:
584 case MFF_ND_TLL:
9b2b8497
VDA
585 case MFF_ND_RESERVED:
586 case MFF_ND_OPTIONS_TYPE:
6a885fd0
BP
587 return true;
588
c61f3870
BP
589 case MFF_IN_PORT_OXM:
590 case MFF_ACTSET_OUTPUT: {
4e022ec0 591 ofp_port_t port;
72333065
JR
592 return !ofputil_port_from_ofp11(value->be32, &port);
593 }
594
530180fd 595 case MFF_IP_DSCP:
7257b535 596 return !(value->u8 & ~IP_DSCP_MASK);
1638b906
BP
597 case MFF_IP_DSCP_SHIFTED:
598 return !(value->u8 & (~IP_DSCP_MASK >> 2));
530180fd
JP
599 case MFF_IP_ECN:
600 return !(value->u8 & ~IP_ECN_MASK);
7257b535 601 case MFF_IP_FRAG:
eadef313 602 return !(value->u8 & ~FLOW_NW_FRAG_MASK);
dc235f7f
JR
603 case MFF_TCP_FLAGS:
604 return !(value->be16 & ~htons(0x0fff));
6a885fd0
BP
605
606 case MFF_ARP_OP:
607 return !(value->be16 & htons(0xff00));
608
441c57a9 609 case MFF_DL_VLAN:
6a885fd0 610 return !(value->be16 & htons(VLAN_CFI | VLAN_PCP_MASK));
cc34bc8c
BP
611 case MFF_VLAN_VID:
612 return !(value->be16 & htons(VLAN_PCP_MASK));
6a885fd0 613
441c57a9 614 case MFF_DL_VLAN_PCP:
cc34bc8c 615 case MFF_VLAN_PCP:
24016f5a 616 return !(value->u8 & ~(VLAN_PCP_MASK >> VLAN_PCP_SHIFT));
6a885fd0 617
fa8223b7
JP
618 case MFF_IPV6_LABEL:
619 return !(value->be32 & ~htonl(IPV6_LABEL_MASK));
620
b02475c5
SH
621 case MFF_MPLS_LABEL:
622 return !(value->be32 & ~htonl(MPLS_LABEL_MASK >> MPLS_LABEL_SHIFT));
623
624 case MFF_MPLS_TC:
625 return !(value->u8 & ~(MPLS_TC_MASK >> MPLS_TC_SHIFT));
626
627 case MFF_MPLS_BOS:
628 return !(value->u8 & ~(MPLS_BOS_MASK >> MPLS_BOS_SHIFT));
629
b666962b
JG
630 case MFF_TUN_FLAGS:
631 return !(value->be16 & ~htons(FLOW_TNL_PUB_F_MASK));
632
07659514
JS
633 case MFF_CT_STATE:
634 return !(value->be32 & ~htonl(CS_SUPPORTED_MASK));
635
3d2fbd70
JS
636 case MFF_NSH_FLAGS:
637 return true;
17553f27
YY
638 case MFF_NSH_TTL:
639 return (value->u8 <= 63);
3d2fbd70
JS
640 case MFF_NSH_MDTYPE:
641 return (value->u8 == 1 || value->u8 == 2);
642 case MFF_NSH_NP:
643 return true;
644 case MFF_NSH_SPI:
645 return !(value->be32 & htonl(0xFF000000));
646 case MFF_NSH_SI:
647 case MFF_NSH_C1:
648 case MFF_NSH_C2:
649 case MFF_NSH_C3:
650 case MFF_NSH_C4:
651 return true;
652
6a885fd0
BP
653 case MFF_N_IDS:
654 default:
428b2edd 655 OVS_NOT_REACHED();
6a885fd0
BP
656 }
657}
658
659/* Copies the value of field 'mf' from 'flow' into 'value'. The caller is
660 * responsible for ensuring that 'flow' meets 'mf''s prerequisites. */
661void
662mf_get_value(const struct mf_field *mf, const struct flow *flow,
663 union mf_value *value)
664{
665 switch (mf->id) {
a79f29f2
AZ
666 case MFF_DP_HASH:
667 value->be32 = htonl(flow->dp_hash);
668 break;
669 case MFF_RECIRC_ID:
670 value->be32 = htonl(flow->recirc_id);
671 break;
3d4b2e6e
JS
672 case MFF_PACKET_TYPE:
673 value->be32 = flow->packet_type;
674 break;
18080541
BP
675 case MFF_CONJ_ID:
676 value->be32 = htonl(flow->conj_id);
677 break;
6a885fd0 678 case MFF_TUN_ID:
296e07ac 679 value->be64 = flow->tunnel.tun_id;
6a885fd0 680 break;
4fe3445a
PS
681 case MFF_TUN_SRC:
682 value->be32 = flow->tunnel.ip_src;
683 break;
684 case MFF_TUN_DST:
685 value->be32 = flow->tunnel.ip_dst;
686 break;
7dad8e9a
TLSC
687 case MFF_TUN_IPV6_SRC:
688 value->ipv6 = flow->tunnel.ipv6_src;
689 break;
690 case MFF_TUN_IPV6_DST:
691 value->ipv6 = flow->tunnel.ipv6_dst;
692 break;
4fe3445a 693 case MFF_TUN_FLAGS:
b666962b 694 value->be16 = htons(flow->tunnel.flags & FLOW_TNL_PUB_F_MASK);
4fe3445a 695 break;
ac6073e3
MC
696 case MFF_TUN_GBP_ID:
697 value->be16 = flow->tunnel.gbp_id;
698 break;
699 case MFF_TUN_GBP_FLAGS:
700 value->u8 = flow->tunnel.gbp_flags;
701 break;
4fe3445a
PS
702 case MFF_TUN_TTL:
703 value->u8 = flow->tunnel.ip_ttl;
704 break;
705 case MFF_TUN_TOS:
706 value->u8 = flow->tunnel.ip_tos;
707 break;
7dc18ae9
WT
708 case MFF_TUN_ERSPAN_VER:
709 value->u8 = flow->tunnel.erspan_ver;
710 break;
711 case MFF_TUN_ERSPAN_IDX:
712 value->be32 = htonl(flow->tunnel.erspan_idx);
713 break;
714 case MFF_TUN_ERSPAN_DIR:
715 value->u8 = flow->tunnel.erspan_dir;
716 break;
717 case MFF_TUN_ERSPAN_HWID:
718 value->u8 = flow->tunnel.erspan_hwid;
719 break;
3c6d05a0
WT
720 case MFF_TUN_GTPU_FLAGS:
721 value->u8 = flow->tunnel.gtpu_flags;
722 break;
723 case MFF_TUN_GTPU_MSGTYPE:
724 value->u8 = flow->tunnel.gtpu_msgtype;
725 break;
9558d2a5 726 CASE_MFF_TUN_METADATA:
6728d578 727 tun_metadata_read(&flow->tunnel, mf, value);
9558d2a5 728 break;
4fe3445a 729
969fc56c
JS
730 case MFF_METADATA:
731 value->be64 = flow->metadata;
732 break;
6a885fd0
BP
733
734 case MFF_IN_PORT:
4e022ec0 735 value->be16 = htons(ofp_to_u16(flow->in_port.ofp_port));
6a885fd0 736 break;
72333065 737 case MFF_IN_PORT_OXM:
4e022ec0 738 value->be32 = ofputil_port_to_ofp11(flow->in_port.ofp_port);
72333065 739 break;
c61f3870
BP
740 case MFF_ACTSET_OUTPUT:
741 value->be32 = ofputil_port_to_ofp11(flow->actset_output);
742 break;
72333065 743
1b567fb9 744 case MFF_SKB_PRIORITY:
a81f0a44 745 value->be32 = htonl(flow->skb_priority);
1b567fb9
AA
746 break;
747
1362e248
JG
748 case MFF_PKT_MARK:
749 value->be32 = htonl(flow->pkt_mark);
1b567fb9
AA
750 break;
751
07659514
JS
752 case MFF_CT_STATE:
753 value->be32 = htonl(flow->ct_state);
754 break;
755
756 case MFF_CT_ZONE:
757 value->be16 = htons(flow->ct_zone);
758 break;
759
8e53fe8c
JS
760 case MFF_CT_MARK:
761 value->be32 = htonl(flow->ct_mark);
762 break;
763
9daf2348 764 case MFF_CT_LABEL:
32ea15f6 765 value->be128 = hton128(flow->ct_label);
9daf2348
JS
766 break;
767
daf4d3c1
JR
768 case MFF_CT_NW_PROTO:
769 value->u8 = flow->ct_nw_proto;
770 break;
771
772 case MFF_CT_NW_SRC:
773 value->be32 = flow->ct_nw_src;
774 break;
775
776 case MFF_CT_NW_DST:
777 value->be32 = flow->ct_nw_dst;
778 break;
779
780 case MFF_CT_IPV6_SRC:
781 value->ipv6 = flow->ct_ipv6_src;
782 break;
783
784 case MFF_CT_IPV6_DST:
785 value->ipv6 = flow->ct_ipv6_dst;
786 break;
787
788 case MFF_CT_TP_SRC:
789 value->be16 = flow->ct_tp_src;
790 break;
791
792 case MFF_CT_TP_DST:
793 value->be16 = flow->ct_tp_dst;
794 break;
795
0d7e2fe4 796 CASE_MFF_REGS:
2f98b0b7 797 value->be32 = htonl(flow->regs[mf->id - MFF_REG0]);
6a885fd0
BP
798 break;
799
79fe0f46
BP
800 CASE_MFF_XREGS:
801 value->be64 = htonll(flow_get_xreg(flow, mf->id - MFF_XREG0));
802 break;
803
b23ada8e
JP
804 CASE_MFF_XXREGS:
805 value->be128 = hton128(flow_get_xxreg(flow, mf->id - MFF_XXREG0));
806 break;
807
6a885fd0 808 case MFF_ETH_SRC:
74ff3298 809 value->mac = flow->dl_src;
6a885fd0
BP
810 break;
811
812 case MFF_ETH_DST:
74ff3298 813 value->mac = flow->dl_dst;
6a885fd0
BP
814 break;
815
816 case MFF_ETH_TYPE:
817 value->be16 = flow->dl_type;
818 break;
819
820 case MFF_VLAN_TCI:
f0fb825a 821 value->be16 = flow->vlans[0].tci;
6a885fd0
BP
822 break;
823
441c57a9 824 case MFF_DL_VLAN:
f0fb825a 825 value->be16 = flow->vlans[0].tci & htons(VLAN_VID_MASK);
6a885fd0 826 break;
cc34bc8c 827 case MFF_VLAN_VID:
f0fb825a 828 value->be16 = flow->vlans[0].tci & htons(VLAN_VID_MASK | VLAN_CFI);
cc34bc8c 829 break;
6a885fd0 830
441c57a9 831 case MFF_DL_VLAN_PCP:
cc34bc8c 832 case MFF_VLAN_PCP:
f0fb825a 833 value->u8 = vlan_tci_to_pcp(flow->vlans[0].tci);
6a885fd0
BP
834 break;
835
b02475c5 836 case MFF_MPLS_LABEL:
8bfd0fda 837 value->be32 = htonl(mpls_lse_to_label(flow->mpls_lse[0]));
b02475c5
SH
838 break;
839
840 case MFF_MPLS_TC:
8bfd0fda 841 value->u8 = mpls_lse_to_tc(flow->mpls_lse[0]);
b02475c5
SH
842 break;
843
844 case MFF_MPLS_BOS:
8bfd0fda
BP
845 value->u8 = mpls_lse_to_bos(flow->mpls_lse[0]);
846 break;
b02475c5 847
bef3f465
BP
848 case MFF_MPLS_TTL:
849 value->u8 = mpls_lse_to_ttl(flow->mpls_lse[0]);
850 break;
851
6a885fd0
BP
852 case MFF_IPV4_SRC:
853 value->be32 = flow->nw_src;
854 break;
855
856 case MFF_IPV4_DST:
857 value->be32 = flow->nw_dst;
858 break;
859
860 case MFF_IPV6_SRC:
861 value->ipv6 = flow->ipv6_src;
862 break;
863
864 case MFF_IPV6_DST:
865 value->ipv6 = flow->ipv6_dst;
866 break;
867
fa8223b7
JP
868 case MFF_IPV6_LABEL:
869 value->be32 = flow->ipv6_label;
870 break;
871
6a885fd0
BP
872 case MFF_IP_PROTO:
873 value->u8 = flow->nw_proto;
874 break;
875
530180fd 876 case MFF_IP_DSCP:
eadef313 877 value->u8 = flow->nw_tos & IP_DSCP_MASK;
7257b535
BP
878 break;
879
1638b906
BP
880 case MFF_IP_DSCP_SHIFTED:
881 value->u8 = flow->nw_tos >> 2;
882 break;
883
530180fd 884 case MFF_IP_ECN:
eadef313 885 value->u8 = flow->nw_tos & IP_ECN_MASK;
530180fd
JP
886 break;
887
a61680c6
JP
888 case MFF_IP_TTL:
889 value->u8 = flow->nw_ttl;
890 break;
891
7257b535 892 case MFF_IP_FRAG:
eadef313 893 value->u8 = flow->nw_frag;
6a885fd0
BP
894 break;
895
896 case MFF_ARP_OP:
897 value->be16 = htons(flow->nw_proto);
898 break;
899
900 case MFF_ARP_SPA:
901 value->be32 = flow->nw_src;
902 break;
903
904 case MFF_ARP_TPA:
905 value->be32 = flow->nw_dst;
906 break;
907
908 case MFF_ARP_SHA:
909 case MFF_ND_SLL:
74ff3298 910 value->mac = flow->arp_sha;
6a885fd0
BP
911 break;
912
913 case MFF_ARP_THA:
914 case MFF_ND_TLL:
74ff3298 915 value->mac = flow->arp_tha;
6a885fd0
BP
916 break;
917
918 case MFF_TCP_SRC:
6a885fd0 919 case MFF_UDP_SRC:
0d56eaf2 920 case MFF_SCTP_SRC:
6a885fd0
BP
921 value->be16 = flow->tp_src;
922 break;
923
71baec06 924 case MFF_TCP_DST:
6a885fd0 925 case MFF_UDP_DST:
0d56eaf2 926 case MFF_SCTP_DST:
6a885fd0
BP
927 value->be16 = flow->tp_dst;
928 break;
929
dc235f7f 930 case MFF_TCP_FLAGS:
9b2b8497 931 case MFF_ND_OPTIONS_TYPE:
dc235f7f
JR
932 value->be16 = flow->tcp_flags;
933 break;
934
9b2b8497
VDA
935 case MFF_ND_RESERVED:
936 value->be32 = flow->igmp_group_ip4;
937 break;
938
268a95e0
BP
939 case MFF_ICMPV4_TYPE:
940 case MFF_ICMPV6_TYPE:
6a885fd0
BP
941 value->u8 = ntohs(flow->tp_src);
942 break;
943
268a95e0
BP
944 case MFF_ICMPV4_CODE:
945 case MFF_ICMPV6_CODE:
6a885fd0
BP
946 value->u8 = ntohs(flow->tp_dst);
947 break;
948
949 case MFF_ND_TARGET:
950 value->ipv6 = flow->nd_target;
951 break;
952
3d2fbd70
JS
953 case MFF_NSH_FLAGS:
954 value->u8 = flow->nsh.flags;
955 break;
17553f27
YY
956 case MFF_NSH_TTL:
957 value->u8 = flow->nsh.ttl;
958 break;
3d2fbd70
JS
959 case MFF_NSH_MDTYPE:
960 value->u8 = flow->nsh.mdtype;
961 break;
962 case MFF_NSH_NP:
963 value->u8 = flow->nsh.np;
964 break;
965 case MFF_NSH_SPI:
17553f27
YY
966 value->be32 = nsh_path_hdr_to_spi(flow->nsh.path_hdr);
967 if (value->be32 == htonl(NSH_SPI_MASK >> NSH_SPI_SHIFT)) {
968 value->be32 = OVS_BE32_MAX;
969 }
3d2fbd70
JS
970 break;
971 case MFF_NSH_SI:
17553f27 972 value->u8 = nsh_path_hdr_to_si(flow->nsh.path_hdr);
3d2fbd70
JS
973 break;
974 case MFF_NSH_C1:
975 case MFF_NSH_C2:
976 case MFF_NSH_C3:
977 case MFF_NSH_C4:
f59cb331 978 value->be32 = flow->nsh.context[mf->id - MFF_NSH_C1];
3d2fbd70
JS
979 break;
980
6a885fd0
BP
981 case MFF_N_IDS:
982 default:
428b2edd 983 OVS_NOT_REACHED();
6a885fd0
BP
984 }
985}
986
81a76618
BP
987/* Makes 'match' match field 'mf' exactly, with the value matched taken from
988 * 'value'. The caller is responsible for ensuring that 'match' meets 'mf''s
4f7b100c
JG
989 * prerequisites.
990 *
991 * If non-NULL, 'err_str' returns a malloc'ed string describing any errors
992 * with the request or NULL if there is no error. The caller is reponsible
993 * for freeing the string. */
6a885fd0
BP
994void
995mf_set_value(const struct mf_field *mf,
4f7b100c 996 const union mf_value *value, struct match *match, char **err_str)
6a885fd0 997{
4f7b100c
JG
998 if (err_str) {
999 *err_str = NULL;
1000 }
1001
6a885fd0 1002 switch (mf->id) {
a79f29f2
AZ
1003 case MFF_DP_HASH:
1004 match_set_dp_hash(match, ntohl(value->be32));
1005 break;
1006 case MFF_RECIRC_ID:
1007 match_set_recirc_id(match, ntohl(value->be32));
1008 break;
3d4b2e6e
JS
1009 case MFF_PACKET_TYPE:
1010 match_set_packet_type(match, value->be32);
1011 break;
18080541
BP
1012 case MFF_CONJ_ID:
1013 match_set_conj_id(match, ntohl(value->be32));
1014 break;
6a885fd0 1015 case MFF_TUN_ID:
81a76618 1016 match_set_tun_id(match, value->be64);
6a885fd0 1017 break;
4fe3445a
PS
1018 case MFF_TUN_SRC:
1019 match_set_tun_src(match, value->be32);
1020 break;
1021 case MFF_TUN_DST:
1022 match_set_tun_dst(match, value->be32);
1023 break;
7dad8e9a
TLSC
1024 case MFF_TUN_IPV6_SRC:
1025 match_set_tun_ipv6_src(match, &value->ipv6);
1026 break;
1027 case MFF_TUN_IPV6_DST:
1028 match_set_tun_ipv6_dst(match, &value->ipv6);
1029 break;
4fe3445a
PS
1030 case MFF_TUN_FLAGS:
1031 match_set_tun_flags(match, ntohs(value->be16));
1032 break;
ac6073e3
MC
1033 case MFF_TUN_GBP_ID:
1034 match_set_tun_gbp_id(match, value->be16);
1035 break;
1036 case MFF_TUN_GBP_FLAGS:
1037 match_set_tun_gbp_flags(match, value->u8);
1038 break;
4fe3445a
PS
1039 case MFF_TUN_TOS:
1040 match_set_tun_tos(match, value->u8);
1041 break;
1042 case MFF_TUN_TTL:
1043 match_set_tun_ttl(match, value->u8);
1044 break;
7dc18ae9
WT
1045 case MFF_TUN_ERSPAN_VER:
1046 match_set_tun_erspan_ver(match, value->u8);
1047 break;
1048 case MFF_TUN_ERSPAN_IDX:
1049 match_set_tun_erspan_idx(match, ntohl(value->be32));
1050 break;
1051 case MFF_TUN_ERSPAN_DIR:
1052 match_set_tun_erspan_dir(match, value->u8);
1053 break;
1054 case MFF_TUN_ERSPAN_HWID:
1055 match_set_tun_erspan_hwid(match, value->u8);
1056 break;
3c6d05a0
WT
1057 case MFF_TUN_GTPU_FLAGS:
1058 match_set_tun_gtpu_flags(match, value->u8);
1059 break;
1060 case MFF_TUN_GTPU_MSGTYPE:
1061 match_set_tun_gtpu_msgtype(match, value->u8);
1062 break;
9558d2a5 1063 CASE_MFF_TUN_METADATA:
4f7b100c 1064 tun_metadata_set_match(mf, value, NULL, match, err_str);
9558d2a5 1065 break;
4fe3445a 1066
969fc56c 1067 case MFF_METADATA:
81a76618 1068 match_set_metadata(match, value->be64);
969fc56c 1069 break;
6a885fd0
BP
1070
1071 case MFF_IN_PORT:
4e022ec0 1072 match_set_in_port(match, u16_to_ofp(ntohs(value->be16)));
6a885fd0
BP
1073 break;
1074
72333065 1075 case MFF_IN_PORT_OXM: {
4e022ec0 1076 ofp_port_t port;
bc146369 1077 ofputil_port_from_ofp11(value->be32, &port);
72333065
JR
1078 match_set_in_port(match, port);
1079 break;
1080 }
c61f3870
BP
1081 case MFF_ACTSET_OUTPUT: {
1082 ofp_port_t port;
1083 ofputil_port_from_ofp11(value->be32, &port);
1084 match_set_actset_output(match, port);
1085 break;
1086 }
72333065 1087
1b567fb9
AA
1088 case MFF_SKB_PRIORITY:
1089 match_set_skb_priority(match, ntohl(value->be32));
1090 break;
1091
1362e248
JG
1092 case MFF_PKT_MARK:
1093 match_set_pkt_mark(match, ntohl(value->be32));
1b567fb9
AA
1094 break;
1095
07659514
JS
1096 case MFF_CT_STATE:
1097 match_set_ct_state(match, ntohl(value->be32));
1098 break;
1099
1100 case MFF_CT_ZONE:
1101 match_set_ct_zone(match, ntohs(value->be16));
1102 break;
1103
8e53fe8c
JS
1104 case MFF_CT_MARK:
1105 match_set_ct_mark(match, ntohl(value->be32));
1106 break;
1107
32ea15f6
JP
1108 case MFF_CT_LABEL:
1109 match_set_ct_label(match, ntoh128(value->be128));
9daf2348 1110 break;
9daf2348 1111
daf4d3c1
JR
1112 case MFF_CT_NW_PROTO:
1113 match_set_ct_nw_proto(match, value->u8);
1114 break;
1115
1116 case MFF_CT_NW_SRC:
1117 match_set_ct_nw_src(match, value->be32);
1118 break;
1119
1120 case MFF_CT_NW_DST:
1121 match_set_ct_nw_dst(match, value->be32);
1122 break;
1123
1124 case MFF_CT_IPV6_SRC:
1125 match_set_ct_ipv6_src(match, &value->ipv6);
1126 break;
1127
1128 case MFF_CT_IPV6_DST:
1129 match_set_ct_ipv6_dst(match, &value->ipv6);
1130 break;
1131
1132 case MFF_CT_TP_SRC:
1133 match_set_ct_tp_src(match, value->be16);
1134 break;
1135
1136 case MFF_CT_TP_DST:
1137 match_set_ct_tp_dst(match, value->be16);
1138 break;
1139
0d7e2fe4 1140 CASE_MFF_REGS:
81a76618 1141 match_set_reg(match, mf->id - MFF_REG0, ntohl(value->be32));
6a885fd0 1142 break;
6a885fd0 1143
79fe0f46
BP
1144 CASE_MFF_XREGS:
1145 match_set_xreg(match, mf->id - MFF_XREG0, ntohll(value->be64));
1146 break;
1147
b23ada8e
JP
1148 CASE_MFF_XXREGS:
1149 match_set_xxreg(match, mf->id - MFF_XXREG0, ntoh128(value->be128));
1150 break;
1151
6a885fd0 1152 case MFF_ETH_SRC:
81a76618 1153 match_set_dl_src(match, value->mac);
6a885fd0
BP
1154 break;
1155
1156 case MFF_ETH_DST:
81a76618 1157 match_set_dl_dst(match, value->mac);
6a885fd0
BP
1158 break;
1159
1160 case MFF_ETH_TYPE:
81a76618 1161 match_set_dl_type(match, value->be16);
6a885fd0
BP
1162 break;
1163
1164 case MFF_VLAN_TCI:
81a76618 1165 match_set_dl_tci(match, value->be16);
6a885fd0
BP
1166 break;
1167
441c57a9 1168 case MFF_DL_VLAN:
2f9366be 1169 match_set_dl_vlan(match, value->be16, 0);
6a885fd0 1170 break;
cc34bc8c 1171 case MFF_VLAN_VID:
81a76618 1172 match_set_vlan_vid(match, value->be16);
cc34bc8c 1173 break;
6a885fd0 1174
441c57a9 1175 case MFF_DL_VLAN_PCP:
cc34bc8c 1176 case MFF_VLAN_PCP:
2f9366be 1177 match_set_dl_vlan_pcp(match, value->u8, 0);
6a885fd0
BP
1178 break;
1179
b02475c5 1180 case MFF_MPLS_LABEL:
8bfd0fda 1181 match_set_mpls_label(match, 0, value->be32);
b02475c5
SH
1182 break;
1183
1184 case MFF_MPLS_TC:
8bfd0fda 1185 match_set_mpls_tc(match, 0, value->u8);
b02475c5
SH
1186 break;
1187
1188 case MFF_MPLS_BOS:
8bfd0fda
BP
1189 match_set_mpls_bos(match, 0, value->u8);
1190 break;
b02475c5 1191
bef3f465
BP
1192 case MFF_MPLS_TTL:
1193 match_set_mpls_ttl(match, 0, value->u8);
1194 break;
1195
6a885fd0 1196 case MFF_IPV4_SRC:
81a76618 1197 match_set_nw_src(match, value->be32);
6a885fd0
BP
1198 break;
1199
1200 case MFF_IPV4_DST:
81a76618 1201 match_set_nw_dst(match, value->be32);
6a885fd0
BP
1202 break;
1203
1204 case MFF_IPV6_SRC:
81a76618 1205 match_set_ipv6_src(match, &value->ipv6);
6a885fd0
BP
1206 break;
1207
1208 case MFF_IPV6_DST:
81a76618 1209 match_set_ipv6_dst(match, &value->ipv6);
6a885fd0
BP
1210 break;
1211
fa8223b7 1212 case MFF_IPV6_LABEL:
81a76618 1213 match_set_ipv6_label(match, value->be32);
fa8223b7
JP
1214 break;
1215
6a885fd0 1216 case MFF_IP_PROTO:
81a76618 1217 match_set_nw_proto(match, value->u8);
6a885fd0
BP
1218 break;
1219
530180fd 1220 case MFF_IP_DSCP:
81a76618 1221 match_set_nw_dscp(match, value->u8);
530180fd
JP
1222 break;
1223
1638b906
BP
1224 case MFF_IP_DSCP_SHIFTED:
1225 match_set_nw_dscp(match, value->u8 << 2);
1226 break;
1227
530180fd 1228 case MFF_IP_ECN:
81a76618 1229 match_set_nw_ecn(match, value->u8);
6a885fd0
BP
1230 break;
1231
a61680c6 1232 case MFF_IP_TTL:
81a76618 1233 match_set_nw_ttl(match, value->u8);
a61680c6
JP
1234 break;
1235
7257b535 1236 case MFF_IP_FRAG:
81a76618 1237 match_set_nw_frag(match, value->u8);
7257b535
BP
1238 break;
1239
6a885fd0 1240 case MFF_ARP_OP:
81a76618 1241 match_set_nw_proto(match, ntohs(value->be16));
6a885fd0
BP
1242 break;
1243
1244 case MFF_ARP_SPA:
81a76618 1245 match_set_nw_src(match, value->be32);
6a885fd0
BP
1246 break;
1247
1248 case MFF_ARP_TPA:
81a76618 1249 match_set_nw_dst(match, value->be32);
6a885fd0
BP
1250 break;
1251
1252 case MFF_ARP_SHA:
1253 case MFF_ND_SLL:
81a76618 1254 match_set_arp_sha(match, value->mac);
6a885fd0
BP
1255 break;
1256
1257 case MFF_ARP_THA:
1258 case MFF_ND_TLL:
81a76618 1259 match_set_arp_tha(match, value->mac);
6a885fd0
BP
1260 break;
1261
1262 case MFF_TCP_SRC:
6a885fd0 1263 case MFF_UDP_SRC:
0d56eaf2 1264 case MFF_SCTP_SRC:
81a76618 1265 match_set_tp_src(match, value->be16);
6a885fd0
BP
1266 break;
1267
71baec06 1268 case MFF_TCP_DST:
6a885fd0 1269 case MFF_UDP_DST:
0d56eaf2 1270 case MFF_SCTP_DST:
81a76618 1271 match_set_tp_dst(match, value->be16);
6a885fd0
BP
1272 break;
1273
dc235f7f
JR
1274 case MFF_TCP_FLAGS:
1275 match_set_tcp_flags(match, value->be16);
1276 break;
1277
268a95e0
BP
1278 case MFF_ICMPV4_TYPE:
1279 case MFF_ICMPV6_TYPE:
81a76618 1280 match_set_icmp_type(match, value->u8);
6a885fd0
BP
1281 break;
1282
268a95e0
BP
1283 case MFF_ICMPV4_CODE:
1284 case MFF_ICMPV6_CODE:
81a76618 1285 match_set_icmp_code(match, value->u8);
6a885fd0
BP
1286 break;
1287
1288 case MFF_ND_TARGET:
81a76618 1289 match_set_nd_target(match, &value->ipv6);
6a885fd0
BP
1290 break;
1291
9b2b8497
VDA
1292 case MFF_ND_RESERVED:
1293 match_set_nd_reserved(match, value->be32);
1294 break;
1295
1296 case MFF_ND_OPTIONS_TYPE:
1297 match_set_nd_options_type(match, value->u8);
1298 break;
1299
3d2fbd70
JS
1300 case MFF_NSH_FLAGS:
1301 MATCH_SET_FIELD_UINT8(match, nsh.flags, value->u8);
1302 break;
17553f27
YY
1303 case MFF_NSH_TTL:
1304 MATCH_SET_FIELD_UINT8(match, nsh.ttl, value->u8);
1305 break;
3d2fbd70
JS
1306 case MFF_NSH_MDTYPE:
1307 MATCH_SET_FIELD_UINT8(match, nsh.mdtype, value->u8);
1308 break;
1309 case MFF_NSH_NP:
1310 MATCH_SET_FIELD_UINT8(match, nsh.np, value->u8);
1311 break;
1312 case MFF_NSH_SPI:
17553f27
YY
1313 match->wc.masks.nsh.path_hdr |= htonl(NSH_SPI_MASK);
1314 nsh_path_hdr_set_spi(&match->flow.nsh.path_hdr, value->be32);
3d2fbd70
JS
1315 break;
1316 case MFF_NSH_SI:
17553f27
YY
1317 match->wc.masks.nsh.path_hdr |= htonl(NSH_SI_MASK);
1318 nsh_path_hdr_set_si(&match->flow.nsh.path_hdr, value->u8);
3d2fbd70
JS
1319 break;
1320 case MFF_NSH_C1:
1321 case MFF_NSH_C2:
1322 case MFF_NSH_C3:
1323 case MFF_NSH_C4:
f59cb331
YY
1324 MATCH_SET_FIELD_BE32(match, nsh.context[mf->id - MFF_NSH_C1],
1325 value->be32);
3d2fbd70
JS
1326 break;
1327
6a885fd0
BP
1328 case MFF_N_IDS:
1329 default:
428b2edd 1330 OVS_NOT_REACHED();
6a885fd0
BP
1331 }
1332}
1333
5cd4ead5
JR
1334/* Unwildcard the bits in 'mask' of the 'wc' member field described by 'mf'.
1335 * The caller is responsible for ensuring that 'wc' meets 'mf''s
1336 * prerequisites. */
5a0a5702 1337void
5cd4ead5
JR
1338mf_mask_field_masked(const struct mf_field *mf, const union mf_value *mask,
1339 struct flow_wildcards *wc)
5a0a5702 1340{
5cd4ead5
JR
1341 union mf_value temp_mask;
1342 /* For MFF_DL_VLAN, we cannot send a all 1's to flow_set_dl_vlan() as that
1343 * will be considered as OFP10_VLAN_NONE. So make sure the mask only has
1344 * valid bits in this case. */
5a0a5702 1345 if (mf->id == MFF_DL_VLAN) {
5cd4ead5
JR
1346 temp_mask.be16 = htons(VLAN_VID_MASK) & mask->be16;
1347 mask = &temp_mask;
5a0a5702 1348 }
5cd4ead5
JR
1349
1350 union mf_value mask_value;
1351
1352 mf_get_value(mf, &wc->masks, &mask_value);
1353 for (size_t i = 0; i < mf->n_bytes; i++) {
1354 mask_value.b[i] |= mask->b[i];
1355 }
1356 mf_set_flow_value(mf, &mask_value, &wc->masks);
1357}
1358
1359/* Unwildcard 'wc' member field described by 'mf'. The caller is
1360 * responsible for ensuring that 'mask' meets 'mf''s prerequisites. */
1361void
1362mf_mask_field(const struct mf_field *mf, struct flow_wildcards *wc)
1363{
1364 mf_mask_field_masked(mf, &exact_match_mask, wc);
5a0a5702
GS
1365}
1366
4ede8c79
JG
1367static int
1368field_len(const struct mf_field *mf, const union mf_value *value_)
1369{
1370 const uint8_t *value = &value_->u8;
1371 int i;
1372
1373 if (!mf->variable_len) {
1374 return mf->n_bytes;
1375 }
1376
1377 if (!value) {
1378 return 0;
1379 }
1380
1381 for (i = 0; i < mf->n_bytes; i++) {
1382 if (value[i] != 0) {
1383 break;
1384 }
1385 }
1386
1387 return mf->n_bytes - i;
1388}
1389
1390/* Returns the effective length of the field. For fixed length fields,
1391 * this is just the defined length. For variable length fields, it is
1392 * the minimum size encoding that retains the same meaning (i.e.
1cb20095
JG
1393 * discarding leading zeros).
1394 *
1395 * 'is_masked' returns (if non-NULL) whether the original contained
1396 * a mask. Otherwise, a mask that is the same length as the value
1397 * might be misinterpreted as an exact match. */
4ede8c79
JG
1398int
1399mf_field_len(const struct mf_field *mf, const union mf_value *value,
1cb20095 1400 const union mf_value *mask, bool *is_masked_)
4ede8c79
JG
1401{
1402 int len, mask_len;
1cb20095 1403 bool is_masked = mask && !is_all_ones(mask, mf->n_bytes);
4ede8c79
JG
1404
1405 len = field_len(mf, value);
1cb20095 1406 if (is_masked) {
4ede8c79
JG
1407 mask_len = field_len(mf, mask);
1408 len = MAX(len, mask_len);
1409 }
1410
1cb20095
JG
1411 if (is_masked_) {
1412 *is_masked_ = is_masked;
1413 }
1414
4ede8c79
JG
1415 return len;
1416}
1417
910fb1d8
JP
1418/* Sets 'flow' member field described by 'mf' to 'value'. The caller is
1419 * responsible for ensuring that 'flow' meets 'mf''s prerequisites.*/
28da1f8f
BP
1420void
1421mf_set_flow_value(const struct mf_field *mf,
1422 const union mf_value *value, struct flow *flow)
1423{
1424 switch (mf->id) {
a79f29f2
AZ
1425 case MFF_DP_HASH:
1426 flow->dp_hash = ntohl(value->be32);
1427 break;
1428 case MFF_RECIRC_ID:
1429 flow->recirc_id = ntohl(value->be32);
1430 break;
3d4b2e6e
JS
1431 case MFF_PACKET_TYPE:
1432 flow->packet_type = value->be32;
1433 break;
18080541
BP
1434 case MFF_CONJ_ID:
1435 flow->conj_id = ntohl(value->be32);
1436 break;
28da1f8f 1437 case MFF_TUN_ID:
296e07ac 1438 flow->tunnel.tun_id = value->be64;
28da1f8f 1439 break;
4fe3445a
PS
1440 case MFF_TUN_SRC:
1441 flow->tunnel.ip_src = value->be32;
1442 break;
1443 case MFF_TUN_DST:
1444 flow->tunnel.ip_dst = value->be32;
1445 break;
7dad8e9a
TLSC
1446 case MFF_TUN_IPV6_SRC:
1447 flow->tunnel.ipv6_src = value->ipv6;
1448 break;
1449 case MFF_TUN_IPV6_DST:
1450 flow->tunnel.ipv6_dst = value->ipv6;
1451 break;
4fe3445a 1452 case MFF_TUN_FLAGS:
b666962b
JG
1453 flow->tunnel.flags = (flow->tunnel.flags & ~FLOW_TNL_PUB_F_MASK) |
1454 ntohs(value->be16);
4fe3445a 1455 break;
ac6073e3
MC
1456 case MFF_TUN_GBP_ID:
1457 flow->tunnel.gbp_id = value->be16;
1458 break;
1459 case MFF_TUN_GBP_FLAGS:
1460 flow->tunnel.gbp_flags = value->u8;
1461 break;
4fe3445a
PS
1462 case MFF_TUN_TOS:
1463 flow->tunnel.ip_tos = value->u8;
1464 break;
1465 case MFF_TUN_TTL:
1466 flow->tunnel.ip_ttl = value->u8;
1467 break;
7dc18ae9
WT
1468 case MFF_TUN_ERSPAN_VER:
1469 flow->tunnel.erspan_ver = value->u8;
1470 break;
1471 case MFF_TUN_ERSPAN_IDX:
1472 flow->tunnel.erspan_idx = ntohl(value->be32);
1473 break;
1474 case MFF_TUN_ERSPAN_DIR:
1475 flow->tunnel.erspan_dir = value->u8;
1476 break;
1477 case MFF_TUN_ERSPAN_HWID:
1478 flow->tunnel.erspan_hwid = value->u8;
1479 break;
3c6d05a0
WT
1480 case MFF_TUN_GTPU_FLAGS:
1481 flow->tunnel.gtpu_flags = value->u8;
1482 break;
1483 case MFF_TUN_GTPU_MSGTYPE:
1484 flow->tunnel.gtpu_msgtype = value->u8;
1485 break;
9558d2a5 1486 CASE_MFF_TUN_METADATA:
6728d578 1487 tun_metadata_write(&flow->tunnel, mf, value);
1aaf0ada 1488 break;
969fc56c
JS
1489 case MFF_METADATA:
1490 flow->metadata = value->be64;
1491 break;
28da1f8f
BP
1492
1493 case MFF_IN_PORT:
4e022ec0 1494 flow->in_port.ofp_port = u16_to_ofp(ntohs(value->be16));
28da1f8f 1495 break;
c61f3870
BP
1496 case MFF_IN_PORT_OXM:
1497 ofputil_port_from_ofp11(value->be32, &flow->in_port.ofp_port);
1498 break;
1499 case MFF_ACTSET_OUTPUT:
1500 ofputil_port_from_ofp11(value->be32, &flow->actset_output);
72333065 1501 break;
72333065 1502
1b567fb9
AA
1503 case MFF_SKB_PRIORITY:
1504 flow->skb_priority = ntohl(value->be32);
1505 break;
1506
1362e248
JG
1507 case MFF_PKT_MARK:
1508 flow->pkt_mark = ntohl(value->be32);
1b567fb9
AA
1509 break;
1510
07659514
JS
1511 case MFF_CT_STATE:
1512 flow->ct_state = ntohl(value->be32);
1513 break;
1514
1515 case MFF_CT_ZONE:
1516 flow->ct_zone = ntohs(value->be16);
1517 break;
1518
8e53fe8c
JS
1519 case MFF_CT_MARK:
1520 flow->ct_mark = ntohl(value->be32);
1521 break;
1522
9daf2348 1523 case MFF_CT_LABEL:
32ea15f6 1524 flow->ct_label = ntoh128(value->be128);
9daf2348
JS
1525 break;
1526
daf4d3c1
JR
1527 case MFF_CT_NW_PROTO:
1528 flow->ct_nw_proto = value->u8;
1529 break;
1530
1531 case MFF_CT_NW_SRC:
1532 flow->ct_nw_src = value->be32;
1533 break;
1534
1535 case MFF_CT_NW_DST:
1536 flow->ct_nw_dst = value->be32;
1537 break;
1538
1539 case MFF_CT_IPV6_SRC:
1540 flow->ct_ipv6_src = value->ipv6;
1541 break;
1542
1543 case MFF_CT_IPV6_DST:
1544 flow->ct_ipv6_dst = value->ipv6;
1545 break;
1546
1547 case MFF_CT_TP_SRC:
1548 flow->ct_tp_src = value->be16;
1549 break;
1550
1551 case MFF_CT_TP_DST:
1552 flow->ct_tp_dst = value->be16;
1553 break;
1554
0d7e2fe4 1555 CASE_MFF_REGS:
28da1f8f
BP
1556 flow->regs[mf->id - MFF_REG0] = ntohl(value->be32);
1557 break;
28da1f8f 1558
79fe0f46
BP
1559 CASE_MFF_XREGS:
1560 flow_set_xreg(flow, mf->id - MFF_XREG0, ntohll(value->be64));
1561 break;
1562
b23ada8e
JP
1563 CASE_MFF_XXREGS:
1564 flow_set_xxreg(flow, mf->id - MFF_XXREG0, ntoh128(value->be128));
1565 break;
1566
28da1f8f 1567 case MFF_ETH_SRC:
74ff3298 1568 flow->dl_src = value->mac;
28da1f8f
BP
1569 break;
1570
1571 case MFF_ETH_DST:
74ff3298 1572 flow->dl_dst = value->mac;
28da1f8f
BP
1573 break;
1574
1575 case MFF_ETH_TYPE:
1576 flow->dl_type = value->be16;
1577 break;
1578
1579 case MFF_VLAN_TCI:
f0fb825a
EG
1580 flow->vlans[0].tci = value->be16;
1581 flow_fix_vlan_tpid(flow);
28da1f8f
BP
1582 break;
1583
441c57a9 1584 case MFF_DL_VLAN:
2f9366be 1585 flow_set_dl_vlan(flow, value->be16, 0);
f0fb825a 1586 flow_fix_vlan_tpid(flow);
28da1f8f 1587 break;
f0fb825a 1588
cc34bc8c
BP
1589 case MFF_VLAN_VID:
1590 flow_set_vlan_vid(flow, value->be16);
f0fb825a 1591 flow_fix_vlan_tpid(flow);
cc34bc8c 1592 break;
28da1f8f 1593
441c57a9 1594 case MFF_DL_VLAN_PCP:
cc34bc8c 1595 case MFF_VLAN_PCP:
2f9366be 1596 flow_set_vlan_pcp(flow, value->u8, 0);
f0fb825a 1597 flow_fix_vlan_tpid(flow);
28da1f8f
BP
1598 break;
1599
b02475c5 1600 case MFF_MPLS_LABEL:
8bfd0fda 1601 flow_set_mpls_label(flow, 0, value->be32);
b02475c5
SH
1602 break;
1603
1604 case MFF_MPLS_TC:
8bfd0fda 1605 flow_set_mpls_tc(flow, 0, value->u8);
b02475c5
SH
1606 break;
1607
1608 case MFF_MPLS_BOS:
8bfd0fda
BP
1609 flow_set_mpls_bos(flow, 0, value->u8);
1610 break;
b02475c5 1611
bef3f465
BP
1612 case MFF_MPLS_TTL:
1613 flow_set_mpls_ttl(flow, 0, value->u8);
1614 break;
1615
28da1f8f
BP
1616 case MFF_IPV4_SRC:
1617 flow->nw_src = value->be32;
1618 break;
1619
1620 case MFF_IPV4_DST:
1621 flow->nw_dst = value->be32;
1622 break;
1623
1624 case MFF_IPV6_SRC:
1625 flow->ipv6_src = value->ipv6;
1626 break;
1627
1628 case MFF_IPV6_DST:
1629 flow->ipv6_dst = value->ipv6;
1630 break;
1631
1632 case MFF_IPV6_LABEL:
88cc95c1 1633 flow->ipv6_label = value->be32 & htonl(IPV6_LABEL_MASK);
28da1f8f
BP
1634 break;
1635
1636 case MFF_IP_PROTO:
1637 flow->nw_proto = value->u8;
1638 break;
1639
1640 case MFF_IP_DSCP:
1641 flow->nw_tos &= ~IP_DSCP_MASK;
1642 flow->nw_tos |= value->u8 & IP_DSCP_MASK;
1643 break;
1644
1638b906
BP
1645 case MFF_IP_DSCP_SHIFTED:
1646 flow->nw_tos &= ~IP_DSCP_MASK;
1647 flow->nw_tos |= value->u8 << 2;
1648 break;
1649
28da1f8f
BP
1650 case MFF_IP_ECN:
1651 flow->nw_tos &= ~IP_ECN_MASK;
1652 flow->nw_tos |= value->u8 & IP_ECN_MASK;
1653 break;
1654
1655 case MFF_IP_TTL:
1656 flow->nw_ttl = value->u8;
1657 break;
1658
1659 case MFF_IP_FRAG:
c8abb8af 1660 flow->nw_frag = value->u8 & FLOW_NW_FRAG_MASK;
28da1f8f
BP
1661 break;
1662
1663 case MFF_ARP_OP:
1664 flow->nw_proto = ntohs(value->be16);
1665 break;
1666
1667 case MFF_ARP_SPA:
1668 flow->nw_src = value->be32;
1669 break;
1670
1671 case MFF_ARP_TPA:
1672 flow->nw_dst = value->be32;
1673 break;
1674
1675 case MFF_ARP_SHA:
1676 case MFF_ND_SLL:
74ff3298 1677 flow->arp_sha = value->mac;
28da1f8f
BP
1678 break;
1679
1680 case MFF_ARP_THA:
1681 case MFF_ND_TLL:
74ff3298 1682 flow->arp_tha = value->mac;
28da1f8f
BP
1683 break;
1684
1685 case MFF_TCP_SRC:
1686 case MFF_UDP_SRC:
0d56eaf2 1687 case MFF_SCTP_SRC:
28da1f8f
BP
1688 flow->tp_src = value->be16;
1689 break;
1690
1691 case MFF_TCP_DST:
1692 case MFF_UDP_DST:
0d56eaf2 1693 case MFF_SCTP_DST:
28da1f8f
BP
1694 flow->tp_dst = value->be16;
1695 break;
1696
dc235f7f
JR
1697 case MFF_TCP_FLAGS:
1698 flow->tcp_flags = value->be16;
1699 break;
1700
28da1f8f
BP
1701 case MFF_ICMPV4_TYPE:
1702 case MFF_ICMPV6_TYPE:
1703 flow->tp_src = htons(value->u8);
1704 break;
1705
1706 case MFF_ICMPV4_CODE:
1707 case MFF_ICMPV6_CODE:
1708 flow->tp_dst = htons(value->u8);
1709 break;
1710
1711 case MFF_ND_TARGET:
1712 flow->nd_target = value->ipv6;
1713 break;
1714
9b2b8497
VDA
1715 case MFF_ND_RESERVED:
1716 flow->igmp_group_ip4 = value->be32;
1717 break;
1718
1719 case MFF_ND_OPTIONS_TYPE:
1720 flow->tcp_flags = htons(value->u8);
1721 break;
1722
3d2fbd70
JS
1723 case MFF_NSH_FLAGS:
1724 flow->nsh.flags = value->u8;
1725 break;
17553f27
YY
1726 case MFF_NSH_TTL:
1727 flow->nsh.ttl = value->u8;
1728 break;
3d2fbd70
JS
1729 case MFF_NSH_MDTYPE:
1730 flow->nsh.mdtype = value->u8;
1731 break;
1732 case MFF_NSH_NP:
1733 flow->nsh.np = value->u8;
1734 break;
1735 case MFF_NSH_SPI:
17553f27 1736 nsh_path_hdr_set_spi(&flow->nsh.path_hdr, value->be32);
3d2fbd70
JS
1737 break;
1738 case MFF_NSH_SI:
17553f27 1739 nsh_path_hdr_set_si(&flow->nsh.path_hdr, value->u8);
3d2fbd70
JS
1740 break;
1741 case MFF_NSH_C1:
1742 case MFF_NSH_C2:
1743 case MFF_NSH_C3:
1744 case MFF_NSH_C4:
f59cb331 1745 flow->nsh.context[mf->id - MFF_NSH_C1] = value->be32;
3d2fbd70
JS
1746 break;
1747
28da1f8f
BP
1748 case MFF_N_IDS:
1749 default:
428b2edd 1750 OVS_NOT_REACHED();
28da1f8f
BP
1751 }
1752}
1753
7eb4b1f1
BP
1754/* Consider each of 'src', 'mask', and 'dst' as if they were arrays of 8*n
1755 * bits. Then, for each 0 <= i < 8 * n such that mask[i] == 1, sets dst[i] =
1756 * src[i]. */
1757static void
1758apply_mask(const uint8_t *src, const uint8_t *mask, uint8_t *dst, size_t n)
1759{
1760 size_t i;
1761
1762 for (i = 0; i < n; i++) {
1763 dst[i] = (src[i] & mask[i]) | (dst[i] & ~mask[i]);
1764 }
1765}
1766
1767/* Sets 'flow' member field described by 'field' to 'value', except that bits
1768 * for which 'mask' has a 0-bit keep their existing values. The caller is
1769 * responsible for ensuring that 'flow' meets 'field''s prerequisites.*/
1770void
1771mf_set_flow_value_masked(const struct mf_field *field,
1772 const union mf_value *value,
1773 const union mf_value *mask,
1774 struct flow *flow)
1775{
1776 union mf_value tmp;
1777
1778 mf_get_value(field, flow, &tmp);
1779 apply_mask((const uint8_t *) value, (const uint8_t *) mask,
1780 (uint8_t *) &tmp, field->n_bytes);
1781 mf_set_flow_value(field, &tmp, flow);
1782}
1783
1cb20095
JG
1784bool
1785mf_is_tun_metadata(const struct mf_field *mf)
1786{
1787 return mf->id >= MFF_TUN_METADATA0 &&
1788 mf->id < MFF_TUN_METADATA0 + TUN_METADATA_NUM_OPTS;
1789}
1790
d7892c81
YHW
1791bool
1792mf_is_pipeline_field(const struct mf_field *mf)
1793{
1794 switch (mf->id) {
1795 case MFF_TUN_ID:
1796 case MFF_TUN_SRC:
1797 case MFF_TUN_DST:
1798 case MFF_TUN_IPV6_SRC:
1799 case MFF_TUN_IPV6_DST:
1800 case MFF_TUN_FLAGS:
1801 case MFF_TUN_GBP_ID:
1802 case MFF_TUN_GBP_FLAGS:
7dc18ae9
WT
1803 case MFF_TUN_ERSPAN_VER:
1804 case MFF_TUN_ERSPAN_IDX:
1805 case MFF_TUN_ERSPAN_DIR:
1806 case MFF_TUN_ERSPAN_HWID:
3c6d05a0
WT
1807 case MFF_TUN_GTPU_FLAGS:
1808 case MFF_TUN_GTPU_MSGTYPE:
d7892c81
YHW
1809 CASE_MFF_TUN_METADATA:
1810 case MFF_METADATA:
1811 case MFF_IN_PORT:
1812 case MFF_IN_PORT_OXM:
1813 CASE_MFF_REGS:
1814 CASE_MFF_XREGS:
1815 CASE_MFF_XXREGS:
3d4b2e6e 1816 case MFF_PACKET_TYPE:
d7892c81
YHW
1817 return true;
1818
1819 case MFF_DP_HASH:
1820 case MFF_RECIRC_ID:
1821 case MFF_CONJ_ID:
1822 case MFF_TUN_TTL:
1823 case MFF_TUN_TOS:
1824 case MFF_ACTSET_OUTPUT:
1825 case MFF_SKB_PRIORITY:
1826 case MFF_PKT_MARK:
1827 case MFF_CT_STATE:
1828 case MFF_CT_ZONE:
1829 case MFF_CT_MARK:
1830 case MFF_CT_LABEL:
1831 case MFF_CT_NW_PROTO:
1832 case MFF_CT_NW_SRC:
1833 case MFF_CT_NW_DST:
1834 case MFF_CT_IPV6_SRC:
1835 case MFF_CT_IPV6_DST:
1836 case MFF_CT_TP_SRC:
1837 case MFF_CT_TP_DST:
1838 case MFF_ETH_SRC:
1839 case MFF_ETH_DST:
1840 case MFF_ETH_TYPE:
1841 case MFF_VLAN_TCI:
1842 case MFF_DL_VLAN:
1843 case MFF_VLAN_VID:
1844 case MFF_DL_VLAN_PCP:
1845 case MFF_VLAN_PCP:
1846 case MFF_MPLS_LABEL:
1847 case MFF_MPLS_TC:
1848 case MFF_MPLS_BOS:
1849 case MFF_MPLS_TTL:
1850 case MFF_IPV4_SRC:
1851 case MFF_IPV4_DST:
1852 case MFF_IPV6_SRC:
1853 case MFF_IPV6_DST:
1854 case MFF_IPV6_LABEL:
1855 case MFF_IP_PROTO:
1856 case MFF_IP_DSCP:
1857 case MFF_IP_DSCP_SHIFTED:
1858 case MFF_IP_ECN:
1859 case MFF_IP_TTL:
1860 case MFF_IP_FRAG:
1861 case MFF_ARP_OP:
1862 case MFF_ARP_SPA:
1863 case MFF_ARP_TPA:
1864 case MFF_ARP_SHA:
1865 case MFF_ARP_THA:
1866 case MFF_TCP_SRC:
1867 case MFF_TCP_DST:
1868 case MFF_TCP_FLAGS:
1869 case MFF_UDP_SRC:
1870 case MFF_UDP_DST:
1871 case MFF_SCTP_SRC:
1872 case MFF_SCTP_DST:
1873 case MFF_ICMPV4_TYPE:
1874 case MFF_ICMPV4_CODE:
1875 case MFF_ICMPV6_TYPE:
1876 case MFF_ICMPV6_CODE:
1877 case MFF_ND_TARGET:
1878 case MFF_ND_SLL:
1879 case MFF_ND_TLL:
9b2b8497
VDA
1880 case MFF_ND_RESERVED:
1881 case MFF_ND_OPTIONS_TYPE:
3d2fbd70 1882 case MFF_NSH_FLAGS:
17553f27 1883 case MFF_NSH_TTL:
3d2fbd70
JS
1884 case MFF_NSH_MDTYPE:
1885 case MFF_NSH_NP:
1886 case MFF_NSH_SPI:
1887 case MFF_NSH_SI:
1888 case MFF_NSH_C1:
1889 case MFF_NSH_C2:
1890 case MFF_NSH_C3:
1891 case MFF_NSH_C4:
d7892c81
YHW
1892 return false;
1893
1894 case MFF_N_IDS:
1895 default:
1896 OVS_NOT_REACHED();
1897 }
1898}
1899
1cb20095
JG
1900/* Returns true if 'mf' has previously been set in 'flow', false if
1901 * it contains a non-default value.
ccbe50f8
BP
1902 *
1903 * The caller is responsible for ensuring that 'flow' meets 'mf''s
1904 * prerequisites. */
1905bool
1cb20095 1906mf_is_set(const struct mf_field *mf, const struct flow *flow)
ccbe50f8 1907{
1cb20095
JG
1908 if (!mf_is_tun_metadata(mf)) {
1909 union mf_value value;
ccbe50f8 1910
1cb20095
JG
1911 mf_get_value(mf, flow, &value);
1912 return !is_all_zeros(&value, mf->n_bytes);
1913 } else {
1914 return ULLONG_GET(flow->tunnel.metadata.present.map,
1915 mf->id - MFF_TUN_METADATA0);
1916 }
ccbe50f8
BP
1917}
1918
81a76618 1919/* Makes 'match' wildcard field 'mf'.
6a885fd0 1920 *
81a76618 1921 * The caller is responsible for ensuring that 'match' meets 'mf''s
4f7b100c
JG
1922 * prerequisites.
1923 *
1924 * If non-NULL, 'err_str' returns a malloc'ed string describing any errors
1925 * with the request or NULL if there is no error. The caller is reponsible
1926 * for freeing the string. */
6a885fd0 1927void
4f7b100c 1928mf_set_wild(const struct mf_field *mf, struct match *match, char **err_str)
6a885fd0 1929{
4f7b100c
JG
1930 if (err_str) {
1931 *err_str = NULL;
1932 }
1933
6a885fd0 1934 switch (mf->id) {
a79f29f2
AZ
1935 case MFF_DP_HASH:
1936 match->flow.dp_hash = 0;
1937 match->wc.masks.dp_hash = 0;
1938 break;
1939 case MFF_RECIRC_ID:
1940 match->flow.recirc_id = 0;
1941 match->wc.masks.recirc_id = 0;
1942 break;
3d4b2e6e
JS
1943 case MFF_PACKET_TYPE:
1944 match->flow.packet_type = 0;
1945 match->wc.masks.packet_type = 0;
1946 break;
18080541
BP
1947 case MFF_CONJ_ID:
1948 match->flow.conj_id = 0;
1949 match->wc.masks.conj_id = 0;
1950 break;
6a885fd0 1951 case MFF_TUN_ID:
81a76618 1952 match_set_tun_id_masked(match, htonll(0), htonll(0));
6a885fd0 1953 break;
4fe3445a
PS
1954 case MFF_TUN_SRC:
1955 match_set_tun_src_masked(match, htonl(0), htonl(0));
1956 break;
1957 case MFF_TUN_DST:
1958 match_set_tun_dst_masked(match, htonl(0), htonl(0));
1959 break;
7dad8e9a
TLSC
1960 case MFF_TUN_IPV6_SRC:
1961 memset(&match->wc.masks.tunnel.ipv6_src, 0,
1962 sizeof match->wc.masks.tunnel.ipv6_src);
1963 memset(&match->flow.tunnel.ipv6_src, 0,
1964 sizeof match->flow.tunnel.ipv6_src);
1965 break;
1966 case MFF_TUN_IPV6_DST:
1967 memset(&match->wc.masks.tunnel.ipv6_dst, 0,
1968 sizeof match->wc.masks.tunnel.ipv6_dst);
1969 memset(&match->flow.tunnel.ipv6_dst, 0,
1970 sizeof match->flow.tunnel.ipv6_dst);
1971 break;
4fe3445a
PS
1972 case MFF_TUN_FLAGS:
1973 match_set_tun_flags_masked(match, 0, 0);
1974 break;
ac6073e3
MC
1975 case MFF_TUN_GBP_ID:
1976 match_set_tun_gbp_id_masked(match, 0, 0);
1977 break;
1978 case MFF_TUN_GBP_FLAGS:
1979 match_set_tun_gbp_flags_masked(match, 0, 0);
1980 break;
4fe3445a
PS
1981 case MFF_TUN_TOS:
1982 match_set_tun_tos_masked(match, 0, 0);
1983 break;
1984 case MFF_TUN_TTL:
1985 match_set_tun_ttl_masked(match, 0, 0);
1986 break;
7dc18ae9
WT
1987 case MFF_TUN_ERSPAN_VER:
1988 match_set_tun_erspan_ver_masked(match, 0, 0);
1989 break;
1990 case MFF_TUN_ERSPAN_IDX:
1991 match_set_tun_erspan_idx_masked(match, 0, 0);
1992 break;
1993 case MFF_TUN_ERSPAN_DIR:
1994 match_set_tun_erspan_dir_masked(match, 0, 0);
1995 break;
1996 case MFF_TUN_ERSPAN_HWID:
1997 match_set_tun_erspan_hwid_masked(match, 0, 0);
1998 break;
3c6d05a0
WT
1999 case MFF_TUN_GTPU_FLAGS:
2000 match_set_tun_gtpu_flags_masked(match, 0, 0);
2001 break;
2002 case MFF_TUN_GTPU_MSGTYPE:
2003 match_set_tun_gtpu_msgtype_masked(match, 0, 0);
2004 break;
9558d2a5 2005 CASE_MFF_TUN_METADATA:
4f7b100c 2006 tun_metadata_set_match(mf, NULL, NULL, match, err_str);
9558d2a5 2007 break;
4fe3445a 2008
969fc56c 2009 case MFF_METADATA:
81a76618 2010 match_set_metadata_masked(match, htonll(0), htonll(0));
801d2b2a 2011 break;
6a885fd0
BP
2012
2013 case MFF_IN_PORT:
72333065 2014 case MFF_IN_PORT_OXM:
4e022ec0
AW
2015 match->flow.in_port.ofp_port = 0;
2016 match->wc.masks.in_port.ofp_port = 0;
6a885fd0 2017 break;
c61f3870
BP
2018 case MFF_ACTSET_OUTPUT:
2019 match->flow.actset_output = 0;
2020 match->wc.masks.actset_output = 0;
2021 break;
6a885fd0 2022
1b567fb9
AA
2023 case MFF_SKB_PRIORITY:
2024 match->flow.skb_priority = 0;
2025 match->wc.masks.skb_priority = 0;
2026 break;
2027
1362e248
JG
2028 case MFF_PKT_MARK:
2029 match->flow.pkt_mark = 0;
2030 match->wc.masks.pkt_mark = 0;
1b567fb9
AA
2031 break;
2032
07659514
JS
2033 case MFF_CT_STATE:
2034 match->flow.ct_state = 0;
2035 match->wc.masks.ct_state = 0;
2036 break;
2037
2038 case MFF_CT_ZONE:
2039 match->flow.ct_zone = 0;
2040 match->wc.masks.ct_zone = 0;
2041 break;
2042
8e53fe8c
JS
2043 case MFF_CT_MARK:
2044 match->flow.ct_mark = 0;
2045 match->wc.masks.ct_mark = 0;
2046 break;
2047
9daf2348
JS
2048 case MFF_CT_LABEL:
2049 memset(&match->flow.ct_label, 0, sizeof(match->flow.ct_label));
2050 memset(&match->wc.masks.ct_label, 0, sizeof(match->wc.masks.ct_label));
2051 break;
2052
daf4d3c1
JR
2053 case MFF_CT_NW_PROTO:
2054 match->flow.ct_nw_proto = 0;
2055 match->wc.masks.ct_nw_proto = 0;
2056 break;
2057
2058 case MFF_CT_NW_SRC:
2059 match->flow.ct_nw_src = 0;
2060 match->wc.masks.ct_nw_src = 0;
2061 break;
2062
2063 case MFF_CT_NW_DST:
2064 match->flow.ct_nw_dst = 0;
2065 match->wc.masks.ct_nw_dst = 0;
2066 break;
2067
2068 case MFF_CT_IPV6_SRC:
2069 memset(&match->flow.ct_ipv6_src, 0, sizeof(match->flow.ct_ipv6_src));
2070 WC_UNMASK_FIELD(&match->wc, ct_ipv6_src);
2071 break;
2072
2073 case MFF_CT_IPV6_DST:
2074 memset(&match->flow.ct_ipv6_dst, 0, sizeof(match->flow.ct_ipv6_dst));
2075 WC_UNMASK_FIELD(&match->wc, ct_ipv6_dst);
2076 break;
2077
2078 case MFF_CT_TP_SRC:
2079 match->flow.ct_tp_src = 0;
2080 match->wc.masks.ct_tp_src = 0;
2081 break;
2082
2083 case MFF_CT_TP_DST:
2084 match->flow.ct_tp_dst = 0;
2085 match->wc.masks.ct_tp_dst = 0;
2086 break;
2087
0d7e2fe4 2088 CASE_MFF_REGS:
81a76618 2089 match_set_reg_masked(match, mf->id - MFF_REG0, 0, 0);
e9358af6 2090 break;
6a885fd0 2091
79fe0f46
BP
2092 CASE_MFF_XREGS:
2093 match_set_xreg_masked(match, mf->id - MFF_XREG0, 0, 0);
2094 break;
2095
b23ada8e
JP
2096 CASE_MFF_XXREGS: {
2097 match_set_xxreg_masked(match, mf->id - MFF_XXREG0, OVS_U128_ZERO,
2098 OVS_U128_ZERO);
2099 break;
2100 }
2101
6a885fd0 2102 case MFF_ETH_SRC:
74ff3298
JR
2103 match->flow.dl_src = eth_addr_zero;
2104 match->wc.masks.dl_src = eth_addr_zero;
6a885fd0
BP
2105 break;
2106
2107 case MFF_ETH_DST:
74ff3298
JR
2108 match->flow.dl_dst = eth_addr_zero;
2109 match->wc.masks.dl_dst = eth_addr_zero;
6a885fd0
BP
2110 break;
2111
2112 case MFF_ETH_TYPE:
81a76618
BP
2113 match->flow.dl_type = htons(0);
2114 match->wc.masks.dl_type = htons(0);
6a885fd0
BP
2115 break;
2116
2117 case MFF_VLAN_TCI:
81a76618 2118 match_set_dl_tci_masked(match, htons(0), htons(0));
6a885fd0
BP
2119 break;
2120
441c57a9 2121 case MFF_DL_VLAN:
cc34bc8c 2122 case MFF_VLAN_VID:
81a76618 2123 match_set_any_vid(match);
6a885fd0
BP
2124 break;
2125
441c57a9 2126 case MFF_DL_VLAN_PCP:
cc34bc8c 2127 case MFF_VLAN_PCP:
81a76618 2128 match_set_any_pcp(match);
6a885fd0
BP
2129 break;
2130
b02475c5 2131 case MFF_MPLS_LABEL:
8bfd0fda 2132 match_set_any_mpls_label(match, 0);
b02475c5
SH
2133 break;
2134
2135 case MFF_MPLS_TC:
8bfd0fda 2136 match_set_any_mpls_tc(match, 0);
b02475c5
SH
2137 break;
2138
2139 case MFF_MPLS_BOS:
8bfd0fda
BP
2140 match_set_any_mpls_bos(match, 0);
2141 break;
b02475c5 2142
bef3f465
BP
2143 case MFF_MPLS_TTL:
2144 match_set_any_mpls_ttl(match, 0);
2145 break;
2146
6a885fd0
BP
2147 case MFF_IPV4_SRC:
2148 case MFF_ARP_SPA:
81a76618 2149 match_set_nw_src_masked(match, htonl(0), htonl(0));
6a885fd0
BP
2150 break;
2151
2152 case MFF_IPV4_DST:
2153 case MFF_ARP_TPA:
81a76618 2154 match_set_nw_dst_masked(match, htonl(0), htonl(0));
6a885fd0
BP
2155 break;
2156
2157 case MFF_IPV6_SRC:
81a76618
BP
2158 memset(&match->wc.masks.ipv6_src, 0, sizeof match->wc.masks.ipv6_src);
2159 memset(&match->flow.ipv6_src, 0, sizeof match->flow.ipv6_src);
6a885fd0
BP
2160 break;
2161
2162 case MFF_IPV6_DST:
81a76618
BP
2163 memset(&match->wc.masks.ipv6_dst, 0, sizeof match->wc.masks.ipv6_dst);
2164 memset(&match->flow.ipv6_dst, 0, sizeof match->flow.ipv6_dst);
6a885fd0
BP
2165 break;
2166
fa8223b7 2167 case MFF_IPV6_LABEL:
81a76618
BP
2168 match->wc.masks.ipv6_label = htonl(0);
2169 match->flow.ipv6_label = htonl(0);
fa8223b7
JP
2170 break;
2171
6a885fd0 2172 case MFF_IP_PROTO:
81a76618
BP
2173 match->wc.masks.nw_proto = 0;
2174 match->flow.nw_proto = 0;
6a885fd0
BP
2175 break;
2176
530180fd 2177 case MFF_IP_DSCP:
1638b906 2178 case MFF_IP_DSCP_SHIFTED:
81a76618
BP
2179 match->wc.masks.nw_tos &= ~IP_DSCP_MASK;
2180 match->flow.nw_tos &= ~IP_DSCP_MASK;
7257b535
BP
2181 break;
2182
530180fd 2183 case MFF_IP_ECN:
81a76618
BP
2184 match->wc.masks.nw_tos &= ~IP_ECN_MASK;
2185 match->flow.nw_tos &= ~IP_ECN_MASK;
530180fd
JP
2186 break;
2187
a61680c6 2188 case MFF_IP_TTL:
81a76618
BP
2189 match->wc.masks.nw_ttl = 0;
2190 match->flow.nw_ttl = 0;
a61680c6
JP
2191 break;
2192
7257b535 2193 case MFF_IP_FRAG:
6ca49d5e 2194 match->wc.masks.nw_frag &= ~FLOW_NW_FRAG_MASK;
81a76618 2195 match->flow.nw_frag &= ~FLOW_NW_FRAG_MASK;
6a885fd0
BP
2196 break;
2197
2198 case MFF_ARP_OP:
81a76618
BP
2199 match->wc.masks.nw_proto = 0;
2200 match->flow.nw_proto = 0;
6a885fd0
BP
2201 break;
2202
2203 case MFF_ARP_SHA:
2204 case MFF_ND_SLL:
74ff3298
JR
2205 match->flow.arp_sha = eth_addr_zero;
2206 match->wc.masks.arp_sha = eth_addr_zero;
6a885fd0
BP
2207 break;
2208
2209 case MFF_ARP_THA:
2210 case MFF_ND_TLL:
74ff3298
JR
2211 match->flow.arp_tha = eth_addr_zero;
2212 match->wc.masks.arp_tha = eth_addr_zero;
6a885fd0
BP
2213 break;
2214
9b2b8497
VDA
2215 case MFF_ND_RESERVED:
2216 match->wc.masks.igmp_group_ip4 = htonl(0);
2217 match->flow.igmp_group_ip4 = htonl(0);
2218 break;
2219
6a885fd0
BP
2220 case MFF_TCP_SRC:
2221 case MFF_UDP_SRC:
0d56eaf2 2222 case MFF_SCTP_SRC:
268a95e0
BP
2223 case MFF_ICMPV4_TYPE:
2224 case MFF_ICMPV6_TYPE:
81a76618
BP
2225 match->wc.masks.tp_src = htons(0);
2226 match->flow.tp_src = htons(0);
6a885fd0
BP
2227 break;
2228
2229 case MFF_TCP_DST:
2230 case MFF_UDP_DST:
0d56eaf2 2231 case MFF_SCTP_DST:
268a95e0
BP
2232 case MFF_ICMPV4_CODE:
2233 case MFF_ICMPV6_CODE:
81a76618
BP
2234 match->wc.masks.tp_dst = htons(0);
2235 match->flow.tp_dst = htons(0);
6a885fd0
BP
2236 break;
2237
dc235f7f 2238 case MFF_TCP_FLAGS:
9b2b8497 2239 case MFF_ND_OPTIONS_TYPE:
dc235f7f
JR
2240 match->wc.masks.tcp_flags = htons(0);
2241 match->flow.tcp_flags = htons(0);
2242 break;
2243
6a885fd0 2244 case MFF_ND_TARGET:
81a76618
BP
2245 memset(&match->wc.masks.nd_target, 0,
2246 sizeof match->wc.masks.nd_target);
2247 memset(&match->flow.nd_target, 0, sizeof match->flow.nd_target);
6a885fd0
BP
2248 break;
2249
3d2fbd70
JS
2250 case MFF_NSH_FLAGS:
2251 MATCH_SET_FIELD_MASKED(match, nsh.flags, 0, 0);
2252 break;
17553f27
YY
2253 case MFF_NSH_TTL:
2254 MATCH_SET_FIELD_MASKED(match, nsh.ttl, 0, 0);
2255 break;
3d2fbd70
JS
2256 case MFF_NSH_MDTYPE:
2257 MATCH_SET_FIELD_MASKED(match, nsh.mdtype, 0, 0);
2258 break;
2259 case MFF_NSH_NP:
2260 MATCH_SET_FIELD_MASKED(match, nsh.np, 0, 0);
2261 break;
2262 case MFF_NSH_SPI:
17553f27
YY
2263 match->wc.masks.nsh.path_hdr &= ~htonl(NSH_SPI_MASK);
2264 nsh_path_hdr_set_spi(&match->flow.nsh.path_hdr, htonl(0));
3d2fbd70
JS
2265 break;
2266 case MFF_NSH_SI:
17553f27
YY
2267 match->wc.masks.nsh.path_hdr &= ~htonl(NSH_SI_MASK);
2268 nsh_path_hdr_set_si(&match->flow.nsh.path_hdr, 0);
3d2fbd70
JS
2269 break;
2270 case MFF_NSH_C1:
2271 case MFF_NSH_C2:
2272 case MFF_NSH_C3:
2273 case MFF_NSH_C4:
f59cb331 2274 MATCH_SET_FIELD_MASKED(match, nsh.context[mf->id - MFF_NSH_C1],
3d2fbd70
JS
2275 htonl(0), htonl(0));
2276 break;
2277
6a885fd0
BP
2278 case MFF_N_IDS:
2279 default:
428b2edd 2280 OVS_NOT_REACHED();
6a885fd0
BP
2281 }
2282}
2283
81a76618 2284/* Makes 'match' match field 'mf' with the specified 'value' and 'mask'.
6a885fd0
BP
2285 * 'value' specifies a value to match and 'mask' specifies a wildcard pattern,
2286 * with a 1-bit indicating that the corresponding value bit must match and a
2287 * 0-bit indicating a don't-care.
2288 *
2289 * If 'mask' is NULL or points to all-1-bits, then this call is equivalent to
81a76618
BP
2290 * mf_set_value(mf, value, match). If 'mask' points to all-0-bits, then this
2291 * call is equivalent to mf_set_wild(mf, match).
6a885fd0
BP
2292 *
2293 * 'mask' must be a valid mask for 'mf' (see mf_is_mask_valid()). The caller
4f7b100c
JG
2294 * is responsible for ensuring that 'match' meets 'mf''s prerequisites.
2295 *
2296 * If non-NULL, 'err_str' returns a malloc'ed string describing any errors
2297 * with the request or NULL if there is no error. The caller is reponsible
1fd58977
JR
2298 * for freeing the string.
2299 *
2300 * Return a set of enum ofputil_protocol bits (as an uint32_t to avoid circular
2301 * dependency on enum ofputil_protocol definition) indicating which OpenFlow
2302 * protocol versions can support this functionality. */
2303uint32_t
6a885fd0
BP
2304mf_set(const struct mf_field *mf,
2305 const union mf_value *value, const union mf_value *mask,
4f7b100c 2306 struct match *match, char **err_str)
6a885fd0 2307{
53cb9c3e 2308 if (!mask || is_all_ones(mask, mf->n_bytes)) {
4f7b100c 2309 mf_set_value(mf, value, match, err_str);
a4ce8b25 2310 return mf->usable_protocols_exact;
1cb20095
JG
2311 } else if (is_all_zeros(mask, mf->n_bytes) && !mf_is_tun_metadata(mf)) {
2312 /* Tunnel metadata matches on the existence of the field itself, so
2313 * it still needs to be encoded even if the value is wildcarded. */
4f7b100c 2314 mf_set_wild(mf, match, err_str);
db0b6c29 2315 return OFPUTIL_P_ANY;
6a885fd0
BP
2316 }
2317
4f7b100c
JG
2318 if (err_str) {
2319 *err_str = NULL;
2320 }
2321
c90656d4
BP
2322 /* The cases where 'mask' is all-1-bits or all-0-bits were already handled
2323 * above[*], so the code below only needs to work for the remaining cases
2324 * of a nontrivial mask.
2325 *
2326 * [*] Except where the field is a tunnel metadata field and 'mask' is
2327 * all-0-bits; see above. */
6a885fd0 2328 switch (mf->id) {
07659514 2329 case MFF_CT_ZONE:
daf4d3c1 2330 case MFF_CT_NW_PROTO:
a79f29f2 2331 case MFF_RECIRC_ID:
3d4b2e6e 2332 case MFF_PACKET_TYPE:
18080541 2333 case MFF_CONJ_ID:
6a885fd0 2334 case MFF_IN_PORT:
72333065 2335 case MFF_IN_PORT_OXM:
c61f3870 2336 case MFF_ACTSET_OUTPUT:
1b567fb9 2337 case MFF_SKB_PRIORITY:
6a885fd0 2338 case MFF_ETH_TYPE:
441c57a9
BP
2339 case MFF_DL_VLAN:
2340 case MFF_DL_VLAN_PCP:
cc34bc8c 2341 case MFF_VLAN_PCP:
b02475c5
SH
2342 case MFF_MPLS_LABEL:
2343 case MFF_MPLS_TC:
2344 case MFF_MPLS_BOS:
bef3f465 2345 case MFF_MPLS_TTL:
6a885fd0 2346 case MFF_IP_PROTO:
a61680c6 2347 case MFF_IP_TTL:
530180fd 2348 case MFF_IP_DSCP:
1638b906 2349 case MFF_IP_DSCP_SHIFTED:
530180fd 2350 case MFF_IP_ECN:
6a885fd0 2351 case MFF_ARP_OP:
268a95e0
BP
2352 case MFF_ICMPV4_TYPE:
2353 case MFF_ICMPV4_CODE:
2354 case MFF_ICMPV6_TYPE:
2355 case MFF_ICMPV6_CODE:
9b2b8497
VDA
2356 case MFF_ND_RESERVED:
2357 case MFF_ND_OPTIONS_TYPE:
af69bf19 2358 return OFPUTIL_P_NONE;
6a885fd0 2359
a79f29f2
AZ
2360 case MFF_DP_HASH:
2361 match_set_dp_hash_masked(match, ntohl(value->be32), ntohl(mask->be32));
2362 break;
6a885fd0 2363 case MFF_TUN_ID:
81a76618 2364 match_set_tun_id_masked(match, value->be64, mask->be64);
6a885fd0 2365 break;
4fe3445a
PS
2366 case MFF_TUN_SRC:
2367 match_set_tun_src_masked(match, value->be32, mask->be32);
2368 break;
2369 case MFF_TUN_DST:
2370 match_set_tun_dst_masked(match, value->be32, mask->be32);
2371 break;
7dad8e9a
TLSC
2372 case MFF_TUN_IPV6_SRC:
2373 match_set_tun_ipv6_src_masked(match, &value->ipv6, &mask->ipv6);
2374 break;
2375 case MFF_TUN_IPV6_DST:
2376 match_set_tun_ipv6_dst_masked(match, &value->ipv6, &mask->ipv6);
2377 break;
4fe3445a
PS
2378 case MFF_TUN_FLAGS:
2379 match_set_tun_flags_masked(match, ntohs(value->be16), ntohs(mask->be16));
2380 break;
ac6073e3
MC
2381 case MFF_TUN_GBP_ID:
2382 match_set_tun_gbp_id_masked(match, value->be16, mask->be16);
2383 break;
2384 case MFF_TUN_GBP_FLAGS:
2385 match_set_tun_gbp_flags_masked(match, value->u8, mask->u8);
2386 break;
4fe3445a
PS
2387 case MFF_TUN_TTL:
2388 match_set_tun_ttl_masked(match, value->u8, mask->u8);
2389 break;
2390 case MFF_TUN_TOS:
2391 match_set_tun_tos_masked(match, value->u8, mask->u8);
2392 break;
7dc18ae9
WT
2393 case MFF_TUN_ERSPAN_VER:
2394 match_set_tun_erspan_ver_masked(match, value->u8, mask->u8);
2395 break;
2396 case MFF_TUN_ERSPAN_IDX:
2397 match_set_tun_erspan_idx_masked(match, ntohl(value->be32),
2398 ntohl(mask->be32));
2399 break;
2400 case MFF_TUN_ERSPAN_DIR:
2401 match_set_tun_erspan_dir_masked(match, value->u8, mask->u8);
2402 break;
2403 case MFF_TUN_ERSPAN_HWID:
2404 match_set_tun_erspan_hwid_masked(match, value->u8, mask->u8);
2405 break;
3c6d05a0
WT
2406 case MFF_TUN_GTPU_FLAGS:
2407 match_set_tun_gtpu_flags_masked(match, value->u8, mask->u8);
2408 break;
2409 case MFF_TUN_GTPU_MSGTYPE:
2410 match_set_tun_gtpu_msgtype_masked(match, value->u8, mask->u8);
2411 break;
9558d2a5 2412 CASE_MFF_TUN_METADATA:
4f7b100c 2413 tun_metadata_set_match(mf, value, mask, match, err_str);
9558d2a5 2414 break;
4fe3445a 2415
969fc56c 2416 case MFF_METADATA:
81a76618 2417 match_set_metadata_masked(match, value->be64, mask->be64);
969fc56c 2418 break;
6a885fd0 2419
0d7e2fe4 2420 CASE_MFF_REGS:
81a76618
BP
2421 match_set_reg_masked(match, mf->id - MFF_REG0,
2422 ntohl(value->be32), ntohl(mask->be32));
6a885fd0 2423 break;
ac923e91 2424
79fe0f46
BP
2425 CASE_MFF_XREGS:
2426 match_set_xreg_masked(match, mf->id - MFF_XREG0,
2427 ntohll(value->be64), ntohll(mask->be64));
2428 break;
2429
b23ada8e
JP
2430 CASE_MFF_XXREGS: {
2431 match_set_xxreg_masked(match, mf->id - MFF_XXREG0,
2432 ntoh128(value->be128), ntoh128(mask->be128));
2433 break;
2434 }
2435
ac923e91
JG
2436 case MFF_PKT_MARK:
2437 match_set_pkt_mark_masked(match, ntohl(value->be32),
2438 ntohl(mask->be32));
2439 break;
6a885fd0 2440
07659514
JS
2441 case MFF_CT_STATE:
2442 match_set_ct_state_masked(match, ntohl(value->be32), ntohl(mask->be32));
2443 break;
2444
8e53fe8c
JS
2445 case MFF_CT_MARK:
2446 match_set_ct_mark_masked(match, ntohl(value->be32), ntohl(mask->be32));
2447 break;
2448
32ea15f6
JP
2449 case MFF_CT_LABEL:
2450 match_set_ct_label_masked(match, ntoh128(value->be128),
964e01d1 2451 ntoh128(mask->be128));
9daf2348 2452 break;
9daf2348 2453
1740aaf4
YHW
2454 case MFF_CT_NW_SRC:
2455 match_set_ct_nw_src_masked(match, value->be32, mask->be32);
2456 break;
2457
2458 case MFF_CT_NW_DST:
2459 match_set_ct_nw_dst_masked(match, value->be32, mask->be32);
2460 break;
2461
2462 case MFF_CT_IPV6_SRC:
2463 match_set_ct_ipv6_src_masked(match, &value->ipv6, &mask->ipv6);
2464 break;
2465
2466 case MFF_CT_IPV6_DST:
2467 match_set_ct_ipv6_dst_masked(match, &value->ipv6, &mask->ipv6);
2468 break;
2469
2470 case MFF_CT_TP_SRC:
2471 match_set_ct_tp_src_masked(match, value->be16, mask->be16);
2472 break;
2473
2474 case MFF_CT_TP_DST:
2475 match_set_ct_tp_dst_masked(match, value->be16, mask->be16);
2476 break;
2477
6a885fd0 2478 case MFF_ETH_DST:
81a76618 2479 match_set_dl_dst_masked(match, value->mac, mask->mac);
73c0ce34
JS
2480 break;
2481
2482 case MFF_ETH_SRC:
81a76618 2483 match_set_dl_src_masked(match, value->mac, mask->mac);
6a885fd0
BP
2484 break;
2485
e878338b
SH
2486 case MFF_ARP_SHA:
2487 case MFF_ND_SLL:
81a76618 2488 match_set_arp_sha_masked(match, value->mac, mask->mac);
e878338b
SH
2489 break;
2490
2491 case MFF_ARP_THA:
2492 case MFF_ND_TLL:
81a76618 2493 match_set_arp_tha_masked(match, value->mac, mask->mac);
e878338b
SH
2494 break;
2495
6a885fd0 2496 case MFF_VLAN_TCI:
81a76618 2497 match_set_dl_tci_masked(match, value->be16, mask->be16);
6a885fd0
BP
2498 break;
2499
cc34bc8c 2500 case MFF_VLAN_VID:
81a76618 2501 match_set_vlan_vid_masked(match, value->be16, mask->be16);
cc34bc8c
BP
2502 break;
2503
6a885fd0 2504 case MFF_IPV4_SRC:
81a76618 2505 match_set_nw_src_masked(match, value->be32, mask->be32);
a4ce8b25 2506 break;
6a885fd0
BP
2507
2508 case MFF_IPV4_DST:
81a76618 2509 match_set_nw_dst_masked(match, value->be32, mask->be32);
a4ce8b25 2510 break;
6a885fd0
BP
2511
2512 case MFF_IPV6_SRC:
81a76618 2513 match_set_ipv6_src_masked(match, &value->ipv6, &mask->ipv6);
6a885fd0
BP
2514 break;
2515
2516 case MFF_IPV6_DST:
81a76618 2517 match_set_ipv6_dst_masked(match, &value->ipv6, &mask->ipv6);
6a885fd0
BP
2518 break;
2519
32455024
SH
2520 case MFF_IPV6_LABEL:
2521 if ((mask->be32 & htonl(IPV6_LABEL_MASK)) == htonl(IPV6_LABEL_MASK)) {
4f7b100c 2522 mf_set_value(mf, value, match, err_str);
32455024 2523 } else {
81a76618 2524 match_set_ipv6_label_masked(match, value->be32, mask->be32);
32455024
SH
2525 }
2526 break;
2527
47284b1f 2528 case MFF_ND_TARGET:
81a76618 2529 match_set_nd_target_masked(match, &value->ipv6, &mask->ipv6);
47284b1f
AA
2530 break;
2531
7257b535 2532 case MFF_IP_FRAG:
81a76618 2533 match_set_nw_frag_masked(match, value->u8, mask->u8);
7257b535
BP
2534 break;
2535
6a885fd0 2536 case MFF_ARP_SPA:
81a76618 2537 match_set_nw_src_masked(match, value->be32, mask->be32);
a4ce8b25 2538 break;
6a885fd0
BP
2539
2540 case MFF_ARP_TPA:
81a76618 2541 match_set_nw_dst_masked(match, value->be32, mask->be32);
a4ce8b25 2542 break;
6a885fd0 2543
73f33563
BP
2544 case MFF_TCP_SRC:
2545 case MFF_UDP_SRC:
0d56eaf2 2546 case MFF_SCTP_SRC:
81a76618 2547 match_set_tp_src_masked(match, value->be16, mask->be16);
73f33563
BP
2548 break;
2549
2550 case MFF_TCP_DST:
2551 case MFF_UDP_DST:
0d56eaf2 2552 case MFF_SCTP_DST:
81a76618 2553 match_set_tp_dst_masked(match, value->be16, mask->be16);
73f33563
BP
2554 break;
2555
dc235f7f
JR
2556 case MFF_TCP_FLAGS:
2557 match_set_tcp_flags_masked(match, value->be16, mask->be16);
2558 break;
2559
3d2fbd70
JS
2560 case MFF_NSH_FLAGS:
2561 MATCH_SET_FIELD_MASKED(match, nsh.flags, value->u8, mask->u8);
2562 break;
17553f27
YY
2563 case MFF_NSH_TTL:
2564 MATCH_SET_FIELD_MASKED(match, nsh.ttl, value->u8, mask->u8);
2565 break;
3d2fbd70
JS
2566 case MFF_NSH_MDTYPE:
2567 MATCH_SET_FIELD_MASKED(match, nsh.mdtype, value->u8, mask->u8);
2568 break;
2569 case MFF_NSH_NP:
2570 MATCH_SET_FIELD_MASKED(match, nsh.np, value->u8, mask->u8);
2571 break;
2572 case MFF_NSH_SPI:
17553f27
YY
2573 match->wc.masks.nsh.path_hdr |= mask->be32;
2574 nsh_path_hdr_set_spi(&match->flow.nsh.path_hdr,
2575 value->be32 & mask->be32);
3d2fbd70
JS
2576 break;
2577 case MFF_NSH_SI:
17553f27
YY
2578 match->wc.masks.nsh.path_hdr |= htonl(mask->u8);
2579 nsh_path_hdr_set_si(&match->flow.nsh.path_hdr,
2580 value->u8 & mask->u8);
3d2fbd70
JS
2581 break;
2582 case MFF_NSH_C1:
2583 case MFF_NSH_C2:
2584 case MFF_NSH_C3:
2585 case MFF_NSH_C4:
f59cb331 2586 MATCH_SET_FIELD_MASKED(match, nsh.context[mf->id - MFF_NSH_C1],
3d2fbd70
JS
2587 value->be32, mask->be32);
2588 break;
2589
6a885fd0
BP
2590 case MFF_N_IDS:
2591 default:
428b2edd 2592 OVS_NOT_REACHED();
6a885fd0 2593 }
db0b6c29 2594
a4ce8b25
BP
2595 return ((mf->usable_protocols_bitwise == mf->usable_protocols_cidr
2596 || ip_is_cidr(mask->be32))
2597 ? mf->usable_protocols_cidr
2598 : mf->usable_protocols_bitwise);
6a885fd0
BP
2599}
2600
816fd533 2601static enum ofperr
67210a55 2602mf_check__(const struct mf_subfield *sf, const struct match *match,
816fd533 2603 const char *type)
6a885fd0 2604{
816fd533
BP
2605 if (!sf->field) {
2606 VLOG_WARN_RL(&rl, "unknown %s field", type);
9e404d1e 2607 return OFPERR_OFPBAC_BAD_SET_TYPE;
816fd533
BP
2608 } else if (!sf->n_bits) {
2609 VLOG_WARN_RL(&rl, "zero bit %s field %s", type, sf->field->name);
9e404d1e 2610 return OFPERR_OFPBAC_BAD_SET_LEN;
816fd533
BP
2611 } else if (sf->ofs >= sf->field->n_bits) {
2612 VLOG_WARN_RL(&rl, "bit offset %d exceeds %d-bit width of %s field %s",
2613 sf->ofs, sf->field->n_bits, type, sf->field->name);
9e404d1e 2614 return OFPERR_OFPBAC_BAD_SET_LEN;
816fd533
BP
2615 } else if (sf->ofs + sf->n_bits > sf->field->n_bits) {
2616 VLOG_WARN_RL(&rl, "bit offset %d and width %d exceeds %d-bit width "
2617 "of %s field %s", sf->ofs, sf->n_bits,
2618 sf->field->n_bits, type, sf->field->name);
9e404d1e 2619 return OFPERR_OFPBAC_BAD_SET_LEN;
67210a55 2620 } else if (match && !mf_are_match_prereqs_ok(sf->field, match)) {
816fd533
BP
2621 VLOG_WARN_RL(&rl, "%s field %s lacks correct prerequisites",
2622 type, sf->field->name);
9e404d1e 2623 return OFPERR_OFPBAC_MATCH_INCONSISTENT;
6a885fd0 2624 } else {
816fd533
BP
2625 return 0;
2626 }
816fd533 2627}
6a885fd0 2628
67842eba
BP
2629/* Sets all the bits in 'sf' to 1 within 'wc', if 'wc' is nonnull. */
2630static void
2631unwildcard_subfield(const struct mf_subfield *sf, struct flow_wildcards *wc)
2632{
2633 if (wc) {
2634 union mf_value mask;
2635
2636 memset(&mask, 0, sizeof mask);
2637 bitwise_one(&mask, sf->field->n_bytes, sf->ofs, sf->n_bits);
2638 mf_mask_field_masked(sf->field, &mask, wc);
2639 }
2640}
2641
2642/* Copies 'src' into 'dst' within 'flow', and sets all the bits in 'src' and
2643 * 'dst' to 1s in 'wc', if 'wc' is nonnull.
2644 *
2645 * 'src' and 'dst' may overlap. */
2646void
2647mf_subfield_copy(const struct mf_subfield *src,
2648 const struct mf_subfield *dst,
2649 struct flow *flow, struct flow_wildcards *wc)
2650{
2651 ovs_assert(src->n_bits == dst->n_bits);
2652 if (mf_are_prereqs_ok(dst->field, flow, wc)
2653 && mf_are_prereqs_ok(src->field, flow, wc)) {
2654 unwildcard_subfield(src, wc);
2655 unwildcard_subfield(dst, wc);
2656
2657 union mf_value src_value;
2658 union mf_value dst_value;
2659 mf_get_value(dst->field, flow, &dst_value);
2660 mf_get_value(src->field, flow, &src_value);
2661 bitwise_copy(&src_value, src->field->n_bytes, src->ofs,
2662 &dst_value, dst->field->n_bytes, dst->ofs,
2663 src->n_bits);
2664 mf_set_flow_value(dst->field, &dst_value, flow);
2665 }
2666}
2667
2668/* Swaps the bits in 'src' and 'dst' within 'flow', and sets all the bits in
2669 * 'src' and 'dst' to 1s in 'wc', if 'wc' is nonnull.
2670 *
2671 * 'src' and 'dst' may overlap. */
2672void
2673mf_subfield_swap(const struct mf_subfield *a,
2674 const struct mf_subfield *b,
2675 struct flow *flow, struct flow_wildcards *wc)
2676{
2677 ovs_assert(a->n_bits == b->n_bits);
2678 if (mf_are_prereqs_ok(a->field, flow, wc)
2679 && mf_are_prereqs_ok(b->field, flow, wc)) {
2680 unwildcard_subfield(a, wc);
2681 unwildcard_subfield(b, wc);
2682
2683 union mf_value a_value;
2684 union mf_value b_value;
2685 mf_get_value(a->field, flow, &a_value);
2686 mf_get_value(b->field, flow, &b_value);
2687 union mf_value b2_value = b_value;
2688
2689 /* Copy 'a' into 'b'. */
2690 bitwise_copy(&a_value, a->field->n_bytes, a->ofs,
2691 &b_value, b->field->n_bytes, b->ofs,
2692 a->n_bits);
2693 mf_set_flow_value(b->field, &b_value, flow);
2694
2695 /* Copy original 'b' into 'a'. */
2696 bitwise_copy(&b2_value, b->field->n_bytes, b->ofs,
2697 &a_value, a->field->n_bytes, a->ofs,
2698 b->n_bits);
2699 mf_set_flow_value(a->field, &a_value, flow);
2700 }
2701}
2702
816fd533
BP
2703/* Checks whether 'sf' is valid for reading a subfield out of 'flow'. Returns
2704 * 0 if so, otherwise an OpenFlow error code (e.g. as returned by
2705 * ofp_mkerr()). */
2706enum ofperr
67210a55 2707mf_check_src(const struct mf_subfield *sf, const struct match *match)
816fd533 2708{
67210a55 2709 return mf_check__(sf, match, "source");
816fd533 2710}
6a885fd0 2711
816fd533
BP
2712/* Checks whether 'sf' is valid for writing a subfield into 'flow'. Returns 0
2713 * if so, otherwise an OpenFlow error code (e.g. as returned by
2714 * ofp_mkerr()). */
2715enum ofperr
67210a55 2716mf_check_dst(const struct mf_subfield *sf, const struct match *match)
816fd533 2717{
67210a55 2718 int error = mf_check__(sf, match, "destination");
816fd533
BP
2719 if (!error && !sf->field->writable) {
2720 VLOG_WARN_RL(&rl, "destination field %s is not writable",
2721 sf->field->name);
9e404d1e 2722 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
6a885fd0 2723 }
816fd533 2724 return error;
6a885fd0
BP
2725}
2726
81a76618 2727/* Copies the value and wildcard bit pattern for 'mf' from 'match' into the
6a885fd0
BP
2728 * 'value' and 'mask', respectively. */
2729void
81a76618 2730mf_get(const struct mf_field *mf, const struct match *match,
6a885fd0
BP
2731 union mf_value *value, union mf_value *mask)
2732{
81a76618
BP
2733 mf_get_value(mf, &match->flow, value);
2734 mf_get_mask(mf, &match->wc, mask);
6a885fd0
BP
2735}
2736
6a885fd0
BP
2737static char *
2738mf_from_integer_string(const struct mf_field *mf, const char *s,
2739 uint8_t *valuep, uint8_t *maskp)
2740{
6a885fd0 2741 char *tail;
1734bf29
JG
2742 const char *err_str = "";
2743 int err;
6a885fd0 2744
1734bf29
JG
2745 err = parse_int_string(s, valuep, mf->n_bytes, &tail);
2746 if (err || (*tail != '\0' && *tail != '/')) {
2747 err_str = "value";
6a885fd0
BP
2748 goto syntax_error;
2749 }
2750
2751 if (*tail == '/') {
1734bf29
JG
2752 err = parse_int_string(tail + 1, maskp, mf->n_bytes, &tail);
2753 if (err || *tail != '\0') {
2754 err_str = "mask";
6a885fd0
BP
2755 goto syntax_error;
2756 }
2757 } else {
1734bf29 2758 memset(maskp, 0xff, mf->n_bytes);
6a885fd0
BP
2759 }
2760
6a885fd0
BP
2761 return NULL;
2762
2763syntax_error:
1734bf29
JG
2764 if (err == ERANGE) {
2765 return xasprintf("%s: %s too large for %u-byte field %s",
2766 s, err_str, mf->n_bytes, mf->name);
2767 } else {
2768 return xasprintf("%s: bad syntax for %s %s", s, mf->name, err_str);
2769 }
6a885fd0
BP
2770}
2771
3d4b2e6e
JS
2772static char *
2773mf_from_packet_type_string(const char *s, ovs_be32 *packet_type)
2774{
2775 char *tail;
2776 const char *err_str = "";
2777 int err;
2778
2779 if (*s != '(') {
2780 err_str = "missing '('";
2781 goto syntax_error;
2782 }
2783 s++;
2784 err = parse_int_string(s, (uint8_t *)packet_type, 2, &tail);
2785 if (err) {
2786 err_str = "ns";
2787 goto syntax_error;
2788 }
2789 if (*tail != ',') {
2790 err_str = "missing ','";
2791 goto syntax_error;
2792 }
2793 s = tail + 1;
2794 err = parse_int_string(s, ((uint8_t *)packet_type) + 2, 2, &tail);
2795 if (err) {
2796 err_str = "ns_type";
2797 goto syntax_error;
2798 }
2799 if (*tail != ')') {
2800 err_str = "missing ')'";
2801 goto syntax_error;
2802 }
2803
2804 return NULL;
2805
2806syntax_error:
2807 return xasprintf("%s: bad syntax for packet type %s", s, err_str);
2808}
2809
6a885fd0
BP
2810static char *
2811mf_from_ethernet_string(const struct mf_field *mf, const char *s,
74ff3298 2812 struct eth_addr *mac, struct eth_addr *mask)
6a885fd0 2813{
279fb135 2814 int n;
6a885fd0 2815
279fb135 2816 ovs_assert(mf->n_bytes == ETH_ADDR_LEN);
6a885fd0 2817
279fb135 2818 n = -1;
74ff3298 2819 if (ovs_scan(s, ETH_ADDR_SCAN_FMT"%n", ETH_ADDR_SCAN_ARGS(*mac), &n)
279fb135 2820 && n == strlen(s)) {
74ff3298 2821 *mask = eth_addr_exact;
6a885fd0 2822 return NULL;
279fb135 2823 }
6a885fd0 2824
279fb135 2825 n = -1;
c2c28dfd 2826 if (ovs_scan(s, ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT"%n",
74ff3298 2827 ETH_ADDR_SCAN_ARGS(*mac), ETH_ADDR_SCAN_ARGS(*mask), &n)
279fb135
BP
2828 && n == strlen(s)) {
2829 return NULL;
6a885fd0 2830 }
279fb135
BP
2831
2832 return xasprintf("%s: invalid Ethernet address", s);
6a885fd0
BP
2833}
2834
2835static char *
2836mf_from_ipv4_string(const struct mf_field *mf, const char *s,
2837 ovs_be32 *ip, ovs_be32 *mask)
2838{
cb22974d 2839 ovs_assert(mf->n_bytes == sizeof *ip);
61440451 2840 return ip_parse_masked(s, ip, mask);
6a885fd0
BP
2841}
2842
2843static char *
2844mf_from_ipv6_string(const struct mf_field *mf, const char *s,
10f61b6d 2845 struct in6_addr *ipv6, struct in6_addr *mask)
6a885fd0 2846{
10f61b6d
JP
2847 ovs_assert(mf->n_bytes == sizeof *ipv6);
2848 return ipv6_parse_masked(s, ipv6, mask);
6a885fd0
BP
2849}
2850
2851static char *
2852mf_from_ofp_port_string(const struct mf_field *mf, const char *s,
50f96b10 2853 const struct ofputil_port_map *port_map,
6a885fd0
BP
2854 ovs_be16 *valuep, ovs_be16 *maskp)
2855{
4e022ec0 2856 ofp_port_t port;
6a885fd0 2857
cb22974d 2858 ovs_assert(mf->n_bytes == sizeof(ovs_be16));
33ab38d9 2859
50f96b10 2860 if (ofputil_port_from_string(s, port_map, &port)) {
4e022ec0 2861 *valuep = htons(ofp_to_u16(port));
b8266395 2862 *maskp = OVS_BE16_MAX;
6a885fd0 2863 return NULL;
6a885fd0 2864 }
50f96b10 2865 return xasprintf("%s: invalid or unknown port for %s", s, mf->name);
6a885fd0
BP
2866}
2867
72333065
JR
2868static char *
2869mf_from_ofp_port_string32(const struct mf_field *mf, const char *s,
50f96b10 2870 const struct ofputil_port_map *port_map,
72333065
JR
2871 ovs_be32 *valuep, ovs_be32 *maskp)
2872{
4e022ec0 2873 ofp_port_t port;
72333065
JR
2874
2875 ovs_assert(mf->n_bytes == sizeof(ovs_be32));
50f96b10 2876 if (ofputil_port_from_string(s, port_map, &port)) {
72333065 2877 *valuep = ofputil_port_to_ofp11(port);
b8266395 2878 *maskp = OVS_BE32_MAX;
72333065
JR
2879 return NULL;
2880 }
2881 return xasprintf("%s: port value out of range for %s", s, mf->name);
2882}
2883
7257b535
BP
2884struct frag_handling {
2885 const char *name;
2886 uint8_t mask;
2887 uint8_t value;
2888};
2889
2890static const struct frag_handling all_frags[] = {
eadef313
JP
2891#define A FLOW_NW_FRAG_ANY
2892#define L FLOW_NW_FRAG_LATER
7257b535
BP
2893 /* name mask value */
2894
2895 { "no", A|L, 0 },
2896 { "first", A|L, A },
2897 { "later", A|L, A|L },
2898
2899 { "no", A, 0 },
2900 { "yes", A, A },
2901
2902 { "not_later", L, 0 },
2903 { "later", L, L },
2904#undef A
2905#undef L
2906};
2907
2908static char *
2909mf_from_frag_string(const char *s, uint8_t *valuep, uint8_t *maskp)
2910{
2911 const struct frag_handling *h;
2912
2913 for (h = all_frags; h < &all_frags[ARRAY_SIZE(all_frags)]; h++) {
2914 if (!strcasecmp(s, h->name)) {
2915 /* We force the upper bits of the mask on to make mf_parse_value()
2916 * happy (otherwise it will never think it's an exact match.) */
eadef313 2917 *maskp = h->mask | ~FLOW_NW_FRAG_MASK;
7257b535
BP
2918 *valuep = h->value;
2919 return NULL;
2920 }
2921 }
2922
2923 return xasprintf("%s: unknown fragment type (valid types are \"no\", "
cc266ef0 2924 "\"yes\", \"first\", \"later\", \"not_later\"", s);
7257b535
BP
2925}
2926
8e4c1621
JG
2927static char *
2928parse_mf_flags(const char *s, const char *(*bit_to_string)(uint32_t),
2929 const char *field_name, ovs_be16 *flagsp, ovs_be16 allowed,
2930 ovs_be16 *maskp)
4fe3445a 2931{
8e4c1621
JG
2932 int err;
2933 char *err_str;
2934 uint32_t flags, mask;
4fe3445a 2935
8e4c1621
JG
2936 err = parse_flags(s, bit_to_string, '\0', field_name, &err_str,
2937 &flags, ntohs(allowed), maskp ? &mask : NULL);
2938 if (err < 0) {
2939 return err_str;
2940 }
4fe3445a 2941
8e4c1621
JG
2942 *flagsp = htons(flags);
2943 if (maskp) {
2944 *maskp = htons(mask);
4fe3445a
PS
2945 }
2946
8e4c1621 2947 return NULL;
4fe3445a
PS
2948}
2949
2950static char *
8e4c1621 2951mf_from_tcp_flags_string(const char *s, ovs_be16 *flagsp, ovs_be16 *maskp)
4fe3445a 2952{
8e4c1621
JG
2953 return parse_mf_flags(s, packet_tcp_flag_to_string, "TCP", flagsp,
2954 TCP_FLAGS_BE16(OVS_BE16_MAX), maskp);
4fe3445a
PS
2955}
2956
61bf6666 2957static char *
8e4c1621 2958mf_from_tun_flags_string(const char *s, ovs_be16 *flagsp, ovs_be16 *maskp)
61bf6666 2959{
8e4c1621 2960 return parse_mf_flags(s, flow_tun_flag_to_string, "tunnel", flagsp,
b666962b 2961 htons(FLOW_TNL_PUB_F_MASK), maskp);
61bf6666
JR
2962}
2963
07659514
JS
2964static char *
2965mf_from_ct_state_string(const char *s, ovs_be32 *flagsp, ovs_be32 *maskp)
2966{
2967 int err;
2968 char *err_str;
2969 uint32_t flags, mask;
2970
2971 err = parse_flags(s, ct_state_to_string, '\0', "ct_state", &err_str,
2972 &flags, CS_SUPPORTED_MASK, maskp ? &mask : NULL);
2973 if (err < 0) {
2974 return err_str;
2975 }
2976
2977 *flagsp = htonl(flags);
2978 if (maskp) {
2979 *maskp = htonl(mask);
2980 }
2981
2982 return NULL;
2983}
2984
6a885fd0
BP
2985/* Parses 's', a string value for field 'mf', into 'value' and 'mask'. Returns
2986 * NULL if successful, otherwise a malloc()'d string describing the error. */
2987char *
2988mf_parse(const struct mf_field *mf, const char *s,
50f96b10 2989 const struct ofputil_port_map *port_map,
6a885fd0
BP
2990 union mf_value *value, union mf_value *mask)
2991{
3634dff8
BP
2992 char *error;
2993
bedde04c 2994 if (!strcmp(s, "*")) {
6a885fd0
BP
2995 memset(value, 0, mf->n_bytes);
2996 memset(mask, 0, mf->n_bytes);
2997 return NULL;
2998 }
2999
3000 switch (mf->string) {
3001 case MFS_DECIMAL:
3002 case MFS_HEXADECIMAL:
3634dff8
BP
3003 error = mf_from_integer_string(mf, s,
3004 (uint8_t *) value, (uint8_t *) mask);
3005 break;
6a885fd0 3006
07659514
JS
3007 case MFS_CT_STATE:
3008 ovs_assert(mf->n_bytes == sizeof(ovs_be32));
3009 error = mf_from_ct_state_string(s, &value->be32, &mask->be32);
3010 break;
3011
6a885fd0 3012 case MFS_ETHERNET:
74ff3298 3013 error = mf_from_ethernet_string(mf, s, &value->mac, &mask->mac);
3634dff8 3014 break;
6a885fd0
BP
3015
3016 case MFS_IPV4:
3634dff8
BP
3017 error = mf_from_ipv4_string(mf, s, &value->be32, &mask->be32);
3018 break;
6a885fd0
BP
3019
3020 case MFS_IPV6:
3634dff8
BP
3021 error = mf_from_ipv6_string(mf, s, &value->ipv6, &mask->ipv6);
3022 break;
6a885fd0
BP
3023
3024 case MFS_OFP_PORT:
50f96b10
BP
3025 error = mf_from_ofp_port_string(mf, s, port_map,
3026 &value->be16, &mask->be16);
3634dff8 3027 break;
7257b535 3028
72333065 3029 case MFS_OFP_PORT_OXM:
50f96b10
BP
3030 error = mf_from_ofp_port_string32(mf, s, port_map,
3031 &value->be32, &mask->be32);
3634dff8 3032 break;
72333065 3033
7257b535 3034 case MFS_FRAG:
3634dff8
BP
3035 error = mf_from_frag_string(s, &value->u8, &mask->u8);
3036 break;
4fe3445a
PS
3037
3038 case MFS_TNL_FLAGS:
cb22974d 3039 ovs_assert(mf->n_bytes == sizeof(ovs_be16));
3634dff8
BP
3040 error = mf_from_tun_flags_string(s, &value->be16, &mask->be16);
3041 break;
3042
61bf6666
JR
3043 case MFS_TCP_FLAGS:
3044 ovs_assert(mf->n_bytes == sizeof(ovs_be16));
3045 error = mf_from_tcp_flags_string(s, &value->be16, &mask->be16);
3046 break;
3047
3d4b2e6e
JS
3048 case MFS_PACKET_TYPE:
3049 ovs_assert(mf->n_bytes == sizeof(ovs_be32));
3050 error = mf_from_packet_type_string(s, &value->be32);
3051 mask->be32 = OVS_BE32_MAX;
3052 break;
3053
3634dff8 3054 default:
428b2edd 3055 OVS_NOT_REACHED();
6a885fd0 3056 }
3634dff8
BP
3057
3058 if (!error && !mf_is_mask_valid(mf, mask)) {
3059 error = xasprintf("%s: invalid mask for field %s", s, mf->name);
3060 }
3061 return error;
6a885fd0
BP
3062}
3063
3064/* Parses 's', a string value for field 'mf', into 'value'. Returns NULL if
3065 * successful, otherwise a malloc()'d string describing the error. */
3066char *
50f96b10
BP
3067mf_parse_value(const struct mf_field *mf, const char *s,
3068 const struct ofputil_port_map *port_map, union mf_value *value)
6a885fd0
BP
3069{
3070 union mf_value mask;
3071 char *error;
3072
50f96b10 3073 error = mf_parse(mf, s, port_map, value, &mask);
6a885fd0
BP
3074 if (error) {
3075 return error;
3076 }
3077
3078 if (!is_all_ones((const uint8_t *) &mask, mf->n_bytes)) {
3079 return xasprintf("%s: wildcards not allowed here", s);
3080 }
3081 return NULL;
3082}
3083
3084static void
3085mf_format_integer_string(const struct mf_field *mf, const uint8_t *valuep,
3086 const uint8_t *maskp, struct ds *s)
3087{
6a885fd0 3088 if (mf->string == MFS_HEXADECIMAL) {
1734bf29 3089 ds_put_hex(s, valuep, mf->n_bytes);
6a885fd0 3090 } else {
1734bf29
JG
3091 unsigned long long int integer = 0;
3092 int i;
6a885fd0 3093
1734bf29 3094 ovs_assert(mf->n_bytes <= 8);
6a885fd0 3095 for (i = 0; i < mf->n_bytes; i++) {
1734bf29 3096 integer = (integer << 8) | valuep[i];
6a885fd0 3097 }
1734bf29
JG
3098 ds_put_format(s, "%lld", integer);
3099 }
6a885fd0 3100
1734bf29 3101 if (maskp) {
6a885fd0
BP
3102 /* I guess we could write the mask in decimal for MFS_DECIMAL but I'm
3103 * not sure that that a bit-mask written in decimal is ever easier to
3104 * understand than the same bit-mask written in hexadecimal. */
1734bf29
JG
3105 ds_put_char(s, '/');
3106 ds_put_hex(s, maskp, mf->n_bytes);
6a885fd0
BP
3107 }
3108}
3109
7257b535 3110static void
8f75dea3 3111mf_format_frag_string(uint8_t value, uint8_t mask, struct ds *s)
7257b535
BP
3112{
3113 const struct frag_handling *h;
7257b535 3114
eadef313 3115 mask &= FLOW_NW_FRAG_MASK;
8f75dea3 3116 value &= mask;
7257b535
BP
3117
3118 for (h = all_frags; h < &all_frags[ARRAY_SIZE(all_frags)]; h++) {
3119 if (value == h->value && mask == h->mask) {
3120 ds_put_cstr(s, h->name);
3121 return;
3122 }
3123 }
3124 ds_put_cstr(s, "<error>");
3125}
3126
4fe3445a 3127static void
b666962b 3128mf_format_tnl_flags_string(ovs_be16 value, ovs_be16 mask, struct ds *s)
4fe3445a 3129{
b666962b
JG
3130 format_flags_masked(s, NULL, flow_tun_flag_to_string, ntohs(value),
3131 ntohs(mask) & FLOW_TNL_PUB_F_MASK, FLOW_TNL_PUB_F_MASK);
4fe3445a
PS
3132}
3133
61bf6666
JR
3134static void
3135mf_format_tcp_flags_string(ovs_be16 value, ovs_be16 mask, struct ds *s)
3136{
3137 format_flags_masked(s, NULL, packet_tcp_flag_to_string, ntohs(value),
8e4c1621 3138 TCP_FLAGS(mask), TCP_FLAGS(OVS_BE16_MAX));
61bf6666
JR
3139}
3140
07659514
JS
3141static void
3142mf_format_ct_state_string(ovs_be32 value, ovs_be32 mask, struct ds *s)
3143{
3144 format_flags_masked(s, NULL, ct_state_to_string, ntohl(value),
3145 ntohl(mask), UINT16_MAX);
3146}
3147
3d4b2e6e
JS
3148static void
3149mf_format_packet_type_string(ovs_be32 value, ovs_be32 mask, struct ds *s)
3150{
3151 format_packet_type_masked(s, value, mask);
3152}
3153
6a885fd0
BP
3154/* Appends to 's' a string representation of field 'mf' whose value is in
3155 * 'value' and 'mask'. 'mask' may be NULL to indicate an exact match. */
3156void
3157mf_format(const struct mf_field *mf,
3158 const union mf_value *value, const union mf_value *mask,
50f96b10 3159 const struct ofputil_port_map *port_map,
6a885fd0
BP
3160 struct ds *s)
3161{
3162 if (mask) {
53cb9c3e 3163 if (is_all_zeros(mask, mf->n_bytes)) {
6a885fd0
BP
3164 ds_put_cstr(s, "ANY");
3165 return;
53cb9c3e 3166 } else if (is_all_ones(mask, mf->n_bytes)) {
6a885fd0
BP
3167 mask = NULL;
3168 }
3169 }
3170
3171 switch (mf->string) {
72333065
JR
3172 case MFS_OFP_PORT_OXM:
3173 if (!mask) {
4e022ec0 3174 ofp_port_t port;
bc146369 3175 ofputil_port_from_ofp11(value->be32, &port);
50f96b10 3176 ofputil_format_port(port, port_map, s);
72333065
JR
3177 break;
3178 }
3179 /* fall through */
6a885fd0
BP
3180 case MFS_OFP_PORT:
3181 if (!mask) {
50f96b10 3182 ofputil_format_port(u16_to_ofp(ntohs(value->be16)), port_map, s);
6a885fd0
BP
3183 break;
3184 }
3185 /* fall through */
3186 case MFS_DECIMAL:
3187 case MFS_HEXADECIMAL:
3188 mf_format_integer_string(mf, (uint8_t *) value, (uint8_t *) mask, s);
3189 break;
3190
07659514
JS
3191 case MFS_CT_STATE:
3192 mf_format_ct_state_string(value->be32,
3193 mask ? mask->be32 : OVS_BE32_MAX, s);
3194 break;
3195
6a885fd0 3196 case MFS_ETHERNET:
74ff3298 3197 eth_format_masked(value->mac, mask ? &mask->mac : NULL, s);
6a885fd0
BP
3198 break;
3199
3200 case MFS_IPV4:
b8266395 3201 ip_format_masked(value->be32, mask ? mask->be32 : OVS_BE32_MAX, s);
6a885fd0
BP
3202 break;
3203
3204 case MFS_IPV6:
ac6d120f 3205 ipv6_format_masked(&value->ipv6, mask ? &mask->ipv6 : NULL, s);
6a885fd0
BP
3206 break;
3207
7257b535 3208 case MFS_FRAG:
8f75dea3 3209 mf_format_frag_string(value->u8, mask ? mask->u8 : UINT8_MAX, s);
7257b535
BP
3210 break;
3211
4fe3445a 3212 case MFS_TNL_FLAGS:
b666962b
JG
3213 mf_format_tnl_flags_string(value->be16,
3214 mask ? mask->be16 : OVS_BE16_MAX, s);
4fe3445a
PS
3215 break;
3216
61bf6666
JR
3217 case MFS_TCP_FLAGS:
3218 mf_format_tcp_flags_string(value->be16,
3219 mask ? mask->be16 : OVS_BE16_MAX, s);
3220 break;
3221
3d4b2e6e
JS
3222 case MFS_PACKET_TYPE:
3223 mf_format_packet_type_string(value->be32,
3224 mask ? mask->be32 : OVS_BE32_MAX, s);
3225 break;
3226
6a885fd0 3227 default:
428b2edd 3228 OVS_NOT_REACHED();
6a885fd0
BP
3229 }
3230}
816fd533 3231\f
9bab681f
IY
3232/* Makes subfield 'sf' within 'flow' exactly match the 'sf->n_bits'
3233 * least-significant bits in 'x'.
3234 */
3235void
3236mf_write_subfield_flow(const struct mf_subfield *sf,
3237 const union mf_subvalue *x, struct flow *flow)
3238{
3239 const struct mf_field *field = sf->field;
3240 union mf_value value;
3241
3242 mf_get_value(field, flow, &value);
158edc8d 3243 bitwise_copy(x, sizeof *x, 0, &value, field->n_bytes,
9bab681f
IY
3244 sf->ofs, sf->n_bits);
3245 mf_set_flow_value(field, &value, flow);
3246}
3247
81a76618 3248/* Makes subfield 'sf' within 'match' exactly match the 'sf->n_bits'
1b35df45 3249 * least-significant bits in 'x'.
615660a9 3250 */
1b35df45
BP
3251void
3252mf_write_subfield(const struct mf_subfield *sf, const union mf_subvalue *x,
81a76618 3253 struct match *match)
1b35df45
BP
3254{
3255 const struct mf_field *field = sf->field;
3256 union mf_value value, mask;
3257
81a76618 3258 mf_get(field, match, &value, &mask);
1b35df45
BP
3259 bitwise_copy(x, sizeof *x, 0, &value, field->n_bytes, sf->ofs, sf->n_bits);
3260 bitwise_one ( &mask, field->n_bytes, sf->ofs, sf->n_bits);
4f7b100c 3261 mf_set(field, &value, &mask, match, NULL);
1b35df45
BP
3262}
3263
dfe191d5
JR
3264void
3265mf_write_subfield_value(const struct mf_subfield *sf, const void *src,
3266 struct match *match)
3267{
3268 const struct mf_field *field = sf->field;
3269 union mf_value value, mask;
3270 unsigned int size = DIV_ROUND_UP(sf->n_bits, 8);
3271
3272 mf_get(field, match, &value, &mask);
3273 bitwise_copy(src, size, 0, &value, field->n_bytes, sf->ofs, sf->n_bits);
3274 bitwise_one ( &mask, field->n_bytes, sf->ofs, sf->n_bits);
3275 mf_set(field, &value, &mask, match, NULL);
3276}
3277
35c1f798
BP
3278/* 'v' and 'm' correspond to values of 'field'. This function copies them into
3279 * 'match' in the correspond positions. */
3280void
3281mf_mask_subfield(const struct mf_field *field,
3282 const union mf_subvalue *v,
3283 const union mf_subvalue *m,
3284 struct match *match)
3285{
3286 union mf_value value, mask;
3287
3288 mf_get(field, match, &value, &mask);
3289 bitwise_copy(v, sizeof *v, 0, &value, field->n_bytes, 0, field->n_bits);
3290 bitwise_copy(m, sizeof *m, 0, &mask, field->n_bytes, 0, field->n_bits);
4f7b100c 3291 mf_set(field, &value, &mask, match, NULL);
35c1f798
BP
3292}
3293
1b35df45
BP
3294/* Initializes 'x' to the value of 'sf' within 'flow'. 'sf' must be valid for
3295 * reading 'flow', e.g. as checked by mf_check_src(). */
3296void
3297mf_read_subfield(const struct mf_subfield *sf, const struct flow *flow,
3298 union mf_subvalue *x)
3299{
3300 union mf_value value;
3301
3302 mf_get_value(sf->field, flow, &value);
3303
3304 memset(x, 0, sizeof *x);
3305 bitwise_copy(&value, sf->field->n_bytes, sf->ofs,
3306 x, sizeof *x, 0,
3307 sf->n_bits);
3308}
3309
816fd533
BP
3310/* Returns the value of 'sf' within 'flow'. 'sf' must be valid for reading
3311 * 'flow', e.g. as checked by mf_check_src() and sf->n_bits must be 64 or
3312 * less. */
3313uint64_t
3314mf_get_subfield(const struct mf_subfield *sf, const struct flow *flow)
3315{
3316 union mf_value value;
3317
3318 mf_get_value(sf->field, flow, &value);
3319 return bitwise_get(&value, sf->field->n_bytes, sf->ofs, sf->n_bits);
3320}
3321
9bab681f
IY
3322void
3323mf_format_subvalue(const union mf_subvalue *subvalue, struct ds *s)
3324{
e7ae59f9 3325 ds_put_hex(s, subvalue->u8, sizeof subvalue->u8);
9bab681f 3326}
bc65c25a
SH
3327
3328void
3329field_array_set(enum mf_field_id id, const union mf_value *value,
3330 struct field_array *fa)
3331{
e8dba719
JR
3332 size_t i, offset = 0;
3333
bc65c25a 3334 ovs_assert(id < MFF_N_IDS);
e8dba719
JR
3335
3336 /* Find the spot for 'id'. */
3337 BITMAP_FOR_EACH_1 (i, id, fa->used.bm) {
3338 offset += mf_from_id(i)->n_bytes;
3339 }
3340
3341 size_t value_size = mf_from_id(id)->n_bytes;
3342
3343 /* make room if necessary. */
3344 if (!bitmap_is_set(fa->used.bm, id)) {
3345 fa->values = xrealloc(fa->values, fa->values_size + value_size);
3346 /* Move remainder forward, if any. */
3347 if (offset < fa->values_size) {
3348 memmove(fa->values + offset + value_size, fa->values + offset,
3349 fa->values_size - offset);
3350 }
3351 fa->values_size += value_size;
3352 }
bc65c25a 3353 bitmap_set1(fa->used.bm, id);
e8dba719
JR
3354
3355 memcpy(fa->values + offset, value, value_size);
bc65c25a 3356}
04f48a68 3357
aafee638
YHW
3358/* A wrapper for variable length mf_fields that is maintained by
3359 * struct vl_mff_map.*/
3360struct vl_mf_field {
3361 struct mf_field mf;
5c7c16d8 3362 struct ovs_refcount ref_cnt;
aafee638
YHW
3363 struct cmap_node cmap_node; /* In ofproto->vl_mff_map->cmap. */
3364};
3365
04f48a68
YHW
3366static inline uint32_t
3367mf_field_hash(uint32_t key)
3368{
3369 return hash_int(key, 0);
3370}
3371
5c7c16d8
YHW
3372static void
3373vmf_delete(struct vl_mf_field *vmf)
3374{
3375 if (ovs_refcount_unref(&vmf->ref_cnt) == 1) {
3376 /* Postpone as this function is typically called immediately
3377 * after removing from cmap. */
3378 ovsrcu_postpone(free, vmf);
3379 } else {
3380 VLOG_WARN_RL(&rl,
3381 "Attempted to delete VMF %s but refcount is nonzero!",
3382 vmf->mf.name);
3383 }
3384}
3385
3386enum ofperr
3387mf_vl_mff_map_clear(struct vl_mff_map *vl_mff_map, bool force)
04f48a68
YHW
3388 OVS_REQUIRES(vl_mff_map->mutex)
3389{
aafee638 3390 struct vl_mf_field *vmf;
04f48a68 3391
5c7c16d8
YHW
3392 if (!force) {
3393 CMAP_FOR_EACH (vmf, cmap_node, &vl_mff_map->cmap) {
3394 if (ovs_refcount_read(&vmf->ref_cnt) != 1) {
3395 return OFPERR_NXTTMFC_INVALID_TLV_DEL;
3396 }
3397 }
3398 }
3399
aafee638
YHW
3400 CMAP_FOR_EACH (vmf, cmap_node, &vl_mff_map->cmap) {
3401 cmap_remove(&vl_mff_map->cmap, &vmf->cmap_node,
3402 mf_field_hash(vmf->mf.id));
5c7c16d8 3403 vmf_delete(vmf);
04f48a68 3404 }
5c7c16d8
YHW
3405
3406 return 0;
04f48a68
YHW
3407}
3408
aafee638 3409static struct vl_mf_field *
04f48a68
YHW
3410mf_get_vl_mff__(uint32_t id, const struct vl_mff_map *vl_mff_map)
3411{
aafee638 3412 struct vl_mf_field *vmf;
04f48a68 3413
aafee638 3414 CMAP_FOR_EACH_WITH_HASH (vmf, cmap_node, mf_field_hash(id),
04f48a68 3415 &vl_mff_map->cmap) {
aafee638
YHW
3416 if (vmf->mf.id == id) {
3417 return vmf;
04f48a68
YHW
3418 }
3419 }
3420
3421 return NULL;
3422}
3423
3424/* If 'mff' is a variable length field, looks up 'vl_mff_map', returns a
3425 * pointer to the variable length meta-flow field corresponding to 'mff'.
3426 * Returns NULL if no mapping is existed for 'mff'. */
3427const struct mf_field *
3428mf_get_vl_mff(const struct mf_field *mff,
3429 const struct vl_mff_map *vl_mff_map)
3430{
3431 if (mff && mff->variable_len && vl_mff_map) {
aafee638 3432 return &mf_get_vl_mff__(mff->id, vl_mff_map)->mf;
04f48a68
YHW
3433 }
3434
3435 return NULL;
3436}
3437
5c7c16d8
YHW
3438static enum ofperr
3439mf_vl_mff_map_del(struct vl_mff_map *vl_mff_map,
3440 const struct ofputil_tlv_table_mod *ttm, bool force)
04f48a68
YHW
3441 OVS_REQUIRES(vl_mff_map->mutex)
3442{
3443 struct ofputil_tlv_map *tlv_map;
5c7c16d8
YHW
3444 struct vl_mf_field *vmf;
3445 unsigned int idx;
04f48a68 3446
5c7c16d8
YHW
3447 if (!force) {
3448 LIST_FOR_EACH (tlv_map, list_node, &ttm->mappings) {
3449 idx = MFF_TUN_METADATA0 + tlv_map->index;
3450 if (idx >= MFF_TUN_METADATA0 + TUN_METADATA_NUM_OPTS) {
3451 return OFPERR_NXTTMFC_BAD_FIELD_IDX;
3452 }
3453
3454 vmf = mf_get_vl_mff__(idx, vl_mff_map);
3455 if (vmf && ovs_refcount_read(&vmf->ref_cnt) != 1) {
3456 return OFPERR_NXTTMFC_INVALID_TLV_DEL;
3457 }
3458 }
04f48a68
YHW
3459 }
3460
3461 LIST_FOR_EACH (tlv_map, list_node, &ttm->mappings) {
5c7c16d8 3462 idx = MFF_TUN_METADATA0 + tlv_map->index;
04f48a68
YHW
3463 if (idx >= MFF_TUN_METADATA0 + TUN_METADATA_NUM_OPTS) {
3464 return OFPERR_NXTTMFC_BAD_FIELD_IDX;
3465 }
3466
5c7c16d8
YHW
3467 vmf = mf_get_vl_mff__(idx, vl_mff_map);
3468 if (vmf) {
3469 cmap_remove(&vl_mff_map->cmap, &vmf->cmap_node,
aafee638 3470 mf_field_hash(idx));
5c7c16d8
YHW
3471 vmf_delete(vmf);
3472 }
3473 }
04f48a68 3474
5c7c16d8
YHW
3475 return 0;
3476}
04f48a68 3477
5c7c16d8
YHW
3478static enum ofperr
3479mf_vl_mff_map_add(struct vl_mff_map *vl_mff_map,
3480 const struct ofputil_tlv_table_mod *ttm)
3481 OVS_REQUIRES(vl_mff_map->mutex)
3482{
3483 struct ofputil_tlv_map *tlv_map;
3484 struct vl_mf_field *vmf;
3485 unsigned int idx;
3486
3487 LIST_FOR_EACH (tlv_map, list_node, &ttm->mappings) {
3488 idx = MFF_TUN_METADATA0 + tlv_map->index;
3489 if (idx >= MFF_TUN_METADATA0 + TUN_METADATA_NUM_OPTS) {
3490 return OFPERR_NXTTMFC_BAD_FIELD_IDX;
04f48a68 3491 }
5c7c16d8
YHW
3492
3493 vmf = xmalloc(sizeof *vmf);
3494 vmf->mf = mf_fields[idx];
3495 vmf->mf.n_bytes = tlv_map->option_len;
3496 vmf->mf.n_bits = tlv_map->option_len * 8;
3497 vmf->mf.mapped = true;
3498 ovs_refcount_init(&vmf->ref_cnt);
3499
3500 cmap_insert(&vl_mff_map->cmap, &vmf->cmap_node,
3501 mf_field_hash(idx));
3502 }
3503
3504 return 0;
3505}
3506
3507/* Updates the tun_metadata mf_field in 'vl_mff_map' according to 'ttm'.
3508 * This function must be invoked after tun_metadata_table_mod().
3509 * Returns OFPERR_NXTTMFC_BAD_FIELD_IDX, if the index for the vl_mf_field is
3510 * invalid.
3511 * Returns OFPERR_NXTTMFC_INVALID_TLV_DEL, if 'ttm' tries to delete an
3512 * vl_mf_field that is still used by any active flow.*/
3513enum ofperr
3514mf_vl_mff_map_mod_from_tun_metadata(struct vl_mff_map *vl_mff_map,
3515 const struct ofputil_tlv_table_mod *ttm)
3516 OVS_REQUIRES(vl_mff_map->mutex)
3517{
3518 switch (ttm->command) {
3519 case NXTTMC_ADD:
3520 return mf_vl_mff_map_add(vl_mff_map, ttm);
3521
3522 case NXTTMC_DELETE:
3523 return mf_vl_mff_map_del(vl_mff_map, ttm, false);
3524
3525 case NXTTMC_CLEAR:
3526 return mf_vl_mff_map_clear(vl_mff_map, false);
3527
3528 default:
3529 OVS_NOT_REACHED();
04f48a68
YHW
3530 }
3531
3532 return 0;
3533}
3534
3535/* Returns true if a variable length meta-flow field 'mff' is not mapped in
3536 * the 'vl_mff_map'. */
3537bool
3538mf_vl_mff_invalid(const struct mf_field *mff, const struct vl_mff_map *map)
3539{
3540 return map && mff && mff->variable_len && !mff->mapped;
3541}
5c7c16d8
YHW
3542
3543void
3544mf_vl_mff_set_tlv_bitmap(const struct mf_field *mff, uint64_t *tlv_bitmap)
3545{
3546 if (mff && mff->mapped) {
3547 ovs_assert(mf_is_tun_metadata(mff));
3548 ULLONG_SET1(*tlv_bitmap, mff->id - MFF_TUN_METADATA0);
3549 }
3550}
3551
3552static void
3553mf_vl_mff_ref_cnt_mod(const struct vl_mff_map *map, uint64_t tlv_bitmap,
3554 bool ref)
3555{
3556 struct vl_mf_field *vmf;
3557 int i;
3558
3559 if (map) {
3560 ULLONG_FOR_EACH_1 (i, tlv_bitmap) {
3561 vmf = mf_get_vl_mff__(i + MFF_TUN_METADATA0, map);
3562 if (vmf) {
3563 if (ref) {
3564 ovs_refcount_ref(&vmf->ref_cnt);
3565 } else {
3566 ovs_refcount_unref(&vmf->ref_cnt);
3567 }
3568 } else {
3569 VLOG_WARN("Invalid TLV index %d.", i);
3570 }
3571 }
3572 }
3573}
3574
3575void
3576mf_vl_mff_ref(const struct vl_mff_map *map, uint64_t tlv_bitmap)
3577{
3578 mf_vl_mff_ref_cnt_mod(map, tlv_bitmap, true);
3579}
3580
3581void
3582mf_vl_mff_unref(const struct vl_mff_map *map, uint64_t tlv_bitmap)
3583{
3584 mf_vl_mff_ref_cnt_mod(map, tlv_bitmap, false);
3585}
3586
3587enum ofperr
3588mf_vl_mff_nx_pull_header(struct ofpbuf *b, const struct vl_mff_map *vl_mff_map,
3589 const struct mf_field **field, bool *masked,
3590 uint64_t *tlv_bitmap)
3591{
3592 enum ofperr error = nx_pull_header(b, vl_mff_map, field, masked);
3593 if (error) {
3594 return error;
3595 }
3596
3597 mf_vl_mff_set_tlv_bitmap(*field, tlv_bitmap);
3598 return 0;
3599}
3600
3601enum ofperr
3602mf_vl_mff_nx_pull_entry(struct ofpbuf *b, const struct vl_mff_map *vl_mff_map,
3603 const struct mf_field **field, union mf_value *value,
3604 union mf_value *mask, uint64_t *tlv_bitmap)
3605{
731dbbbe 3606 enum ofperr error = nx_pull_entry(b, vl_mff_map, field, value, mask, true);
5c7c16d8
YHW
3607 if (error) {
3608 return error;
3609 }
3610
3611 mf_vl_mff_set_tlv_bitmap(*field, tlv_bitmap);
3612 return 0;
3613}
3614
3615enum ofperr
3616mf_vl_mff_mf_from_nxm_header(uint32_t header,
3617 const struct vl_mff_map *vl_mff_map,
3618 const struct mf_field **field,
3619 uint64_t *tlv_bitmap)
3620{
3621 *field = mf_from_nxm_header(header, vl_mff_map);
c5358a96
BP
3622 if (!*field) {
3623 return OFPERR_OFPBAC_BAD_SET_TYPE;
3624 } else if (mf_vl_mff_invalid(*field, vl_mff_map)) {
5c7c16d8
YHW
3625 return OFPERR_NXFMFC_INVALID_TLV_FIELD;
3626 }
3627
3628 mf_vl_mff_set_tlv_bitmap(*field, tlv_bitmap);
3629 return 0;
3630}
4e413ac8
BP
3631\f
3632/* Returns true if the 1-bits in 'super' are a superset of the 1-bits in 'sub',
3633 * false otherwise. */
3634bool
3635mf_bitmap_is_superset(const struct mf_bitmap *super,
3636 const struct mf_bitmap *sub)
3637{
3638 return bitmap_is_superset(super->bm, sub->bm, MFF_N_IDS);
3639}
95a5454c
BP
3640
3641/* Returns the bitwise-and of 'a' and 'b'. */
3642struct mf_bitmap
3643mf_bitmap_and(struct mf_bitmap a, struct mf_bitmap b)
3644{
3645 bitmap_and(a.bm, b.bm, MFF_N_IDS);
3646 return a;
3647}
3648
3649/* Returns the bitwise-or of 'a' and 'b'. */
3650struct mf_bitmap
3651mf_bitmap_or(struct mf_bitmap a, struct mf_bitmap b)
3652{
3653 bitmap_or(a.bm, b.bm, MFF_N_IDS);
3654 return a;
3655}
3656
3657/* Returns the bitwise-not of 'x'. */
3658struct mf_bitmap
3659mf_bitmap_not(struct mf_bitmap x)
3660{
3661 bitmap_not(x.bm, MFF_N_IDS);
3662 return x;
3663}