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