2 * Copyright (c) 2011-2017 Nicira, Inc.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include "openvswitch/meta-flow.h"
23 #include <netinet/icmp6.h>
24 #include <netinet/ip6.h>
26 #include "classifier.h"
27 #include "openvswitch/dynamic-string.h"
29 #include "ovs-atomic.h"
31 #include "ovs-thread.h"
34 #include "openvswitch/shash.h"
35 #include "socket-util.h"
36 #include "tun-metadata.h"
37 #include "unaligned.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"
46 VLOG_DEFINE_THIS_MODULE(meta_flow
);
48 #define FLOW_U32OFS(FIELD) \
49 offsetof(struct flow, FIELD) % 4 ? -1 : offsetof(struct flow, FIELD) / 4
51 #define MF_FIELD_SIZES(MEMBER) \
52 sizeof ((union mf_value *)0)->MEMBER, \
53 8 * sizeof ((union mf_value *)0)->MEMBER
55 extern const struct mf_field mf_fields
[MFF_N_IDS
]; /* Silence a warning. */
57 const struct mf_field mf_fields
[MFF_N_IDS
] = {
58 #include "meta-flow.inc"
61 /* Maps from an mf_field's 'name' or 'extra_name' to the mf_field. */
62 static struct shash mf_by_name
;
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);
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 } }
75 const union mf_value exact_match_mask
= MF_VALUE_EXACT_INITIALIZER
;
77 static void nxm_init(void);
79 /* Returns the field with the given 'name', or a null pointer if no field has
81 const struct mf_field
*
82 mf_from_name(const char *name
)
85 return shash_find_data(&mf_by_name
, name
);
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
)
95 struct shash_node
*node
= shash_find_len(&mf_by_name
, name
, len
);
96 return node
? node
->data
: NULL
;
104 shash_init(&mf_by_name
);
105 for (i
= 0; i
< MFF_N_IDS
; i
++) {
106 const struct mf_field
*mf
= &mf_fields
[i
];
108 ovs_assert(mf
->id
== i
); /* Fields must be in the enum order. */
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
);
120 static pthread_once_t once
= PTHREAD_ONCE_INIT
;
121 pthread_once(&once
, nxm_do_init
);
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),
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.) */
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
)
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
];
151 if ((av
^ bv
) & (am
& bm
)) {
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. */
163 mf_subvalue_width(const union mf_subvalue
*v
)
165 return 1 + bitwise_rscan(v
, sizeof *v
, true, sizeof *v
* 8 - 1, -1);
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. */
171 mf_subvalue_shift(union mf_subvalue
*value
, int n
)
174 union mf_subvalue tmp
;
175 memset(&tmp
, 0, sizeof tmp
);
177 if (n
> 0 && n
< 8 * sizeof tmp
) {
178 bitwise_copy(value
, sizeof *value
, 0,
181 } else if (n
< 0 && n
> -8 * sizeof tmp
) {
182 bitwise_copy(value
, sizeof *value
, -n
,
190 /* Appends a formatted representation of 'sv' to 's'. */
192 mf_subvalue_format(const union mf_subvalue
*sv
, struct ds
*s
)
194 ds_put_hex(s
, sv
, sizeof *sv
);
197 /* Returns true if 'wc' wildcards all the bits in field 'mf', false if 'wc'
198 * specifies at least one bit in the field.
200 * The caller is responsible for ensuring that 'wc' corresponds to a flow that
201 * meets 'mf''s prerequisites. */
203 mf_is_all_wild(const struct mf_field
*mf
, const struct flow_wildcards
*wc
)
207 return !wc
->masks
.dp_hash
;
209 return !wc
->masks
.recirc_id
;
210 case MFF_PACKET_TYPE
:
211 return !wc
->masks
.packet_type
;
213 return !wc
->masks
.conj_id
;
215 return !wc
->masks
.tunnel
.ip_src
;
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
);
223 return !wc
->masks
.tunnel
.tun_id
;
225 return !wc
->masks
.tunnel
.ip_tos
;
227 return !wc
->masks
.tunnel
.ip_ttl
;
229 return !(wc
->masks
.tunnel
.flags
& FLOW_TNL_PUB_F_MASK
);
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
);
246 return !wc
->masks
.metadata
;
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
;
253 return !wc
->masks
.pkt_mark
;
255 return !wc
->masks
.ct_state
;
257 return !wc
->masks
.ct_zone
;
259 return !wc
->masks
.ct_mark
;
261 return ovs_u128_is_zero(wc
->masks
.ct_label
);
262 case MFF_CT_NW_PROTO
:
263 return !wc
->masks
.ct_nw_proto
;
265 return !wc
->masks
.ct_nw_src
;
267 return !wc
->masks
.ct_nw_dst
;
269 return !wc
->masks
.ct_tp_src
;
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
);
277 return !wc
->masks
.regs
[mf
->id
- MFF_REG0
];
279 return !flow_get_xreg(&wc
->masks
, mf
->id
- MFF_XREG0
);
281 ovs_u128 value
= flow_get_xxreg(&wc
->masks
, mf
->id
- MFF_XXREG0
);
282 return ovs_u128_is_zero(value
);
284 case MFF_ACTSET_OUTPUT
:
285 return !wc
->masks
.actset_output
;
288 return eth_addr_is_zero(wc
->masks
.dl_src
);
290 return eth_addr_is_zero(wc
->masks
.dl_dst
);
292 return !wc
->masks
.dl_type
;
296 return eth_addr_is_zero(wc
->masks
.arp_sha
);
300 return eth_addr_is_zero(wc
->masks
.arp_tha
);
303 return !wc
->masks
.vlans
[0].tci
;
305 return !(wc
->masks
.vlans
[0].tci
& htons(VLAN_VID_MASK
));
307 return !(wc
->masks
.vlans
[0].tci
& htons(VLAN_VID_MASK
| VLAN_CFI
));
308 case MFF_DL_VLAN_PCP
:
310 return !(wc
->masks
.vlans
[0].tci
& htons(VLAN_PCP_MASK
));
313 return !(wc
->masks
.mpls_lse
[0] & htonl(MPLS_LABEL_MASK
));
315 return !(wc
->masks
.mpls_lse
[0] & htonl(MPLS_TC_MASK
));
317 return !(wc
->masks
.mpls_lse
[0] & htonl(MPLS_BOS_MASK
));
319 return !(wc
->masks
.mpls_lse
[0] & htonl(MPLS_TTL_MASK
));
322 return !wc
->masks
.nw_src
;
324 return !wc
->masks
.nw_dst
;
327 return ipv6_mask_is_any(&wc
->masks
.ipv6_src
);
329 return ipv6_mask_is_any(&wc
->masks
.ipv6_dst
);
332 return !wc
->masks
.ipv6_label
;
335 return !wc
->masks
.nw_proto
;
337 case MFF_IP_DSCP_SHIFTED
:
338 return !(wc
->masks
.nw_tos
& IP_DSCP_MASK
);
340 return !(wc
->masks
.nw_tos
& IP_ECN_MASK
);
342 return !wc
->masks
.nw_ttl
;
345 return ipv6_mask_is_any(&wc
->masks
.nd_target
);
347 case MFF_ND_RESERVED
:
348 return !wc
->masks
.igmp_group_ip4
;
349 case MFF_ND_OPTIONS_TYPE
:
350 return !wc
->masks
.tcp_flags
;
353 return !(wc
->masks
.nw_frag
& FLOW_NW_FRAG_MASK
);
356 return !wc
->masks
.nw_proto
;
358 return !wc
->masks
.nw_src
;
360 return !wc
->masks
.nw_dst
;
365 case MFF_ICMPV4_TYPE
:
366 case MFF_ICMPV6_TYPE
:
367 return !wc
->masks
.tp_src
;
371 case MFF_ICMPV4_CODE
:
372 case MFF_ICMPV6_CODE
:
373 return !wc
->masks
.tp_dst
;
375 return !wc
->masks
.tcp_flags
;
378 return !wc
->masks
.nsh
.flags
;
380 return !wc
->masks
.nsh
.ttl
;
382 return !wc
->masks
.nsh
.mdtype
;
384 return !wc
->masks
.nsh
.np
;
386 return !(wc
->masks
.nsh
.path_hdr
& htonl(NSH_SPI_MASK
));
388 return !(wc
->masks
.nsh
.path_hdr
& htonl(NSH_SI_MASK
));
393 return !wc
->masks
.nsh
.context
[mf
->id
- MFF_NSH_C1
];
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.
405 * The caller is responsible for ensuring that 'wc' corresponds to a flow that
406 * meets 'mf''s prerequisites. */
408 mf_get_mask(const struct mf_field
*mf
, const struct flow_wildcards
*wc
,
409 union mf_value
*mask
)
411 mf_get_value(mf
, &wc
->masks
, mask
);
414 /* Tests whether 'mask' is a valid wildcard bit pattern for 'mf'. Returns true
415 * if the mask is valid, false otherwise. */
417 mf_is_mask_valid(const struct mf_field
*mf
, const union mf_value
*mask
)
419 switch (mf
->maskable
) {
421 return (is_all_zeros(mask
, mf
->n_bytes
) ||
422 is_all_ones(mask
, mf
->n_bytes
));
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. */
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
)
439 ovs_be16 dl_type
= get_dl_type(flow
);
441 switch (mf
->prereqs
) {
445 return is_ethernet(flow
, wc
);
447 return (dl_type
== htons(ETH_TYPE_ARP
) ||
448 dl_type
== htons(ETH_TYPE_RARP
));
450 return dl_type
== htons(ETH_TYPE_IP
);
452 return dl_type
== htons(ETH_TYPE_IPV6
);
454 return is_vlan(flow
, wc
);
456 return eth_type_mpls(dl_type
);
458 return is_ip_any(flow
);
460 return dl_type
== htons(ETH_TYPE_NSH
);
462 return is_ct_valid(flow
, mask
, wc
);
464 /* Matching !FRAG_LATER is not enforced (mask is not checked). */
465 return is_tcp(flow
, wc
) && !(flow
->nw_frag
& FLOW_NW_FRAG_LATER
);
467 return is_udp(flow
, wc
) && !(flow
->nw_frag
& FLOW_NW_FRAG_LATER
);
469 return is_sctp(flow
, wc
) && !(flow
->nw_frag
& FLOW_NW_FRAG_LATER
);
471 return is_icmpv4(flow
, wc
);
473 return is_icmpv6(flow
, wc
);
475 return is_nd(flow
, wc
);
477 return is_nd(flow
, wc
) && flow
->tp_src
== htons(ND_NEIGHBOR_SOLICIT
);
479 return is_nd(flow
, wc
) && flow
->tp_src
== htons(ND_NEIGHBOR_ADVERT
);
485 /* Returns true if 'flow' meets the prerequisites for 'mf', false otherwise.
486 * Sets inspected bits in 'wc', if non-NULL. */
488 mf_are_prereqs_ok(const struct mf_field
*mf
, const struct flow
*flow
,
489 struct flow_wildcards
*wc
)
491 return mf_are_prereqs_ok__(mf
, flow
, NULL
, wc
);
494 /* Returns true if 'match' meets the prerequisites for 'mf', false otherwise.
497 mf_are_match_prereqs_ok(const struct mf_field
*mf
, const struct match
*match
)
499 return mf_are_prereqs_ok__(mf
, &match
->flow
, &match
->wc
, NULL
);
502 /* Returns true if 'value' may be a valid value *as part of a masked match*,
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. */
513 mf_is_value_valid(const struct mf_field
*mf
, const union mf_value
*value
)
518 case MFF_PACKET_TYPE
:
523 case MFF_TUN_IPV6_SRC
:
524 case MFF_TUN_IPV6_DST
:
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
:
536 case MFF_SKB_PRIORITY
:
541 case MFF_CT_NW_PROTO
:
544 case MFF_CT_IPV6_SRC
:
545 case MFF_CT_IPV6_DST
:
572 case MFF_ICMPV4_TYPE
:
573 case MFF_ICMPV4_CODE
:
574 case MFF_ICMPV6_TYPE
:
575 case MFF_ICMPV6_CODE
:
579 case MFF_ND_RESERVED
:
580 case MFF_ND_OPTIONS_TYPE
:
583 case MFF_IN_PORT_OXM
:
584 case MFF_ACTSET_OUTPUT
: {
586 return !ofputil_port_from_ofp11(value
->be32
, &port
);
590 return !(value
->u8
& ~IP_DSCP_MASK
);
591 case MFF_IP_DSCP_SHIFTED
:
592 return !(value
->u8
& (~IP_DSCP_MASK
>> 2));
594 return !(value
->u8
& ~IP_ECN_MASK
);
596 return !(value
->u8
& ~FLOW_NW_FRAG_MASK
);
598 return !(value
->be16
& ~htons(0x0fff));
601 return !(value
->be16
& htons(0xff00));
604 return !(value
->be16
& htons(VLAN_CFI
| VLAN_PCP_MASK
));
606 return !(value
->be16
& htons(VLAN_PCP_MASK
));
608 case MFF_DL_VLAN_PCP
:
610 return !(value
->u8
& ~(VLAN_PCP_MASK
>> VLAN_PCP_SHIFT
));
613 return !(value
->be32
& ~htonl(IPV6_LABEL_MASK
));
616 return !(value
->be32
& ~htonl(MPLS_LABEL_MASK
>> MPLS_LABEL_SHIFT
));
619 return !(value
->u8
& ~(MPLS_TC_MASK
>> MPLS_TC_SHIFT
));
622 return !(value
->u8
& ~(MPLS_BOS_MASK
>> MPLS_BOS_SHIFT
));
625 return !(value
->be16
& ~htons(FLOW_TNL_PUB_F_MASK
));
628 return !(value
->be32
& ~htonl(CS_SUPPORTED_MASK
));
633 return (value
->u8
<= 63);
635 return (value
->u8
== 1 || value
->u8
== 2);
639 return !(value
->be32
& htonl(0xFF000000));
653 /* Copies the value of field 'mf' from 'flow' into 'value'. The caller is
654 * responsible for ensuring that 'flow' meets 'mf''s prerequisites. */
656 mf_get_value(const struct mf_field
*mf
, const struct flow
*flow
,
657 union mf_value
*value
)
661 value
->be32
= htonl(flow
->dp_hash
);
664 value
->be32
= htonl(flow
->recirc_id
);
666 case MFF_PACKET_TYPE
:
667 value
->be32
= flow
->packet_type
;
670 value
->be32
= htonl(flow
->conj_id
);
673 value
->be64
= flow
->tunnel
.tun_id
;
676 value
->be32
= flow
->tunnel
.ip_src
;
679 value
->be32
= flow
->tunnel
.ip_dst
;
681 case MFF_TUN_IPV6_SRC
:
682 value
->ipv6
= flow
->tunnel
.ipv6_src
;
684 case MFF_TUN_IPV6_DST
:
685 value
->ipv6
= flow
->tunnel
.ipv6_dst
;
688 value
->be16
= htons(flow
->tunnel
.flags
& FLOW_TNL_PUB_F_MASK
);
691 value
->be16
= flow
->tunnel
.gbp_id
;
693 case MFF_TUN_GBP_FLAGS
:
694 value
->u8
= flow
->tunnel
.gbp_flags
;
697 value
->u8
= flow
->tunnel
.ip_ttl
;
700 value
->u8
= flow
->tunnel
.ip_tos
;
702 case MFF_TUN_ERSPAN_VER
:
703 value
->u8
= flow
->tunnel
.erspan_ver
;
705 case MFF_TUN_ERSPAN_IDX
:
706 value
->be32
= htonl(flow
->tunnel
.erspan_idx
);
708 case MFF_TUN_ERSPAN_DIR
:
709 value
->u8
= flow
->tunnel
.erspan_dir
;
711 case MFF_TUN_ERSPAN_HWID
:
712 value
->u8
= flow
->tunnel
.erspan_hwid
;
714 CASE_MFF_TUN_METADATA
:
715 tun_metadata_read(&flow
->tunnel
, mf
, value
);
719 value
->be64
= flow
->metadata
;
723 value
->be16
= htons(ofp_to_u16(flow
->in_port
.ofp_port
));
725 case MFF_IN_PORT_OXM
:
726 value
->be32
= ofputil_port_to_ofp11(flow
->in_port
.ofp_port
);
728 case MFF_ACTSET_OUTPUT
:
729 value
->be32
= ofputil_port_to_ofp11(flow
->actset_output
);
732 case MFF_SKB_PRIORITY
:
733 value
->be32
= htonl(flow
->skb_priority
);
737 value
->be32
= htonl(flow
->pkt_mark
);
741 value
->be32
= htonl(flow
->ct_state
);
745 value
->be16
= htons(flow
->ct_zone
);
749 value
->be32
= htonl(flow
->ct_mark
);
753 value
->be128
= hton128(flow
->ct_label
);
756 case MFF_CT_NW_PROTO
:
757 value
->u8
= flow
->ct_nw_proto
;
761 value
->be32
= flow
->ct_nw_src
;
765 value
->be32
= flow
->ct_nw_dst
;
768 case MFF_CT_IPV6_SRC
:
769 value
->ipv6
= flow
->ct_ipv6_src
;
772 case MFF_CT_IPV6_DST
:
773 value
->ipv6
= flow
->ct_ipv6_dst
;
777 value
->be16
= flow
->ct_tp_src
;
781 value
->be16
= flow
->ct_tp_dst
;
785 value
->be32
= htonl(flow
->regs
[mf
->id
- MFF_REG0
]);
789 value
->be64
= htonll(flow_get_xreg(flow
, mf
->id
- MFF_XREG0
));
793 value
->be128
= hton128(flow_get_xxreg(flow
, mf
->id
- MFF_XXREG0
));
797 value
->mac
= flow
->dl_src
;
801 value
->mac
= flow
->dl_dst
;
805 value
->be16
= flow
->dl_type
;
809 value
->be16
= flow
->vlans
[0].tci
;
813 value
->be16
= flow
->vlans
[0].tci
& htons(VLAN_VID_MASK
);
816 value
->be16
= flow
->vlans
[0].tci
& htons(VLAN_VID_MASK
| VLAN_CFI
);
819 case MFF_DL_VLAN_PCP
:
821 value
->u8
= vlan_tci_to_pcp(flow
->vlans
[0].tci
);
825 value
->be32
= htonl(mpls_lse_to_label(flow
->mpls_lse
[0]));
829 value
->u8
= mpls_lse_to_tc(flow
->mpls_lse
[0]);
833 value
->u8
= mpls_lse_to_bos(flow
->mpls_lse
[0]);
837 value
->u8
= mpls_lse_to_ttl(flow
->mpls_lse
[0]);
841 value
->be32
= flow
->nw_src
;
845 value
->be32
= flow
->nw_dst
;
849 value
->ipv6
= flow
->ipv6_src
;
853 value
->ipv6
= flow
->ipv6_dst
;
857 value
->be32
= flow
->ipv6_label
;
861 value
->u8
= flow
->nw_proto
;
865 value
->u8
= flow
->nw_tos
& IP_DSCP_MASK
;
868 case MFF_IP_DSCP_SHIFTED
:
869 value
->u8
= flow
->nw_tos
>> 2;
873 value
->u8
= flow
->nw_tos
& IP_ECN_MASK
;
877 value
->u8
= flow
->nw_ttl
;
881 value
->u8
= flow
->nw_frag
;
885 value
->be16
= htons(flow
->nw_proto
);
889 value
->be32
= flow
->nw_src
;
893 value
->be32
= flow
->nw_dst
;
898 value
->mac
= flow
->arp_sha
;
903 value
->mac
= flow
->arp_tha
;
909 value
->be16
= flow
->tp_src
;
915 value
->be16
= flow
->tp_dst
;
919 case MFF_ND_OPTIONS_TYPE
:
920 value
->be16
= flow
->tcp_flags
;
923 case MFF_ND_RESERVED
:
924 value
->be32
= flow
->igmp_group_ip4
;
927 case MFF_ICMPV4_TYPE
:
928 case MFF_ICMPV6_TYPE
:
929 value
->u8
= ntohs(flow
->tp_src
);
932 case MFF_ICMPV4_CODE
:
933 case MFF_ICMPV6_CODE
:
934 value
->u8
= ntohs(flow
->tp_dst
);
938 value
->ipv6
= flow
->nd_target
;
942 value
->u8
= flow
->nsh
.flags
;
945 value
->u8
= flow
->nsh
.ttl
;
948 value
->u8
= flow
->nsh
.mdtype
;
951 value
->u8
= flow
->nsh
.np
;
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
;
960 value
->u8
= nsh_path_hdr_to_si(flow
->nsh
.path_hdr
);
966 value
->be32
= flow
->nsh
.context
[mf
->id
- MFF_NSH_C1
];
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
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. */
983 mf_set_value(const struct mf_field
*mf
,
984 const union mf_value
*value
, struct match
*match
, char **err_str
)
992 match_set_dp_hash(match
, ntohl(value
->be32
));
995 match_set_recirc_id(match
, ntohl(value
->be32
));
997 case MFF_PACKET_TYPE
:
998 match_set_packet_type(match
, value
->be32
);
1001 match_set_conj_id(match
, ntohl(value
->be32
));
1004 match_set_tun_id(match
, value
->be64
);
1007 match_set_tun_src(match
, value
->be32
);
1010 match_set_tun_dst(match
, value
->be32
);
1012 case MFF_TUN_IPV6_SRC
:
1013 match_set_tun_ipv6_src(match
, &value
->ipv6
);
1015 case MFF_TUN_IPV6_DST
:
1016 match_set_tun_ipv6_dst(match
, &value
->ipv6
);
1019 match_set_tun_flags(match
, ntohs(value
->be16
));
1021 case MFF_TUN_GBP_ID
:
1022 match_set_tun_gbp_id(match
, value
->be16
);
1024 case MFF_TUN_GBP_FLAGS
:
1025 match_set_tun_gbp_flags(match
, value
->u8
);
1028 match_set_tun_tos(match
, value
->u8
);
1031 match_set_tun_ttl(match
, value
->u8
);
1033 case MFF_TUN_ERSPAN_VER
:
1034 match_set_tun_erspan_ver(match
, value
->u8
);
1036 case MFF_TUN_ERSPAN_IDX
:
1037 match_set_tun_erspan_idx(match
, ntohl(value
->be32
));
1039 case MFF_TUN_ERSPAN_DIR
:
1040 match_set_tun_erspan_dir(match
, value
->u8
);
1042 case MFF_TUN_ERSPAN_HWID
:
1043 match_set_tun_erspan_hwid(match
, value
->u8
);
1045 CASE_MFF_TUN_METADATA
:
1046 tun_metadata_set_match(mf
, value
, NULL
, match
, err_str
);
1050 match_set_metadata(match
, value
->be64
);
1054 match_set_in_port(match
, u16_to_ofp(ntohs(value
->be16
)));
1057 case MFF_IN_PORT_OXM
: {
1059 ofputil_port_from_ofp11(value
->be32
, &port
);
1060 match_set_in_port(match
, port
);
1063 case MFF_ACTSET_OUTPUT
: {
1065 ofputil_port_from_ofp11(value
->be32
, &port
);
1066 match_set_actset_output(match
, port
);
1070 case MFF_SKB_PRIORITY
:
1071 match_set_skb_priority(match
, ntohl(value
->be32
));
1075 match_set_pkt_mark(match
, ntohl(value
->be32
));
1079 match_set_ct_state(match
, ntohl(value
->be32
));
1083 match_set_ct_zone(match
, ntohs(value
->be16
));
1087 match_set_ct_mark(match
, ntohl(value
->be32
));
1091 match_set_ct_label(match
, ntoh128(value
->be128
));
1094 case MFF_CT_NW_PROTO
:
1095 match_set_ct_nw_proto(match
, value
->u8
);
1099 match_set_ct_nw_src(match
, value
->be32
);
1103 match_set_ct_nw_dst(match
, value
->be32
);
1106 case MFF_CT_IPV6_SRC
:
1107 match_set_ct_ipv6_src(match
, &value
->ipv6
);
1110 case MFF_CT_IPV6_DST
:
1111 match_set_ct_ipv6_dst(match
, &value
->ipv6
);
1115 match_set_ct_tp_src(match
, value
->be16
);
1119 match_set_ct_tp_dst(match
, value
->be16
);
1123 match_set_reg(match
, mf
->id
- MFF_REG0
, ntohl(value
->be32
));
1127 match_set_xreg(match
, mf
->id
- MFF_XREG0
, ntohll(value
->be64
));
1131 match_set_xxreg(match
, mf
->id
- MFF_XXREG0
, ntoh128(value
->be128
));
1135 match_set_dl_src(match
, value
->mac
);
1139 match_set_dl_dst(match
, value
->mac
);
1143 match_set_dl_type(match
, value
->be16
);
1147 match_set_dl_tci(match
, value
->be16
);
1151 match_set_dl_vlan(match
, value
->be16
, 0);
1154 match_set_vlan_vid(match
, value
->be16
);
1157 case MFF_DL_VLAN_PCP
:
1159 match_set_dl_vlan_pcp(match
, value
->u8
, 0);
1162 case MFF_MPLS_LABEL
:
1163 match_set_mpls_label(match
, 0, value
->be32
);
1167 match_set_mpls_tc(match
, 0, value
->u8
);
1171 match_set_mpls_bos(match
, 0, value
->u8
);
1175 match_set_mpls_ttl(match
, 0, value
->u8
);
1179 match_set_nw_src(match
, value
->be32
);
1183 match_set_nw_dst(match
, value
->be32
);
1187 match_set_ipv6_src(match
, &value
->ipv6
);
1191 match_set_ipv6_dst(match
, &value
->ipv6
);
1194 case MFF_IPV6_LABEL
:
1195 match_set_ipv6_label(match
, value
->be32
);
1199 match_set_nw_proto(match
, value
->u8
);
1203 match_set_nw_dscp(match
, value
->u8
);
1206 case MFF_IP_DSCP_SHIFTED
:
1207 match_set_nw_dscp(match
, value
->u8
<< 2);
1211 match_set_nw_ecn(match
, value
->u8
);
1215 match_set_nw_ttl(match
, value
->u8
);
1219 match_set_nw_frag(match
, value
->u8
);
1223 match_set_nw_proto(match
, ntohs(value
->be16
));
1227 match_set_nw_src(match
, value
->be32
);
1231 match_set_nw_dst(match
, value
->be32
);
1236 match_set_arp_sha(match
, value
->mac
);
1241 match_set_arp_tha(match
, value
->mac
);
1247 match_set_tp_src(match
, value
->be16
);
1253 match_set_tp_dst(match
, value
->be16
);
1257 match_set_tcp_flags(match
, value
->be16
);
1260 case MFF_ICMPV4_TYPE
:
1261 case MFF_ICMPV6_TYPE
:
1262 match_set_icmp_type(match
, value
->u8
);
1265 case MFF_ICMPV4_CODE
:
1266 case MFF_ICMPV6_CODE
:
1267 match_set_icmp_code(match
, value
->u8
);
1271 match_set_nd_target(match
, &value
->ipv6
);
1274 case MFF_ND_RESERVED
:
1275 match_set_nd_reserved(match
, value
->be32
);
1278 case MFF_ND_OPTIONS_TYPE
:
1279 match_set_nd_options_type(match
, value
->u8
);
1283 MATCH_SET_FIELD_UINT8(match
, nsh
.flags
, value
->u8
);
1286 MATCH_SET_FIELD_UINT8(match
, nsh
.ttl
, value
->u8
);
1288 case MFF_NSH_MDTYPE
:
1289 MATCH_SET_FIELD_UINT8(match
, nsh
.mdtype
, value
->u8
);
1292 MATCH_SET_FIELD_UINT8(match
, nsh
.np
, value
->u8
);
1295 match
->wc
.masks
.nsh
.path_hdr
|= htonl(NSH_SPI_MASK
);
1296 nsh_path_hdr_set_spi(&match
->flow
.nsh
.path_hdr
, value
->be32
);
1299 match
->wc
.masks
.nsh
.path_hdr
|= htonl(NSH_SI_MASK
);
1300 nsh_path_hdr_set_si(&match
->flow
.nsh
.path_hdr
, value
->u8
);
1306 MATCH_SET_FIELD_BE32(match
, nsh
.context
[mf
->id
- MFF_NSH_C1
],
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
1320 mf_mask_field_masked(const struct mf_field
*mf
, const union mf_value
*mask
,
1321 struct flow_wildcards
*wc
)
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
;
1332 union mf_value mask_value
;
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
];
1338 mf_set_flow_value(mf
, &mask_value
, &wc
->masks
);
1341 /* Unwildcard 'wc' member field described by 'mf'. The caller is
1342 * responsible for ensuring that 'mask' meets 'mf''s prerequisites. */
1344 mf_mask_field(const struct mf_field
*mf
, struct flow_wildcards
*wc
)
1346 mf_mask_field_masked(mf
, &exact_match_mask
, wc
);
1350 field_len(const struct mf_field
*mf
, const union mf_value
*value_
)
1352 const uint8_t *value
= &value_
->u8
;
1355 if (!mf
->variable_len
) {
1363 for (i
= 0; i
< mf
->n_bytes
; i
++) {
1364 if (value
[i
] != 0) {
1369 return mf
->n_bytes
- i
;
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).
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. */
1381 mf_field_len(const struct mf_field
*mf
, const union mf_value
*value
,
1382 const union mf_value
*mask
, bool *is_masked_
)
1385 bool is_masked
= mask
&& !is_all_ones(mask
, mf
->n_bytes
);
1387 len
= field_len(mf
, value
);
1389 mask_len
= field_len(mf
, mask
);
1390 len
= MAX(len
, mask_len
);
1394 *is_masked_
= is_masked
;
1400 /* Sets 'flow' member field described by 'mf' to 'value'. The caller is
1401 * responsible for ensuring that 'flow' meets 'mf''s prerequisites.*/
1403 mf_set_flow_value(const struct mf_field
*mf
,
1404 const union mf_value
*value
, struct flow
*flow
)
1408 flow
->dp_hash
= ntohl(value
->be32
);
1411 flow
->recirc_id
= ntohl(value
->be32
);
1413 case MFF_PACKET_TYPE
:
1414 flow
->packet_type
= value
->be32
;
1417 flow
->conj_id
= ntohl(value
->be32
);
1420 flow
->tunnel
.tun_id
= value
->be64
;
1423 flow
->tunnel
.ip_src
= value
->be32
;
1426 flow
->tunnel
.ip_dst
= value
->be32
;
1428 case MFF_TUN_IPV6_SRC
:
1429 flow
->tunnel
.ipv6_src
= value
->ipv6
;
1431 case MFF_TUN_IPV6_DST
:
1432 flow
->tunnel
.ipv6_dst
= value
->ipv6
;
1435 flow
->tunnel
.flags
= (flow
->tunnel
.flags
& ~FLOW_TNL_PUB_F_MASK
) |
1438 case MFF_TUN_GBP_ID
:
1439 flow
->tunnel
.gbp_id
= value
->be16
;
1441 case MFF_TUN_GBP_FLAGS
:
1442 flow
->tunnel
.gbp_flags
= value
->u8
;
1445 flow
->tunnel
.ip_tos
= value
->u8
;
1448 flow
->tunnel
.ip_ttl
= value
->u8
;
1450 case MFF_TUN_ERSPAN_VER
:
1451 flow
->tunnel
.erspan_ver
= value
->u8
;
1453 case MFF_TUN_ERSPAN_IDX
:
1454 flow
->tunnel
.erspan_idx
= ntohl(value
->be32
);
1456 case MFF_TUN_ERSPAN_DIR
:
1457 flow
->tunnel
.erspan_dir
= value
->u8
;
1459 case MFF_TUN_ERSPAN_HWID
:
1460 flow
->tunnel
.erspan_hwid
= value
->u8
;
1462 CASE_MFF_TUN_METADATA
:
1463 tun_metadata_write(&flow
->tunnel
, mf
, value
);
1466 flow
->metadata
= value
->be64
;
1470 flow
->in_port
.ofp_port
= u16_to_ofp(ntohs(value
->be16
));
1472 case MFF_IN_PORT_OXM
:
1473 ofputil_port_from_ofp11(value
->be32
, &flow
->in_port
.ofp_port
);
1475 case MFF_ACTSET_OUTPUT
:
1476 ofputil_port_from_ofp11(value
->be32
, &flow
->actset_output
);
1479 case MFF_SKB_PRIORITY
:
1480 flow
->skb_priority
= ntohl(value
->be32
);
1484 flow
->pkt_mark
= ntohl(value
->be32
);
1488 flow
->ct_state
= ntohl(value
->be32
);
1492 flow
->ct_zone
= ntohs(value
->be16
);
1496 flow
->ct_mark
= ntohl(value
->be32
);
1500 flow
->ct_label
= ntoh128(value
->be128
);
1503 case MFF_CT_NW_PROTO
:
1504 flow
->ct_nw_proto
= value
->u8
;
1508 flow
->ct_nw_src
= value
->be32
;
1512 flow
->ct_nw_dst
= value
->be32
;
1515 case MFF_CT_IPV6_SRC
:
1516 flow
->ct_ipv6_src
= value
->ipv6
;
1519 case MFF_CT_IPV6_DST
:
1520 flow
->ct_ipv6_dst
= value
->ipv6
;
1524 flow
->ct_tp_src
= value
->be16
;
1528 flow
->ct_tp_dst
= value
->be16
;
1532 flow
->regs
[mf
->id
- MFF_REG0
] = ntohl(value
->be32
);
1536 flow_set_xreg(flow
, mf
->id
- MFF_XREG0
, ntohll(value
->be64
));
1540 flow_set_xxreg(flow
, mf
->id
- MFF_XXREG0
, ntoh128(value
->be128
));
1544 flow
->dl_src
= value
->mac
;
1548 flow
->dl_dst
= value
->mac
;
1552 flow
->dl_type
= value
->be16
;
1556 flow
->vlans
[0].tci
= value
->be16
;
1557 flow_fix_vlan_tpid(flow
);
1561 flow_set_dl_vlan(flow
, value
->be16
, 0);
1562 flow_fix_vlan_tpid(flow
);
1566 flow_set_vlan_vid(flow
, value
->be16
);
1567 flow_fix_vlan_tpid(flow
);
1570 case MFF_DL_VLAN_PCP
:
1572 flow_set_vlan_pcp(flow
, value
->u8
, 0);
1573 flow_fix_vlan_tpid(flow
);
1576 case MFF_MPLS_LABEL
:
1577 flow_set_mpls_label(flow
, 0, value
->be32
);
1581 flow_set_mpls_tc(flow
, 0, value
->u8
);
1585 flow_set_mpls_bos(flow
, 0, value
->u8
);
1589 flow_set_mpls_ttl(flow
, 0, value
->u8
);
1593 flow
->nw_src
= value
->be32
;
1597 flow
->nw_dst
= value
->be32
;
1601 flow
->ipv6_src
= value
->ipv6
;
1605 flow
->ipv6_dst
= value
->ipv6
;
1608 case MFF_IPV6_LABEL
:
1609 flow
->ipv6_label
= value
->be32
& htonl(IPV6_LABEL_MASK
);
1613 flow
->nw_proto
= value
->u8
;
1617 flow
->nw_tos
&= ~IP_DSCP_MASK
;
1618 flow
->nw_tos
|= value
->u8
& IP_DSCP_MASK
;
1621 case MFF_IP_DSCP_SHIFTED
:
1622 flow
->nw_tos
&= ~IP_DSCP_MASK
;
1623 flow
->nw_tos
|= value
->u8
<< 2;
1627 flow
->nw_tos
&= ~IP_ECN_MASK
;
1628 flow
->nw_tos
|= value
->u8
& IP_ECN_MASK
;
1632 flow
->nw_ttl
= value
->u8
;
1636 flow
->nw_frag
= value
->u8
& FLOW_NW_FRAG_MASK
;
1640 flow
->nw_proto
= ntohs(value
->be16
);
1644 flow
->nw_src
= value
->be32
;
1648 flow
->nw_dst
= value
->be32
;
1653 flow
->arp_sha
= value
->mac
;
1658 flow
->arp_tha
= value
->mac
;
1664 flow
->tp_src
= value
->be16
;
1670 flow
->tp_dst
= value
->be16
;
1674 flow
->tcp_flags
= value
->be16
;
1677 case MFF_ICMPV4_TYPE
:
1678 case MFF_ICMPV6_TYPE
:
1679 flow
->tp_src
= htons(value
->u8
);
1682 case MFF_ICMPV4_CODE
:
1683 case MFF_ICMPV6_CODE
:
1684 flow
->tp_dst
= htons(value
->u8
);
1688 flow
->nd_target
= value
->ipv6
;
1691 case MFF_ND_RESERVED
:
1692 flow
->igmp_group_ip4
= value
->be32
;
1695 case MFF_ND_OPTIONS_TYPE
:
1696 flow
->tcp_flags
= htons(value
->u8
);
1700 flow
->nsh
.flags
= value
->u8
;
1703 flow
->nsh
.ttl
= value
->u8
;
1705 case MFF_NSH_MDTYPE
:
1706 flow
->nsh
.mdtype
= value
->u8
;
1709 flow
->nsh
.np
= value
->u8
;
1712 nsh_path_hdr_set_spi(&flow
->nsh
.path_hdr
, value
->be32
);
1715 nsh_path_hdr_set_si(&flow
->nsh
.path_hdr
, value
->u8
);
1721 flow
->nsh
.context
[mf
->id
- MFF_NSH_C1
] = value
->be32
;
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] =
1734 apply_mask(const uint8_t *src
, const uint8_t *mask
, uint8_t *dst
, size_t n
)
1738 for (i
= 0; i
< n
; i
++) {
1739 dst
[i
] = (src
[i
] & mask
[i
]) | (dst
[i
] & ~mask
[i
]);
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.*/
1747 mf_set_flow_value_masked(const struct mf_field
*field
,
1748 const union mf_value
*value
,
1749 const union mf_value
*mask
,
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
);
1761 mf_is_tun_metadata(const struct mf_field
*mf
)
1763 return mf
->id
>= MFF_TUN_METADATA0
&&
1764 mf
->id
< MFF_TUN_METADATA0
+ TUN_METADATA_NUM_OPTS
;
1768 mf_is_pipeline_field(const struct mf_field
*mf
)
1774 case MFF_TUN_IPV6_SRC
:
1775 case MFF_TUN_IPV6_DST
:
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
:
1786 case MFF_IN_PORT_OXM
:
1790 case MFF_PACKET_TYPE
:
1798 case MFF_ACTSET_OUTPUT
:
1799 case MFF_SKB_PRIORITY
:
1805 case MFF_CT_NW_PROTO
:
1808 case MFF_CT_IPV6_SRC
:
1809 case MFF_CT_IPV6_DST
:
1818 case MFF_DL_VLAN_PCP
:
1820 case MFF_MPLS_LABEL
:
1828 case MFF_IPV6_LABEL
:
1831 case MFF_IP_DSCP_SHIFTED
:
1847 case MFF_ICMPV4_TYPE
:
1848 case MFF_ICMPV4_CODE
:
1849 case MFF_ICMPV6_TYPE
:
1850 case MFF_ICMPV6_CODE
:
1854 case MFF_ND_RESERVED
:
1855 case MFF_ND_OPTIONS_TYPE
:
1858 case MFF_NSH_MDTYPE
:
1874 /* Returns true if 'mf' has previously been set in 'flow', false if
1875 * it contains a non-default value.
1877 * The caller is responsible for ensuring that 'flow' meets 'mf''s
1880 mf_is_set(const struct mf_field
*mf
, const struct flow
*flow
)
1882 if (!mf_is_tun_metadata(mf
)) {
1883 union mf_value value
;
1885 mf_get_value(mf
, flow
, &value
);
1886 return !is_all_zeros(&value
, mf
->n_bytes
);
1888 return ULLONG_GET(flow
->tunnel
.metadata
.present
.map
,
1889 mf
->id
- MFF_TUN_METADATA0
);
1893 /* Makes 'match' wildcard field 'mf'.
1895 * The caller is responsible for ensuring that 'match' meets 'mf''s
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. */
1902 mf_set_wild(const struct mf_field
*mf
, struct match
*match
, char **err_str
)
1910 match
->flow
.dp_hash
= 0;
1911 match
->wc
.masks
.dp_hash
= 0;
1914 match
->flow
.recirc_id
= 0;
1915 match
->wc
.masks
.recirc_id
= 0;
1917 case MFF_PACKET_TYPE
:
1918 match
->flow
.packet_type
= 0;
1919 match
->wc
.masks
.packet_type
= 0;
1922 match
->flow
.conj_id
= 0;
1923 match
->wc
.masks
.conj_id
= 0;
1926 match_set_tun_id_masked(match
, htonll(0), htonll(0));
1929 match_set_tun_src_masked(match
, htonl(0), htonl(0));
1932 match_set_tun_dst_masked(match
, htonl(0), htonl(0));
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
);
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
);
1947 match_set_tun_flags_masked(match
, 0, 0);
1949 case MFF_TUN_GBP_ID
:
1950 match_set_tun_gbp_id_masked(match
, 0, 0);
1952 case MFF_TUN_GBP_FLAGS
:
1953 match_set_tun_gbp_flags_masked(match
, 0, 0);
1956 match_set_tun_tos_masked(match
, 0, 0);
1959 match_set_tun_ttl_masked(match
, 0, 0);
1961 case MFF_TUN_ERSPAN_VER
:
1962 match_set_tun_erspan_ver_masked(match
, 0, 0);
1964 case MFF_TUN_ERSPAN_IDX
:
1965 match_set_tun_erspan_idx_masked(match
, 0, 0);
1967 case MFF_TUN_ERSPAN_DIR
:
1968 match_set_tun_erspan_dir_masked(match
, 0, 0);
1970 case MFF_TUN_ERSPAN_HWID
:
1971 match_set_tun_erspan_hwid_masked(match
, 0, 0);
1973 CASE_MFF_TUN_METADATA
:
1974 tun_metadata_set_match(mf
, NULL
, NULL
, match
, err_str
);
1978 match_set_metadata_masked(match
, htonll(0), htonll(0));
1982 case MFF_IN_PORT_OXM
:
1983 match
->flow
.in_port
.ofp_port
= 0;
1984 match
->wc
.masks
.in_port
.ofp_port
= 0;
1986 case MFF_ACTSET_OUTPUT
:
1987 match
->flow
.actset_output
= 0;
1988 match
->wc
.masks
.actset_output
= 0;
1991 case MFF_SKB_PRIORITY
:
1992 match
->flow
.skb_priority
= 0;
1993 match
->wc
.masks
.skb_priority
= 0;
1997 match
->flow
.pkt_mark
= 0;
1998 match
->wc
.masks
.pkt_mark
= 0;
2002 match
->flow
.ct_state
= 0;
2003 match
->wc
.masks
.ct_state
= 0;
2007 match
->flow
.ct_zone
= 0;
2008 match
->wc
.masks
.ct_zone
= 0;
2012 match
->flow
.ct_mark
= 0;
2013 match
->wc
.masks
.ct_mark
= 0;
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
));
2021 case MFF_CT_NW_PROTO
:
2022 match
->flow
.ct_nw_proto
= 0;
2023 match
->wc
.masks
.ct_nw_proto
= 0;
2027 match
->flow
.ct_nw_src
= 0;
2028 match
->wc
.masks
.ct_nw_src
= 0;
2032 match
->flow
.ct_nw_dst
= 0;
2033 match
->wc
.masks
.ct_nw_dst
= 0;
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
);
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
);
2047 match
->flow
.ct_tp_src
= 0;
2048 match
->wc
.masks
.ct_tp_src
= 0;
2052 match
->flow
.ct_tp_dst
= 0;
2053 match
->wc
.masks
.ct_tp_dst
= 0;
2057 match_set_reg_masked(match
, mf
->id
- MFF_REG0
, 0, 0);
2061 match_set_xreg_masked(match
, mf
->id
- MFF_XREG0
, 0, 0);
2065 match_set_xxreg_masked(match
, mf
->id
- MFF_XXREG0
, OVS_U128_ZERO
,
2071 match
->flow
.dl_src
= eth_addr_zero
;
2072 match
->wc
.masks
.dl_src
= eth_addr_zero
;
2076 match
->flow
.dl_dst
= eth_addr_zero
;
2077 match
->wc
.masks
.dl_dst
= eth_addr_zero
;
2081 match
->flow
.dl_type
= htons(0);
2082 match
->wc
.masks
.dl_type
= htons(0);
2086 match_set_dl_tci_masked(match
, htons(0), htons(0));
2091 match_set_any_vid(match
);
2094 case MFF_DL_VLAN_PCP
:
2096 match_set_any_pcp(match
);
2099 case MFF_MPLS_LABEL
:
2100 match_set_any_mpls_label(match
, 0);
2104 match_set_any_mpls_tc(match
, 0);
2108 match_set_any_mpls_bos(match
, 0);
2112 match_set_any_mpls_ttl(match
, 0);
2117 match_set_nw_src_masked(match
, htonl(0), htonl(0));
2122 match_set_nw_dst_masked(match
, htonl(0), htonl(0));
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
);
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
);
2135 case MFF_IPV6_LABEL
:
2136 match
->wc
.masks
.ipv6_label
= htonl(0);
2137 match
->flow
.ipv6_label
= htonl(0);
2141 match
->wc
.masks
.nw_proto
= 0;
2142 match
->flow
.nw_proto
= 0;
2146 case MFF_IP_DSCP_SHIFTED
:
2147 match
->wc
.masks
.nw_tos
&= ~IP_DSCP_MASK
;
2148 match
->flow
.nw_tos
&= ~IP_DSCP_MASK
;
2152 match
->wc
.masks
.nw_tos
&= ~IP_ECN_MASK
;
2153 match
->flow
.nw_tos
&= ~IP_ECN_MASK
;
2157 match
->wc
.masks
.nw_ttl
= 0;
2158 match
->flow
.nw_ttl
= 0;
2162 match
->wc
.masks
.nw_frag
&= ~FLOW_NW_FRAG_MASK
;
2163 match
->flow
.nw_frag
&= ~FLOW_NW_FRAG_MASK
;
2167 match
->wc
.masks
.nw_proto
= 0;
2168 match
->flow
.nw_proto
= 0;
2173 match
->flow
.arp_sha
= eth_addr_zero
;
2174 match
->wc
.masks
.arp_sha
= eth_addr_zero
;
2179 match
->flow
.arp_tha
= eth_addr_zero
;
2180 match
->wc
.masks
.arp_tha
= eth_addr_zero
;
2183 case MFF_ND_RESERVED
:
2184 match
->wc
.masks
.igmp_group_ip4
= htonl(0);
2185 match
->flow
.igmp_group_ip4
= htonl(0);
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);
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);
2207 case MFF_ND_OPTIONS_TYPE
:
2208 match
->wc
.masks
.tcp_flags
= htons(0);
2209 match
->flow
.tcp_flags
= htons(0);
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
);
2219 MATCH_SET_FIELD_MASKED(match
, nsh
.flags
, 0, 0);
2222 MATCH_SET_FIELD_MASKED(match
, nsh
.ttl
, 0, 0);
2224 case MFF_NSH_MDTYPE
:
2225 MATCH_SET_FIELD_MASKED(match
, nsh
.mdtype
, 0, 0);
2228 MATCH_SET_FIELD_MASKED(match
, nsh
.np
, 0, 0);
2231 match
->wc
.masks
.nsh
.path_hdr
&= ~htonl(NSH_SPI_MASK
);
2232 nsh_path_hdr_set_spi(&match
->flow
.nsh
.path_hdr
, htonl(0));
2235 match
->wc
.masks
.nsh
.path_hdr
&= ~htonl(NSH_SI_MASK
);
2236 nsh_path_hdr_set_si(&match
->flow
.nsh
.path_hdr
, 0);
2242 MATCH_SET_FIELD_MASKED(match
, nsh
.context
[mf
->id
- MFF_NSH_C1
],
2243 htonl(0), htonl(0));
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.
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).
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.
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.
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. */
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
)
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
;
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.
2294 * [*] Except where the field is a tunnel metadata field and 'mask' is
2295 * all-0-bits; see above. */
2298 case MFF_CT_NW_PROTO
:
2301 case MFF_CT_IPV6_SRC
:
2302 case MFF_CT_IPV6_DST
:
2306 case MFF_PACKET_TYPE
:
2309 case MFF_IN_PORT_OXM
:
2310 case MFF_ACTSET_OUTPUT
:
2311 case MFF_SKB_PRIORITY
:
2314 case MFF_DL_VLAN_PCP
:
2316 case MFF_MPLS_LABEL
:
2323 case MFF_IP_DSCP_SHIFTED
:
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
;
2335 match_set_dp_hash_masked(match
, ntohl(value
->be32
), ntohl(mask
->be32
));
2338 match_set_tun_id_masked(match
, value
->be64
, mask
->be64
);
2341 match_set_tun_src_masked(match
, value
->be32
, mask
->be32
);
2344 match_set_tun_dst_masked(match
, value
->be32
, mask
->be32
);
2346 case MFF_TUN_IPV6_SRC
:
2347 match_set_tun_ipv6_src_masked(match
, &value
->ipv6
, &mask
->ipv6
);
2349 case MFF_TUN_IPV6_DST
:
2350 match_set_tun_ipv6_dst_masked(match
, &value
->ipv6
, &mask
->ipv6
);
2353 match_set_tun_flags_masked(match
, ntohs(value
->be16
), ntohs(mask
->be16
));
2355 case MFF_TUN_GBP_ID
:
2356 match_set_tun_gbp_id_masked(match
, value
->be16
, mask
->be16
);
2358 case MFF_TUN_GBP_FLAGS
:
2359 match_set_tun_gbp_flags_masked(match
, value
->u8
, mask
->u8
);
2362 match_set_tun_ttl_masked(match
, value
->u8
, mask
->u8
);
2365 match_set_tun_tos_masked(match
, value
->u8
, mask
->u8
);
2367 case MFF_TUN_ERSPAN_VER
:
2368 match_set_tun_erspan_ver_masked(match
, value
->u8
, mask
->u8
);
2370 case MFF_TUN_ERSPAN_IDX
:
2371 match_set_tun_erspan_idx_masked(match
, ntohl(value
->be32
),
2374 case MFF_TUN_ERSPAN_DIR
:
2375 match_set_tun_erspan_dir_masked(match
, value
->u8
, mask
->u8
);
2377 case MFF_TUN_ERSPAN_HWID
:
2378 match_set_tun_erspan_hwid_masked(match
, value
->u8
, mask
->u8
);
2380 CASE_MFF_TUN_METADATA
:
2381 tun_metadata_set_match(mf
, value
, mask
, match
, err_str
);
2385 match_set_metadata_masked(match
, value
->be64
, mask
->be64
);
2389 match_set_reg_masked(match
, mf
->id
- MFF_REG0
,
2390 ntohl(value
->be32
), ntohl(mask
->be32
));
2394 match_set_xreg_masked(match
, mf
->id
- MFF_XREG0
,
2395 ntohll(value
->be64
), ntohll(mask
->be64
));
2399 match_set_xxreg_masked(match
, mf
->id
- MFF_XXREG0
,
2400 ntoh128(value
->be128
), ntoh128(mask
->be128
));
2405 match_set_pkt_mark_masked(match
, ntohl(value
->be32
),
2410 match_set_ct_state_masked(match
, ntohl(value
->be32
), ntohl(mask
->be32
));
2414 match_set_ct_mark_masked(match
, ntohl(value
->be32
), ntohl(mask
->be32
));
2418 match_set_ct_label_masked(match
, ntoh128(value
->be128
),
2419 ntoh128(mask
->be128
));
2423 match_set_dl_dst_masked(match
, value
->mac
, mask
->mac
);
2427 match_set_dl_src_masked(match
, value
->mac
, mask
->mac
);
2432 match_set_arp_sha_masked(match
, value
->mac
, mask
->mac
);
2437 match_set_arp_tha_masked(match
, value
->mac
, mask
->mac
);
2441 match_set_dl_tci_masked(match
, value
->be16
, mask
->be16
);
2445 match_set_vlan_vid_masked(match
, value
->be16
, mask
->be16
);
2449 match_set_nw_src_masked(match
, value
->be32
, mask
->be32
);
2453 match_set_nw_dst_masked(match
, value
->be32
, mask
->be32
);
2457 match_set_ipv6_src_masked(match
, &value
->ipv6
, &mask
->ipv6
);
2461 match_set_ipv6_dst_masked(match
, &value
->ipv6
, &mask
->ipv6
);
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
);
2468 match_set_ipv6_label_masked(match
, value
->be32
, mask
->be32
);
2473 match_set_nd_target_masked(match
, &value
->ipv6
, &mask
->ipv6
);
2477 match_set_nw_frag_masked(match
, value
->u8
, mask
->u8
);
2481 match_set_nw_src_masked(match
, value
->be32
, mask
->be32
);
2485 match_set_nw_dst_masked(match
, value
->be32
, mask
->be32
);
2491 match_set_tp_src_masked(match
, value
->be16
, mask
->be16
);
2497 match_set_tp_dst_masked(match
, value
->be16
, mask
->be16
);
2501 match_set_tcp_flags_masked(match
, value
->be16
, mask
->be16
);
2505 MATCH_SET_FIELD_MASKED(match
, nsh
.flags
, value
->u8
, mask
->u8
);
2508 MATCH_SET_FIELD_MASKED(match
, nsh
.ttl
, value
->u8
, mask
->u8
);
2510 case MFF_NSH_MDTYPE
:
2511 MATCH_SET_FIELD_MASKED(match
, nsh
.mdtype
, value
->u8
, mask
->u8
);
2514 MATCH_SET_FIELD_MASKED(match
, nsh
.np
, value
->u8
, mask
->u8
);
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
);
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
);
2530 MATCH_SET_FIELD_MASKED(match
, nsh
.context
[mf
->id
- MFF_NSH_C1
],
2531 value
->be32
, mask
->be32
);
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
);
2546 mf_check__(const struct mf_subfield
*sf
, const struct match
*match
,
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
;
2573 /* Sets all the bits in 'sf' to 1 within 'wc', if 'wc' is nonnull. */
2575 unwildcard_subfield(const struct mf_subfield
*sf
, struct flow_wildcards
*wc
)
2578 union mf_value mask
;
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
);
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.
2589 * 'src' and 'dst' may overlap. */
2591 mf_subfield_copy(const struct mf_subfield
*src
,
2592 const struct mf_subfield
*dst
,
2593 struct flow
*flow
, struct flow_wildcards
*wc
)
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
);
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
,
2608 mf_set_flow_value(dst
->field
, &dst_value
, flow
);
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.
2615 * 'src' and 'dst' may overlap. */
2617 mf_subfield_swap(const struct mf_subfield
*a
,
2618 const struct mf_subfield
*b
,
2619 struct flow
*flow
, struct flow_wildcards
*wc
)
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
);
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
;
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
,
2637 mf_set_flow_value(b
->field
, &b_value
, flow
);
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
,
2643 mf_set_flow_value(a
->field
, &a_value
, flow
);
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
2651 mf_check_src(const struct mf_subfield
*sf
, const struct match
*match
)
2653 return mf_check__(sf
, match
, "source");
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
2660 mf_check_dst(const struct mf_subfield
*sf
, const struct match
*match
)
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",
2666 return OFPERR_OFPBAC_BAD_SET_ARGUMENT
;
2671 /* Copies the value and wildcard bit pattern for 'mf' from 'match' into the
2672 * 'value' and 'mask', respectively. */
2674 mf_get(const struct mf_field
*mf
, const struct match
*match
,
2675 union mf_value
*value
, union mf_value
*mask
)
2677 mf_get_value(mf
, &match
->flow
, value
);
2678 mf_get_mask(mf
, &match
->wc
, mask
);
2682 mf_from_integer_string(const struct mf_field
*mf
, const char *s
,
2683 uint8_t *valuep
, uint8_t *maskp
)
2686 const char *err_str
= "";
2689 err
= parse_int_string(s
, valuep
, mf
->n_bytes
, &tail
);
2690 if (err
|| (*tail
!= '\0' && *tail
!= '/')) {
2696 err
= parse_int_string(tail
+ 1, maskp
, mf
->n_bytes
, &tail
);
2697 if (err
|| *tail
!= '\0') {
2702 memset(maskp
, 0xff, mf
->n_bytes
);
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
);
2712 return xasprintf("%s: bad syntax for %s %s", s
, mf
->name
, err_str
);
2717 mf_from_packet_type_string(const char *s
, ovs_be32
*packet_type
)
2720 const char *err_str
= "";
2724 err_str
= "missing '('";
2728 err
= parse_int_string(s
, (uint8_t *)packet_type
, 2, &tail
);
2734 err_str
= "missing ','";
2738 err
= parse_int_string(s
, ((uint8_t *)packet_type
) + 2, 2, &tail
);
2740 err_str
= "ns_type";
2744 err_str
= "missing ')'";
2751 return xasprintf("%s: bad syntax for packet type %s", s
, err_str
);
2755 mf_from_ethernet_string(const struct mf_field
*mf
, const char *s
,
2756 struct eth_addr
*mac
, struct eth_addr
*mask
)
2760 ovs_assert(mf
->n_bytes
== ETH_ADDR_LEN
);
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
;
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
)) {
2776 return xasprintf("%s: invalid Ethernet address", s
);
2780 mf_from_ipv4_string(const struct mf_field
*mf
, const char *s
,
2781 ovs_be32
*ip
, ovs_be32
*mask
)
2783 ovs_assert(mf
->n_bytes
== sizeof *ip
);
2784 return ip_parse_masked(s
, ip
, mask
);
2788 mf_from_ipv6_string(const struct mf_field
*mf
, const char *s
,
2789 struct in6_addr
*ipv6
, struct in6_addr
*mask
)
2791 ovs_assert(mf
->n_bytes
== sizeof *ipv6
);
2792 return ipv6_parse_masked(s
, ipv6
, mask
);
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
)
2802 ovs_assert(mf
->n_bytes
== sizeof(ovs_be16
));
2804 if (ofputil_port_from_string(s
, port_map
, &port
)) {
2805 *valuep
= htons(ofp_to_u16(port
));
2806 *maskp
= OVS_BE16_MAX
;
2809 return xasprintf("%s: invalid or unknown port for %s", s
, mf
->name
);
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
)
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
;
2825 return xasprintf("%s: port value out of range for %s", s
, mf
->name
);
2828 struct frag_handling
{
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 */
2840 { "first", A
|L
, A
},
2841 { "later", A
|L
, A
|L
},
2846 { "not_later", L
, 0 },
2853 mf_from_frag_string(const char *s
, uint8_t *valuep
, uint8_t *maskp
)
2855 const struct frag_handling
*h
;
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
;
2867 return xasprintf("%s: unknown fragment type (valid types are \"no\", "
2868 "\"yes\", \"first\", \"later\", \"not_later\"", s
);
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
,
2878 uint32_t flags
, mask
;
2880 err
= parse_flags(s
, bit_to_string
, '\0', field_name
, &err_str
,
2881 &flags
, ntohs(allowed
), maskp
? &mask
: NULL
);
2886 *flagsp
= htons(flags
);
2888 *maskp
= htons(mask
);
2895 mf_from_tcp_flags_string(const char *s
, ovs_be16
*flagsp
, ovs_be16
*maskp
)
2897 return parse_mf_flags(s
, packet_tcp_flag_to_string
, "TCP", flagsp
,
2898 TCP_FLAGS_BE16(OVS_BE16_MAX
), maskp
);
2902 mf_from_tun_flags_string(const char *s
, ovs_be16
*flagsp
, ovs_be16
*maskp
)
2904 return parse_mf_flags(s
, flow_tun_flag_to_string
, "tunnel", flagsp
,
2905 htons(FLOW_TNL_PUB_F_MASK
), maskp
);
2909 mf_from_ct_state_string(const char *s
, ovs_be32
*flagsp
, ovs_be32
*maskp
)
2913 uint32_t flags
, mask
;
2915 err
= parse_flags(s
, ct_state_to_string
, '\0', "ct_state", &err_str
,
2916 &flags
, CS_SUPPORTED_MASK
, maskp
? &mask
: NULL
);
2921 *flagsp
= htonl(flags
);
2923 *maskp
= htonl(mask
);
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. */
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
)
2938 if (!strcmp(s
, "*")) {
2939 memset(value
, 0, mf
->n_bytes
);
2940 memset(mask
, 0, mf
->n_bytes
);
2944 switch (mf
->string
) {
2946 case MFS_HEXADECIMAL
:
2947 error
= mf_from_integer_string(mf
, s
,
2948 (uint8_t *) value
, (uint8_t *) mask
);
2952 ovs_assert(mf
->n_bytes
== sizeof(ovs_be32
));
2953 error
= mf_from_ct_state_string(s
, &value
->be32
, &mask
->be32
);
2957 error
= mf_from_ethernet_string(mf
, s
, &value
->mac
, &mask
->mac
);
2961 error
= mf_from_ipv4_string(mf
, s
, &value
->be32
, &mask
->be32
);
2965 error
= mf_from_ipv6_string(mf
, s
, &value
->ipv6
, &mask
->ipv6
);
2969 error
= mf_from_ofp_port_string(mf
, s
, port_map
,
2970 &value
->be16
, &mask
->be16
);
2973 case MFS_OFP_PORT_OXM
:
2974 error
= mf_from_ofp_port_string32(mf
, s
, port_map
,
2975 &value
->be32
, &mask
->be32
);
2979 error
= mf_from_frag_string(s
, &value
->u8
, &mask
->u8
);
2983 ovs_assert(mf
->n_bytes
== sizeof(ovs_be16
));
2984 error
= mf_from_tun_flags_string(s
, &value
->be16
, &mask
->be16
);
2988 ovs_assert(mf
->n_bytes
== sizeof(ovs_be16
));
2989 error
= mf_from_tcp_flags_string(s
, &value
->be16
, &mask
->be16
);
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
;
3002 if (!error
&& !mf_is_mask_valid(mf
, mask
)) {
3003 error
= xasprintf("%s: invalid mask for field %s", s
, mf
->name
);
3008 /* Parses 's', a string value for field 'mf', into 'value'. Returns NULL if
3009 * successful, otherwise a malloc()'d string describing the error. */
3011 mf_parse_value(const struct mf_field
*mf
, const char *s
,
3012 const struct ofputil_port_map
*port_map
, union mf_value
*value
)
3014 union mf_value mask
;
3017 error
= mf_parse(mf
, s
, port_map
, value
, &mask
);
3022 if (!is_all_ones((const uint8_t *) &mask
, mf
->n_bytes
)) {
3023 return xasprintf("%s: wildcards not allowed here", s
);
3029 mf_format_integer_string(const struct mf_field
*mf
, const uint8_t *valuep
,
3030 const uint8_t *maskp
, struct ds
*s
)
3032 if (mf
->string
== MFS_HEXADECIMAL
) {
3033 ds_put_hex(s
, valuep
, mf
->n_bytes
);
3035 unsigned long long int integer
= 0;
3038 ovs_assert(mf
->n_bytes
<= 8);
3039 for (i
= 0; i
< mf
->n_bytes
; i
++) {
3040 integer
= (integer
<< 8) | valuep
[i
];
3042 ds_put_format(s
, "%lld", integer
);
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
);
3055 mf_format_frag_string(uint8_t value
, uint8_t mask
, struct ds
*s
)
3057 const struct frag_handling
*h
;
3059 mask
&= FLOW_NW_FRAG_MASK
;
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
);
3068 ds_put_cstr(s
, "<error>");
3072 mf_format_tnl_flags_string(ovs_be16 value
, ovs_be16 mask
, struct ds
*s
)
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
);
3079 mf_format_tcp_flags_string(ovs_be16 value
, ovs_be16 mask
, struct ds
*s
)
3081 format_flags_masked(s
, NULL
, packet_tcp_flag_to_string
, ntohs(value
),
3082 TCP_FLAGS(mask
), TCP_FLAGS(OVS_BE16_MAX
));
3086 mf_format_ct_state_string(ovs_be32 value
, ovs_be32 mask
, struct ds
*s
)
3088 format_flags_masked(s
, NULL
, ct_state_to_string
, ntohl(value
),
3089 ntohl(mask
), UINT16_MAX
);
3093 mf_format_packet_type_string(ovs_be32 value
, ovs_be32 mask
, struct ds
*s
)
3095 format_packet_type_masked(s
, value
, mask
);
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. */
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
,
3107 if (is_all_zeros(mask
, mf
->n_bytes
)) {
3108 ds_put_cstr(s
, "ANY");
3110 } else if (is_all_ones(mask
, mf
->n_bytes
)) {
3115 switch (mf
->string
) {
3116 case MFS_OFP_PORT_OXM
:
3119 ofputil_port_from_ofp11(value
->be32
, &port
);
3120 ofputil_format_port(port
, port_map
, s
);
3126 ofputil_format_port(u16_to_ofp(ntohs(value
->be16
)), port_map
, s
);
3131 case MFS_HEXADECIMAL
:
3132 mf_format_integer_string(mf
, (uint8_t *) value
, (uint8_t *) mask
, s
);
3136 mf_format_ct_state_string(value
->be32
,
3137 mask
? mask
->be32
: OVS_BE32_MAX
, s
);
3141 eth_format_masked(value
->mac
, mask
? &mask
->mac
: NULL
, s
);
3145 ip_format_masked(value
->be32
, mask
? mask
->be32
: OVS_BE32_MAX
, s
);
3149 ipv6_format_masked(&value
->ipv6
, mask
? &mask
->ipv6
: NULL
, s
);
3153 mf_format_frag_string(value
->u8
, mask
? mask
->u8
: UINT8_MAX
, s
);
3157 mf_format_tnl_flags_string(value
->be16
,
3158 mask
? mask
->be16
: OVS_BE16_MAX
, s
);
3162 mf_format_tcp_flags_string(value
->be16
,
3163 mask
? mask
->be16
: OVS_BE16_MAX
, s
);
3166 case MFS_PACKET_TYPE
:
3167 mf_format_packet_type_string(value
->be32
,
3168 mask
? mask
->be32
: OVS_BE32_MAX
, s
);
3176 /* Makes subfield 'sf' within 'flow' exactly match the 'sf->n_bits'
3177 * least-significant bits in 'x'.
3180 mf_write_subfield_flow(const struct mf_subfield
*sf
,
3181 const union mf_subvalue
*x
, struct flow
*flow
)
3183 const struct mf_field
*field
= sf
->field
;
3184 union mf_value value
;
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
);
3192 /* Makes subfield 'sf' within 'match' exactly match the 'sf->n_bits'
3193 * least-significant bits in 'x'.
3196 mf_write_subfield(const struct mf_subfield
*sf
, const union mf_subvalue
*x
,
3197 struct match
*match
)
3199 const struct mf_field
*field
= sf
->field
;
3200 union mf_value value
, mask
;
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
);
3209 mf_write_subfield_value(const struct mf_subfield
*sf
, const void *src
,
3210 struct match
*match
)
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);
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
);
3222 /* 'v' and 'm' correspond to values of 'field'. This function copies them into
3223 * 'match' in the correspond positions. */
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
)
3230 union mf_value value
, mask
;
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
);
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(). */
3241 mf_read_subfield(const struct mf_subfield
*sf
, const struct flow
*flow
,
3242 union mf_subvalue
*x
)
3244 union mf_value value
;
3246 mf_get_value(sf
->field
, flow
, &value
);
3248 memset(x
, 0, sizeof *x
);
3249 bitwise_copy(&value
, sf
->field
->n_bytes
, sf
->ofs
,
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
3258 mf_get_subfield(const struct mf_subfield
*sf
, const struct flow
*flow
)
3260 union mf_value value
;
3262 mf_get_value(sf
->field
, flow
, &value
);
3263 return bitwise_get(&value
, sf
->field
->n_bytes
, sf
->ofs
, sf
->n_bits
);
3267 mf_format_subvalue(const union mf_subvalue
*subvalue
, struct ds
*s
)
3269 ds_put_hex(s
, subvalue
->u8
, sizeof subvalue
->u8
);
3273 field_array_set(enum mf_field_id id
, const union mf_value
*value
,
3274 struct field_array
*fa
)
3276 size_t i
, offset
= 0;
3278 ovs_assert(id
< MFF_N_IDS
);
3280 /* Find the spot for 'id'. */
3281 BITMAP_FOR_EACH_1 (i
, id
, fa
->used
.bm
) {
3282 offset
+= mf_from_id(i
)->n_bytes
;
3285 size_t value_size
= mf_from_id(id
)->n_bytes
;
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
);
3295 fa
->values_size
+= value_size
;
3297 bitmap_set1(fa
->used
.bm
, id
);
3299 memcpy(fa
->values
+ offset
, value
, value_size
);
3302 /* A wrapper for variable length mf_fields that is maintained by
3303 * struct vl_mff_map.*/
3304 struct vl_mf_field
{
3306 struct ovs_refcount ref_cnt
;
3307 struct cmap_node cmap_node
; /* In ofproto->vl_mff_map->cmap. */
3310 static inline uint32_t
3311 mf_field_hash(uint32_t key
)
3313 return hash_int(key
, 0);
3317 vmf_delete(struct vl_mf_field
*vmf
)
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
);
3325 "Attempted to delete VMF %s but refcount is nonzero!",
3331 mf_vl_mff_map_clear(struct vl_mff_map
*vl_mff_map
, bool force
)
3332 OVS_REQUIRES(vl_mff_map
->mutex
)
3334 struct vl_mf_field
*vmf
;
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
;
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
));
3353 static struct vl_mf_field
*
3354 mf_get_vl_mff__(uint32_t id
, const struct vl_mff_map
*vl_mff_map
)
3356 struct vl_mf_field
*vmf
;
3358 CMAP_FOR_EACH_WITH_HASH (vmf
, cmap_node
, mf_field_hash(id
),
3359 &vl_mff_map
->cmap
) {
3360 if (vmf
->mf
.id
== id
) {
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
)
3375 if (mff
&& mff
->variable_len
&& vl_mff_map
) {
3376 return &mf_get_vl_mff__(mff
->id
, vl_mff_map
)->mf
;
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
)
3387 struct ofputil_tlv_map
*tlv_map
;
3388 struct vl_mf_field
*vmf
;
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
;
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
;
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
;
3411 vmf
= mf_get_vl_mff__(idx
, vl_mff_map
);
3413 cmap_remove(&vl_mff_map
->cmap
, &vmf
->cmap_node
,
3414 mf_field_hash(idx
));
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
)
3427 struct ofputil_tlv_map
*tlv_map
;
3428 struct vl_mf_field
*vmf
;
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
;
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
);
3444 cmap_insert(&vl_mff_map
->cmap
, &vmf
->cmap_node
,
3445 mf_field_hash(idx
));
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
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.*/
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
)
3462 switch (ttm
->command
) {
3464 return mf_vl_mff_map_add(vl_mff_map
, ttm
);
3467 return mf_vl_mff_map_del(vl_mff_map
, ttm
, false);
3470 return mf_vl_mff_map_clear(vl_mff_map
, false);
3479 /* Returns true if a variable length meta-flow field 'mff' is not mapped in
3480 * the 'vl_mff_map'. */
3482 mf_vl_mff_invalid(const struct mf_field
*mff
, const struct vl_mff_map
*map
)
3484 return map
&& mff
&& mff
->variable_len
&& !mff
->mapped
;
3488 mf_vl_mff_set_tlv_bitmap(const struct mf_field
*mff
, uint64_t *tlv_bitmap
)
3490 if (mff
&& mff
->mapped
) {
3491 ovs_assert(mf_is_tun_metadata(mff
));
3492 ULLONG_SET1(*tlv_bitmap
, mff
->id
- MFF_TUN_METADATA0
);
3497 mf_vl_mff_ref_cnt_mod(const struct vl_mff_map
*map
, uint64_t tlv_bitmap
,
3500 struct vl_mf_field
*vmf
;
3504 ULLONG_FOR_EACH_1 (i
, tlv_bitmap
) {
3505 vmf
= mf_get_vl_mff__(i
+ MFF_TUN_METADATA0
, map
);
3508 ovs_refcount_ref(&vmf
->ref_cnt
);
3510 ovs_refcount_unref(&vmf
->ref_cnt
);
3513 VLOG_WARN("Invalid TLV index %d.", i
);
3520 mf_vl_mff_ref(const struct vl_mff_map
*map
, uint64_t tlv_bitmap
)
3522 mf_vl_mff_ref_cnt_mod(map
, tlv_bitmap
, true);
3526 mf_vl_mff_unref(const struct vl_mff_map
*map
, uint64_t tlv_bitmap
)
3528 mf_vl_mff_ref_cnt_mod(map
, tlv_bitmap
, false);
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
)
3536 enum ofperr error
= nx_pull_header(b
, vl_mff_map
, field
, masked
);
3541 mf_vl_mff_set_tlv_bitmap(*field
, tlv_bitmap
);
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
)
3550 enum ofperr error
= nx_pull_entry(b
, vl_mff_map
, field
, value
, mask
, true);
3555 mf_vl_mff_set_tlv_bitmap(*field
, tlv_bitmap
);
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
)
3565 *field
= mf_from_nxm_header(header
, vl_mff_map
);
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
;
3572 mf_vl_mff_set_tlv_bitmap(*field
, tlv_bitmap
);
3576 /* Returns true if the 1-bits in 'super' are a superset of the 1-bits in 'sub',
3577 * false otherwise. */
3579 mf_bitmap_is_superset(const struct mf_bitmap
*super
,
3580 const struct mf_bitmap
*sub
)
3582 return bitmap_is_superset(super
->bm
, sub
->bm
, MFF_N_IDS
);
3585 /* Returns the bitwise-and of 'a' and 'b'. */
3587 mf_bitmap_and(struct mf_bitmap a
, struct mf_bitmap b
)
3589 bitmap_and(a
.bm
, b
.bm
, MFF_N_IDS
);
3593 /* Returns the bitwise-or of 'a' and 'b'. */
3595 mf_bitmap_or(struct mf_bitmap a
, struct mf_bitmap b
)
3597 bitmap_or(a
.bm
, b
.bm
, MFF_N_IDS
);
3601 /* Returns the bitwise-not of 'x'. */
3603 mf_bitmap_not(struct mf_bitmap x
)
3605 bitmap_not(x
.bm
, MFF_N_IDS
);