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