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