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
];
394 case MFF_TUN_GTPU_FLAGS
:
395 return !wc
->masks
.tunnel
.gtpu_flags
;
396 case MFF_TUN_GTPU_MSGTYPE
:
397 return !wc
->masks
.tunnel
.gtpu_msgtype
;
405 /* Initializes 'mask' with the wildcard bit pattern for field 'mf' within 'wc'.
406 * Each bit in 'mask' will be set to 1 if the bit is significant for matching
407 * purposes, or to 0 if it is wildcarded.
409 * The caller is responsible for ensuring that 'wc' corresponds to a flow that
410 * meets 'mf''s prerequisites. */
412 mf_get_mask(const struct mf_field
*mf
, const struct flow_wildcards
*wc
,
413 union mf_value
*mask
)
415 mf_get_value(mf
, &wc
->masks
, mask
);
418 /* Tests whether 'mask' is a valid wildcard bit pattern for 'mf'. Returns true
419 * if the mask is valid, false otherwise. */
421 mf_is_mask_valid(const struct mf_field
*mf
, const union mf_value
*mask
)
423 switch (mf
->maskable
) {
425 return (is_all_zeros(mask
, mf
->n_bytes
) ||
426 is_all_ones(mask
, mf
->n_bytes
));
435 /* Returns true if 'flow' meets the prerequisites for 'mf', false otherwise.
436 * If a non-NULL 'mask' is passed, zero-valued matches can also be verified.
437 * Sets inspected bits in 'wc', if non-NULL. */
439 mf_are_prereqs_ok__(const struct mf_field
*mf
, const struct flow
*flow
,
440 const struct flow_wildcards
*mask
,
441 struct flow_wildcards
*wc
)
443 ovs_be16 dl_type
= get_dl_type(flow
);
445 switch (mf
->prereqs
) {
449 return is_ethernet(flow
, wc
);
451 return (dl_type
== htons(ETH_TYPE_ARP
) ||
452 dl_type
== htons(ETH_TYPE_RARP
));
454 return dl_type
== htons(ETH_TYPE_IP
);
456 return dl_type
== htons(ETH_TYPE_IPV6
);
458 return is_vlan(flow
, wc
);
460 return eth_type_mpls(dl_type
);
462 return is_ip_any(flow
);
464 return dl_type
== htons(ETH_TYPE_NSH
);
466 return is_ct_valid(flow
, mask
, wc
);
468 /* Matching !FRAG_LATER is not enforced (mask is not checked). */
469 return is_tcp(flow
, wc
) && !(flow
->nw_frag
& FLOW_NW_FRAG_LATER
);
471 return is_udp(flow
, wc
) && !(flow
->nw_frag
& FLOW_NW_FRAG_LATER
);
473 return is_sctp(flow
, wc
) && !(flow
->nw_frag
& FLOW_NW_FRAG_LATER
);
475 return is_icmpv4(flow
, wc
);
477 return is_icmpv6(flow
, wc
);
479 return is_nd(flow
, wc
);
481 return is_nd(flow
, wc
) && flow
->tp_src
== htons(ND_NEIGHBOR_SOLICIT
);
483 return is_nd(flow
, wc
) && flow
->tp_src
== htons(ND_NEIGHBOR_ADVERT
);
489 /* Returns true if 'flow' meets the prerequisites for 'mf', false otherwise.
490 * Sets inspected bits in 'wc', if non-NULL. */
492 mf_are_prereqs_ok(const struct mf_field
*mf
, const struct flow
*flow
,
493 struct flow_wildcards
*wc
)
495 return mf_are_prereqs_ok__(mf
, flow
, NULL
, wc
);
498 /* Returns true if 'match' meets the prerequisites for 'mf', false otherwise.
501 mf_are_match_prereqs_ok(const struct mf_field
*mf
, const struct match
*match
)
503 return mf_are_prereqs_ok__(mf
, &match
->flow
, &match
->wc
, NULL
);
506 /* Returns true if 'value' may be a valid value *as part of a masked match*,
509 * A value is not rejected just because it is not valid for the field in
510 * question, but only if it doesn't make sense to test the bits in question at
511 * all. For example, the MFF_VLAN_TCI field will never have a nonzero value
512 * without the VLAN_CFI bit being set, but we can't reject those values because
513 * it is still legitimate to test just for those bits (see the documentation
514 * for NXM_OF_VLAN_TCI in meta-flow.h). On the other hand, there is never a
515 * reason to set the low bit of MFF_IP_DSCP to 1, so we reject that. */
517 mf_is_value_valid(const struct mf_field
*mf
, const union mf_value
*value
)
522 case MFF_PACKET_TYPE
:
527 case MFF_TUN_IPV6_SRC
:
528 case MFF_TUN_IPV6_DST
:
532 case MFF_TUN_GBP_FLAGS
:
533 case MFF_TUN_ERSPAN_IDX
:
534 case MFF_TUN_ERSPAN_VER
:
535 case MFF_TUN_ERSPAN_DIR
:
536 case MFF_TUN_ERSPAN_HWID
:
537 case MFF_TUN_GTPU_FLAGS
:
538 case MFF_TUN_GTPU_MSGTYPE
:
539 CASE_MFF_TUN_METADATA
:
542 case MFF_SKB_PRIORITY
:
547 case MFF_CT_NW_PROTO
:
550 case MFF_CT_IPV6_SRC
:
551 case MFF_CT_IPV6_DST
:
578 case MFF_ICMPV4_TYPE
:
579 case MFF_ICMPV4_CODE
:
580 case MFF_ICMPV6_TYPE
:
581 case MFF_ICMPV6_CODE
:
585 case MFF_ND_RESERVED
:
586 case MFF_ND_OPTIONS_TYPE
:
589 case MFF_IN_PORT_OXM
:
590 case MFF_ACTSET_OUTPUT
: {
592 return !ofputil_port_from_ofp11(value
->be32
, &port
);
596 return !(value
->u8
& ~IP_DSCP_MASK
);
597 case MFF_IP_DSCP_SHIFTED
:
598 return !(value
->u8
& (~IP_DSCP_MASK
>> 2));
600 return !(value
->u8
& ~IP_ECN_MASK
);
602 return !(value
->u8
& ~FLOW_NW_FRAG_MASK
);
604 return !(value
->be16
& ~htons(0x0fff));
607 return !(value
->be16
& htons(0xff00));
610 return !(value
->be16
& htons(VLAN_CFI
| VLAN_PCP_MASK
));
612 return !(value
->be16
& htons(VLAN_PCP_MASK
));
614 case MFF_DL_VLAN_PCP
:
616 return !(value
->u8
& ~(VLAN_PCP_MASK
>> VLAN_PCP_SHIFT
));
619 return !(value
->be32
& ~htonl(IPV6_LABEL_MASK
));
622 return !(value
->be32
& ~htonl(MPLS_LABEL_MASK
>> MPLS_LABEL_SHIFT
));
625 return !(value
->u8
& ~(MPLS_TC_MASK
>> MPLS_TC_SHIFT
));
628 return !(value
->u8
& ~(MPLS_BOS_MASK
>> MPLS_BOS_SHIFT
));
631 return !(value
->be16
& ~htons(FLOW_TNL_PUB_F_MASK
));
634 return !(value
->be32
& ~htonl(CS_SUPPORTED_MASK
));
639 return (value
->u8
<= 63);
641 return (value
->u8
== 1 || value
->u8
== 2);
645 return !(value
->be32
& htonl(0xFF000000));
659 /* Copies the value of field 'mf' from 'flow' into 'value'. The caller is
660 * responsible for ensuring that 'flow' meets 'mf''s prerequisites. */
662 mf_get_value(const struct mf_field
*mf
, const struct flow
*flow
,
663 union mf_value
*value
)
667 value
->be32
= htonl(flow
->dp_hash
);
670 value
->be32
= htonl(flow
->recirc_id
);
672 case MFF_PACKET_TYPE
:
673 value
->be32
= flow
->packet_type
;
676 value
->be32
= htonl(flow
->conj_id
);
679 value
->be64
= flow
->tunnel
.tun_id
;
682 value
->be32
= flow
->tunnel
.ip_src
;
685 value
->be32
= flow
->tunnel
.ip_dst
;
687 case MFF_TUN_IPV6_SRC
:
688 value
->ipv6
= flow
->tunnel
.ipv6_src
;
690 case MFF_TUN_IPV6_DST
:
691 value
->ipv6
= flow
->tunnel
.ipv6_dst
;
694 value
->be16
= htons(flow
->tunnel
.flags
& FLOW_TNL_PUB_F_MASK
);
697 value
->be16
= flow
->tunnel
.gbp_id
;
699 case MFF_TUN_GBP_FLAGS
:
700 value
->u8
= flow
->tunnel
.gbp_flags
;
703 value
->u8
= flow
->tunnel
.ip_ttl
;
706 value
->u8
= flow
->tunnel
.ip_tos
;
708 case MFF_TUN_ERSPAN_VER
:
709 value
->u8
= flow
->tunnel
.erspan_ver
;
711 case MFF_TUN_ERSPAN_IDX
:
712 value
->be32
= htonl(flow
->tunnel
.erspan_idx
);
714 case MFF_TUN_ERSPAN_DIR
:
715 value
->u8
= flow
->tunnel
.erspan_dir
;
717 case MFF_TUN_ERSPAN_HWID
:
718 value
->u8
= flow
->tunnel
.erspan_hwid
;
720 case MFF_TUN_GTPU_FLAGS
:
721 value
->u8
= flow
->tunnel
.gtpu_flags
;
723 case MFF_TUN_GTPU_MSGTYPE
:
724 value
->u8
= flow
->tunnel
.gtpu_msgtype
;
726 CASE_MFF_TUN_METADATA
:
727 tun_metadata_read(&flow
->tunnel
, mf
, value
);
731 value
->be64
= flow
->metadata
;
735 value
->be16
= htons(ofp_to_u16(flow
->in_port
.ofp_port
));
737 case MFF_IN_PORT_OXM
:
738 value
->be32
= ofputil_port_to_ofp11(flow
->in_port
.ofp_port
);
740 case MFF_ACTSET_OUTPUT
:
741 value
->be32
= ofputil_port_to_ofp11(flow
->actset_output
);
744 case MFF_SKB_PRIORITY
:
745 value
->be32
= htonl(flow
->skb_priority
);
749 value
->be32
= htonl(flow
->pkt_mark
);
753 value
->be32
= htonl(flow
->ct_state
);
757 value
->be16
= htons(flow
->ct_zone
);
761 value
->be32
= htonl(flow
->ct_mark
);
765 value
->be128
= hton128(flow
->ct_label
);
768 case MFF_CT_NW_PROTO
:
769 value
->u8
= flow
->ct_nw_proto
;
773 value
->be32
= flow
->ct_nw_src
;
777 value
->be32
= flow
->ct_nw_dst
;
780 case MFF_CT_IPV6_SRC
:
781 value
->ipv6
= flow
->ct_ipv6_src
;
784 case MFF_CT_IPV6_DST
:
785 value
->ipv6
= flow
->ct_ipv6_dst
;
789 value
->be16
= flow
->ct_tp_src
;
793 value
->be16
= flow
->ct_tp_dst
;
797 value
->be32
= htonl(flow
->regs
[mf
->id
- MFF_REG0
]);
801 value
->be64
= htonll(flow_get_xreg(flow
, mf
->id
- MFF_XREG0
));
805 value
->be128
= hton128(flow_get_xxreg(flow
, mf
->id
- MFF_XXREG0
));
809 value
->mac
= flow
->dl_src
;
813 value
->mac
= flow
->dl_dst
;
817 value
->be16
= flow
->dl_type
;
821 value
->be16
= flow
->vlans
[0].tci
;
825 value
->be16
= flow
->vlans
[0].tci
& htons(VLAN_VID_MASK
);
828 value
->be16
= flow
->vlans
[0].tci
& htons(VLAN_VID_MASK
| VLAN_CFI
);
831 case MFF_DL_VLAN_PCP
:
833 value
->u8
= vlan_tci_to_pcp(flow
->vlans
[0].tci
);
837 value
->be32
= htonl(mpls_lse_to_label(flow
->mpls_lse
[0]));
841 value
->u8
= mpls_lse_to_tc(flow
->mpls_lse
[0]);
845 value
->u8
= mpls_lse_to_bos(flow
->mpls_lse
[0]);
849 value
->u8
= mpls_lse_to_ttl(flow
->mpls_lse
[0]);
853 value
->be32
= flow
->nw_src
;
857 value
->be32
= flow
->nw_dst
;
861 value
->ipv6
= flow
->ipv6_src
;
865 value
->ipv6
= flow
->ipv6_dst
;
869 value
->be32
= flow
->ipv6_label
;
873 value
->u8
= flow
->nw_proto
;
877 value
->u8
= flow
->nw_tos
& IP_DSCP_MASK
;
880 case MFF_IP_DSCP_SHIFTED
:
881 value
->u8
= flow
->nw_tos
>> 2;
885 value
->u8
= flow
->nw_tos
& IP_ECN_MASK
;
889 value
->u8
= flow
->nw_ttl
;
893 value
->u8
= flow
->nw_frag
;
897 value
->be16
= htons(flow
->nw_proto
);
901 value
->be32
= flow
->nw_src
;
905 value
->be32
= flow
->nw_dst
;
910 value
->mac
= flow
->arp_sha
;
915 value
->mac
= flow
->arp_tha
;
921 value
->be16
= flow
->tp_src
;
927 value
->be16
= flow
->tp_dst
;
931 case MFF_ND_OPTIONS_TYPE
:
932 value
->be16
= flow
->tcp_flags
;
935 case MFF_ND_RESERVED
:
936 value
->be32
= flow
->igmp_group_ip4
;
939 case MFF_ICMPV4_TYPE
:
940 case MFF_ICMPV6_TYPE
:
941 value
->u8
= ntohs(flow
->tp_src
);
944 case MFF_ICMPV4_CODE
:
945 case MFF_ICMPV6_CODE
:
946 value
->u8
= ntohs(flow
->tp_dst
);
950 value
->ipv6
= flow
->nd_target
;
954 value
->u8
= flow
->nsh
.flags
;
957 value
->u8
= flow
->nsh
.ttl
;
960 value
->u8
= flow
->nsh
.mdtype
;
963 value
->u8
= flow
->nsh
.np
;
966 value
->be32
= nsh_path_hdr_to_spi(flow
->nsh
.path_hdr
);
967 if (value
->be32
== htonl(NSH_SPI_MASK
>> NSH_SPI_SHIFT
)) {
968 value
->be32
= OVS_BE32_MAX
;
972 value
->u8
= nsh_path_hdr_to_si(flow
->nsh
.path_hdr
);
978 value
->be32
= flow
->nsh
.context
[mf
->id
- MFF_NSH_C1
];
987 /* Makes 'match' match field 'mf' exactly, with the value matched taken from
988 * 'value'. The caller is responsible for ensuring that 'match' meets 'mf''s
991 * If non-NULL, 'err_str' returns a malloc'ed string describing any errors
992 * with the request or NULL if there is no error. The caller is reponsible
993 * for freeing the string. */
995 mf_set_value(const struct mf_field
*mf
,
996 const union mf_value
*value
, struct match
*match
, char **err_str
)
1004 match_set_dp_hash(match
, ntohl(value
->be32
));
1007 match_set_recirc_id(match
, ntohl(value
->be32
));
1009 case MFF_PACKET_TYPE
:
1010 match_set_packet_type(match
, value
->be32
);
1013 match_set_conj_id(match
, ntohl(value
->be32
));
1016 match_set_tun_id(match
, value
->be64
);
1019 match_set_tun_src(match
, value
->be32
);
1022 match_set_tun_dst(match
, value
->be32
);
1024 case MFF_TUN_IPV6_SRC
:
1025 match_set_tun_ipv6_src(match
, &value
->ipv6
);
1027 case MFF_TUN_IPV6_DST
:
1028 match_set_tun_ipv6_dst(match
, &value
->ipv6
);
1031 match_set_tun_flags(match
, ntohs(value
->be16
));
1033 case MFF_TUN_GBP_ID
:
1034 match_set_tun_gbp_id(match
, value
->be16
);
1036 case MFF_TUN_GBP_FLAGS
:
1037 match_set_tun_gbp_flags(match
, value
->u8
);
1040 match_set_tun_tos(match
, value
->u8
);
1043 match_set_tun_ttl(match
, value
->u8
);
1045 case MFF_TUN_ERSPAN_VER
:
1046 match_set_tun_erspan_ver(match
, value
->u8
);
1048 case MFF_TUN_ERSPAN_IDX
:
1049 match_set_tun_erspan_idx(match
, ntohl(value
->be32
));
1051 case MFF_TUN_ERSPAN_DIR
:
1052 match_set_tun_erspan_dir(match
, value
->u8
);
1054 case MFF_TUN_ERSPAN_HWID
:
1055 match_set_tun_erspan_hwid(match
, value
->u8
);
1057 case MFF_TUN_GTPU_FLAGS
:
1058 match_set_tun_gtpu_flags(match
, value
->u8
);
1060 case MFF_TUN_GTPU_MSGTYPE
:
1061 match_set_tun_gtpu_msgtype(match
, value
->u8
);
1063 CASE_MFF_TUN_METADATA
:
1064 tun_metadata_set_match(mf
, value
, NULL
, match
, err_str
);
1068 match_set_metadata(match
, value
->be64
);
1072 match_set_in_port(match
, u16_to_ofp(ntohs(value
->be16
)));
1075 case MFF_IN_PORT_OXM
: {
1077 ofputil_port_from_ofp11(value
->be32
, &port
);
1078 match_set_in_port(match
, port
);
1081 case MFF_ACTSET_OUTPUT
: {
1083 ofputil_port_from_ofp11(value
->be32
, &port
);
1084 match_set_actset_output(match
, port
);
1088 case MFF_SKB_PRIORITY
:
1089 match_set_skb_priority(match
, ntohl(value
->be32
));
1093 match_set_pkt_mark(match
, ntohl(value
->be32
));
1097 match_set_ct_state(match
, ntohl(value
->be32
));
1101 match_set_ct_zone(match
, ntohs(value
->be16
));
1105 match_set_ct_mark(match
, ntohl(value
->be32
));
1109 match_set_ct_label(match
, ntoh128(value
->be128
));
1112 case MFF_CT_NW_PROTO
:
1113 match_set_ct_nw_proto(match
, value
->u8
);
1117 match_set_ct_nw_src(match
, value
->be32
);
1121 match_set_ct_nw_dst(match
, value
->be32
);
1124 case MFF_CT_IPV6_SRC
:
1125 match_set_ct_ipv6_src(match
, &value
->ipv6
);
1128 case MFF_CT_IPV6_DST
:
1129 match_set_ct_ipv6_dst(match
, &value
->ipv6
);
1133 match_set_ct_tp_src(match
, value
->be16
);
1137 match_set_ct_tp_dst(match
, value
->be16
);
1141 match_set_reg(match
, mf
->id
- MFF_REG0
, ntohl(value
->be32
));
1145 match_set_xreg(match
, mf
->id
- MFF_XREG0
, ntohll(value
->be64
));
1149 match_set_xxreg(match
, mf
->id
- MFF_XXREG0
, ntoh128(value
->be128
));
1153 match_set_dl_src(match
, value
->mac
);
1157 match_set_dl_dst(match
, value
->mac
);
1161 match_set_dl_type(match
, value
->be16
);
1165 match_set_dl_tci(match
, value
->be16
);
1169 match_set_dl_vlan(match
, value
->be16
, 0);
1172 match_set_vlan_vid(match
, value
->be16
);
1175 case MFF_DL_VLAN_PCP
:
1177 match_set_dl_vlan_pcp(match
, value
->u8
, 0);
1180 case MFF_MPLS_LABEL
:
1181 match_set_mpls_label(match
, 0, value
->be32
);
1185 match_set_mpls_tc(match
, 0, value
->u8
);
1189 match_set_mpls_bos(match
, 0, value
->u8
);
1193 match_set_mpls_ttl(match
, 0, value
->u8
);
1197 match_set_nw_src(match
, value
->be32
);
1201 match_set_nw_dst(match
, value
->be32
);
1205 match_set_ipv6_src(match
, &value
->ipv6
);
1209 match_set_ipv6_dst(match
, &value
->ipv6
);
1212 case MFF_IPV6_LABEL
:
1213 match_set_ipv6_label(match
, value
->be32
);
1217 match_set_nw_proto(match
, value
->u8
);
1221 match_set_nw_dscp(match
, value
->u8
);
1224 case MFF_IP_DSCP_SHIFTED
:
1225 match_set_nw_dscp(match
, value
->u8
<< 2);
1229 match_set_nw_ecn(match
, value
->u8
);
1233 match_set_nw_ttl(match
, value
->u8
);
1237 match_set_nw_frag(match
, value
->u8
);
1241 match_set_nw_proto(match
, ntohs(value
->be16
));
1245 match_set_nw_src(match
, value
->be32
);
1249 match_set_nw_dst(match
, value
->be32
);
1254 match_set_arp_sha(match
, value
->mac
);
1259 match_set_arp_tha(match
, value
->mac
);
1265 match_set_tp_src(match
, value
->be16
);
1271 match_set_tp_dst(match
, value
->be16
);
1275 match_set_tcp_flags(match
, value
->be16
);
1278 case MFF_ICMPV4_TYPE
:
1279 case MFF_ICMPV6_TYPE
:
1280 match_set_icmp_type(match
, value
->u8
);
1283 case MFF_ICMPV4_CODE
:
1284 case MFF_ICMPV6_CODE
:
1285 match_set_icmp_code(match
, value
->u8
);
1289 match_set_nd_target(match
, &value
->ipv6
);
1292 case MFF_ND_RESERVED
:
1293 match_set_nd_reserved(match
, value
->be32
);
1296 case MFF_ND_OPTIONS_TYPE
:
1297 match_set_nd_options_type(match
, value
->u8
);
1301 MATCH_SET_FIELD_UINT8(match
, nsh
.flags
, value
->u8
);
1304 MATCH_SET_FIELD_UINT8(match
, nsh
.ttl
, value
->u8
);
1306 case MFF_NSH_MDTYPE
:
1307 MATCH_SET_FIELD_UINT8(match
, nsh
.mdtype
, value
->u8
);
1310 MATCH_SET_FIELD_UINT8(match
, nsh
.np
, value
->u8
);
1313 match
->wc
.masks
.nsh
.path_hdr
|= htonl(NSH_SPI_MASK
);
1314 nsh_path_hdr_set_spi(&match
->flow
.nsh
.path_hdr
, value
->be32
);
1317 match
->wc
.masks
.nsh
.path_hdr
|= htonl(NSH_SI_MASK
);
1318 nsh_path_hdr_set_si(&match
->flow
.nsh
.path_hdr
, value
->u8
);
1324 MATCH_SET_FIELD_BE32(match
, nsh
.context
[mf
->id
- MFF_NSH_C1
],
1334 /* Unwildcard the bits in 'mask' of the 'wc' member field described by 'mf'.
1335 * The caller is responsible for ensuring that 'wc' meets 'mf''s
1338 mf_mask_field_masked(const struct mf_field
*mf
, const union mf_value
*mask
,
1339 struct flow_wildcards
*wc
)
1341 union mf_value temp_mask
;
1342 /* For MFF_DL_VLAN, we cannot send a all 1's to flow_set_dl_vlan() as that
1343 * will be considered as OFP10_VLAN_NONE. So make sure the mask only has
1344 * valid bits in this case. */
1345 if (mf
->id
== MFF_DL_VLAN
) {
1346 temp_mask
.be16
= htons(VLAN_VID_MASK
) & mask
->be16
;
1350 union mf_value mask_value
;
1352 mf_get_value(mf
, &wc
->masks
, &mask_value
);
1353 for (size_t i
= 0; i
< mf
->n_bytes
; i
++) {
1354 mask_value
.b
[i
] |= mask
->b
[i
];
1356 mf_set_flow_value(mf
, &mask_value
, &wc
->masks
);
1359 /* Unwildcard 'wc' member field described by 'mf'. The caller is
1360 * responsible for ensuring that 'mask' meets 'mf''s prerequisites. */
1362 mf_mask_field(const struct mf_field
*mf
, struct flow_wildcards
*wc
)
1364 mf_mask_field_masked(mf
, &exact_match_mask
, wc
);
1368 field_len(const struct mf_field
*mf
, const union mf_value
*value_
)
1370 const uint8_t *value
= &value_
->u8
;
1373 if (!mf
->variable_len
) {
1381 for (i
= 0; i
< mf
->n_bytes
; i
++) {
1382 if (value
[i
] != 0) {
1387 return mf
->n_bytes
- i
;
1390 /* Returns the effective length of the field. For fixed length fields,
1391 * this is just the defined length. For variable length fields, it is
1392 * the minimum size encoding that retains the same meaning (i.e.
1393 * discarding leading zeros).
1395 * 'is_masked' returns (if non-NULL) whether the original contained
1396 * a mask. Otherwise, a mask that is the same length as the value
1397 * might be misinterpreted as an exact match. */
1399 mf_field_len(const struct mf_field
*mf
, const union mf_value
*value
,
1400 const union mf_value
*mask
, bool *is_masked_
)
1403 bool is_masked
= mask
&& !is_all_ones(mask
, mf
->n_bytes
);
1405 len
= field_len(mf
, value
);
1407 mask_len
= field_len(mf
, mask
);
1408 len
= MAX(len
, mask_len
);
1412 *is_masked_
= is_masked
;
1418 /* Sets 'flow' member field described by 'mf' to 'value'. The caller is
1419 * responsible for ensuring that 'flow' meets 'mf''s prerequisites.*/
1421 mf_set_flow_value(const struct mf_field
*mf
,
1422 const union mf_value
*value
, struct flow
*flow
)
1426 flow
->dp_hash
= ntohl(value
->be32
);
1429 flow
->recirc_id
= ntohl(value
->be32
);
1431 case MFF_PACKET_TYPE
:
1432 flow
->packet_type
= value
->be32
;
1435 flow
->conj_id
= ntohl(value
->be32
);
1438 flow
->tunnel
.tun_id
= value
->be64
;
1441 flow
->tunnel
.ip_src
= value
->be32
;
1444 flow
->tunnel
.ip_dst
= value
->be32
;
1446 case MFF_TUN_IPV6_SRC
:
1447 flow
->tunnel
.ipv6_src
= value
->ipv6
;
1449 case MFF_TUN_IPV6_DST
:
1450 flow
->tunnel
.ipv6_dst
= value
->ipv6
;
1453 flow
->tunnel
.flags
= (flow
->tunnel
.flags
& ~FLOW_TNL_PUB_F_MASK
) |
1456 case MFF_TUN_GBP_ID
:
1457 flow
->tunnel
.gbp_id
= value
->be16
;
1459 case MFF_TUN_GBP_FLAGS
:
1460 flow
->tunnel
.gbp_flags
= value
->u8
;
1463 flow
->tunnel
.ip_tos
= value
->u8
;
1466 flow
->tunnel
.ip_ttl
= value
->u8
;
1468 case MFF_TUN_ERSPAN_VER
:
1469 flow
->tunnel
.erspan_ver
= value
->u8
;
1471 case MFF_TUN_ERSPAN_IDX
:
1472 flow
->tunnel
.erspan_idx
= ntohl(value
->be32
);
1474 case MFF_TUN_ERSPAN_DIR
:
1475 flow
->tunnel
.erspan_dir
= value
->u8
;
1477 case MFF_TUN_ERSPAN_HWID
:
1478 flow
->tunnel
.erspan_hwid
= value
->u8
;
1480 case MFF_TUN_GTPU_FLAGS
:
1481 flow
->tunnel
.gtpu_flags
= value
->u8
;
1483 case MFF_TUN_GTPU_MSGTYPE
:
1484 flow
->tunnel
.gtpu_msgtype
= value
->u8
;
1486 CASE_MFF_TUN_METADATA
:
1487 tun_metadata_write(&flow
->tunnel
, mf
, value
);
1490 flow
->metadata
= value
->be64
;
1494 flow
->in_port
.ofp_port
= u16_to_ofp(ntohs(value
->be16
));
1496 case MFF_IN_PORT_OXM
:
1497 ofputil_port_from_ofp11(value
->be32
, &flow
->in_port
.ofp_port
);
1499 case MFF_ACTSET_OUTPUT
:
1500 ofputil_port_from_ofp11(value
->be32
, &flow
->actset_output
);
1503 case MFF_SKB_PRIORITY
:
1504 flow
->skb_priority
= ntohl(value
->be32
);
1508 flow
->pkt_mark
= ntohl(value
->be32
);
1512 flow
->ct_state
= ntohl(value
->be32
);
1516 flow
->ct_zone
= ntohs(value
->be16
);
1520 flow
->ct_mark
= ntohl(value
->be32
);
1524 flow
->ct_label
= ntoh128(value
->be128
);
1527 case MFF_CT_NW_PROTO
:
1528 flow
->ct_nw_proto
= value
->u8
;
1532 flow
->ct_nw_src
= value
->be32
;
1536 flow
->ct_nw_dst
= value
->be32
;
1539 case MFF_CT_IPV6_SRC
:
1540 flow
->ct_ipv6_src
= value
->ipv6
;
1543 case MFF_CT_IPV6_DST
:
1544 flow
->ct_ipv6_dst
= value
->ipv6
;
1548 flow
->ct_tp_src
= value
->be16
;
1552 flow
->ct_tp_dst
= value
->be16
;
1556 flow
->regs
[mf
->id
- MFF_REG0
] = ntohl(value
->be32
);
1560 flow_set_xreg(flow
, mf
->id
- MFF_XREG0
, ntohll(value
->be64
));
1564 flow_set_xxreg(flow
, mf
->id
- MFF_XXREG0
, ntoh128(value
->be128
));
1568 flow
->dl_src
= value
->mac
;
1572 flow
->dl_dst
= value
->mac
;
1576 flow
->dl_type
= value
->be16
;
1580 flow
->vlans
[0].tci
= value
->be16
;
1581 flow_fix_vlan_tpid(flow
);
1585 flow_set_dl_vlan(flow
, value
->be16
, 0);
1586 flow_fix_vlan_tpid(flow
);
1590 flow_set_vlan_vid(flow
, value
->be16
);
1591 flow_fix_vlan_tpid(flow
);
1594 case MFF_DL_VLAN_PCP
:
1596 flow_set_vlan_pcp(flow
, value
->u8
, 0);
1597 flow_fix_vlan_tpid(flow
);
1600 case MFF_MPLS_LABEL
:
1601 flow_set_mpls_label(flow
, 0, value
->be32
);
1605 flow_set_mpls_tc(flow
, 0, value
->u8
);
1609 flow_set_mpls_bos(flow
, 0, value
->u8
);
1613 flow_set_mpls_ttl(flow
, 0, value
->u8
);
1617 flow
->nw_src
= value
->be32
;
1621 flow
->nw_dst
= value
->be32
;
1625 flow
->ipv6_src
= value
->ipv6
;
1629 flow
->ipv6_dst
= value
->ipv6
;
1632 case MFF_IPV6_LABEL
:
1633 flow
->ipv6_label
= value
->be32
& htonl(IPV6_LABEL_MASK
);
1637 flow
->nw_proto
= value
->u8
;
1641 flow
->nw_tos
&= ~IP_DSCP_MASK
;
1642 flow
->nw_tos
|= value
->u8
& IP_DSCP_MASK
;
1645 case MFF_IP_DSCP_SHIFTED
:
1646 flow
->nw_tos
&= ~IP_DSCP_MASK
;
1647 flow
->nw_tos
|= value
->u8
<< 2;
1651 flow
->nw_tos
&= ~IP_ECN_MASK
;
1652 flow
->nw_tos
|= value
->u8
& IP_ECN_MASK
;
1656 flow
->nw_ttl
= value
->u8
;
1660 flow
->nw_frag
= value
->u8
& FLOW_NW_FRAG_MASK
;
1664 flow
->nw_proto
= ntohs(value
->be16
);
1668 flow
->nw_src
= value
->be32
;
1672 flow
->nw_dst
= value
->be32
;
1677 flow
->arp_sha
= value
->mac
;
1682 flow
->arp_tha
= value
->mac
;
1688 flow
->tp_src
= value
->be16
;
1694 flow
->tp_dst
= value
->be16
;
1698 flow
->tcp_flags
= value
->be16
;
1701 case MFF_ICMPV4_TYPE
:
1702 case MFF_ICMPV6_TYPE
:
1703 flow
->tp_src
= htons(value
->u8
);
1706 case MFF_ICMPV4_CODE
:
1707 case MFF_ICMPV6_CODE
:
1708 flow
->tp_dst
= htons(value
->u8
);
1712 flow
->nd_target
= value
->ipv6
;
1715 case MFF_ND_RESERVED
:
1716 flow
->igmp_group_ip4
= value
->be32
;
1719 case MFF_ND_OPTIONS_TYPE
:
1720 flow
->tcp_flags
= htons(value
->u8
);
1724 flow
->nsh
.flags
= value
->u8
;
1727 flow
->nsh
.ttl
= value
->u8
;
1729 case MFF_NSH_MDTYPE
:
1730 flow
->nsh
.mdtype
= value
->u8
;
1733 flow
->nsh
.np
= value
->u8
;
1736 nsh_path_hdr_set_spi(&flow
->nsh
.path_hdr
, value
->be32
);
1739 nsh_path_hdr_set_si(&flow
->nsh
.path_hdr
, value
->u8
);
1745 flow
->nsh
.context
[mf
->id
- MFF_NSH_C1
] = value
->be32
;
1754 /* Consider each of 'src', 'mask', and 'dst' as if they were arrays of 8*n
1755 * bits. Then, for each 0 <= i < 8 * n such that mask[i] == 1, sets dst[i] =
1758 apply_mask(const uint8_t *src
, const uint8_t *mask
, uint8_t *dst
, size_t n
)
1762 for (i
= 0; i
< n
; i
++) {
1763 dst
[i
] = (src
[i
] & mask
[i
]) | (dst
[i
] & ~mask
[i
]);
1767 /* Sets 'flow' member field described by 'field' to 'value', except that bits
1768 * for which 'mask' has a 0-bit keep their existing values. The caller is
1769 * responsible for ensuring that 'flow' meets 'field''s prerequisites.*/
1771 mf_set_flow_value_masked(const struct mf_field
*field
,
1772 const union mf_value
*value
,
1773 const union mf_value
*mask
,
1778 mf_get_value(field
, flow
, &tmp
);
1779 apply_mask((const uint8_t *) value
, (const uint8_t *) mask
,
1780 (uint8_t *) &tmp
, field
->n_bytes
);
1781 mf_set_flow_value(field
, &tmp
, flow
);
1785 mf_is_tun_metadata(const struct mf_field
*mf
)
1787 return mf
->id
>= MFF_TUN_METADATA0
&&
1788 mf
->id
< MFF_TUN_METADATA0
+ TUN_METADATA_NUM_OPTS
;
1792 mf_is_pipeline_field(const struct mf_field
*mf
)
1798 case MFF_TUN_IPV6_SRC
:
1799 case MFF_TUN_IPV6_DST
:
1801 case MFF_TUN_GBP_ID
:
1802 case MFF_TUN_GBP_FLAGS
:
1803 case MFF_TUN_ERSPAN_VER
:
1804 case MFF_TUN_ERSPAN_IDX
:
1805 case MFF_TUN_ERSPAN_DIR
:
1806 case MFF_TUN_ERSPAN_HWID
:
1807 case MFF_TUN_GTPU_FLAGS
:
1808 case MFF_TUN_GTPU_MSGTYPE
:
1809 CASE_MFF_TUN_METADATA
:
1812 case MFF_IN_PORT_OXM
:
1816 case MFF_PACKET_TYPE
:
1824 case MFF_ACTSET_OUTPUT
:
1825 case MFF_SKB_PRIORITY
:
1831 case MFF_CT_NW_PROTO
:
1834 case MFF_CT_IPV6_SRC
:
1835 case MFF_CT_IPV6_DST
:
1844 case MFF_DL_VLAN_PCP
:
1846 case MFF_MPLS_LABEL
:
1854 case MFF_IPV6_LABEL
:
1857 case MFF_IP_DSCP_SHIFTED
:
1873 case MFF_ICMPV4_TYPE
:
1874 case MFF_ICMPV4_CODE
:
1875 case MFF_ICMPV6_TYPE
:
1876 case MFF_ICMPV6_CODE
:
1880 case MFF_ND_RESERVED
:
1881 case MFF_ND_OPTIONS_TYPE
:
1884 case MFF_NSH_MDTYPE
:
1900 /* Returns true if 'mf' has previously been set in 'flow', false if
1901 * it contains a non-default value.
1903 * The caller is responsible for ensuring that 'flow' meets 'mf''s
1906 mf_is_set(const struct mf_field
*mf
, const struct flow
*flow
)
1908 if (!mf_is_tun_metadata(mf
)) {
1909 union mf_value value
;
1911 mf_get_value(mf
, flow
, &value
);
1912 return !is_all_zeros(&value
, mf
->n_bytes
);
1914 return ULLONG_GET(flow
->tunnel
.metadata
.present
.map
,
1915 mf
->id
- MFF_TUN_METADATA0
);
1919 /* Makes 'match' wildcard field 'mf'.
1921 * The caller is responsible for ensuring that 'match' meets 'mf''s
1924 * If non-NULL, 'err_str' returns a malloc'ed string describing any errors
1925 * with the request or NULL if there is no error. The caller is reponsible
1926 * for freeing the string. */
1928 mf_set_wild(const struct mf_field
*mf
, struct match
*match
, char **err_str
)
1936 match
->flow
.dp_hash
= 0;
1937 match
->wc
.masks
.dp_hash
= 0;
1940 match
->flow
.recirc_id
= 0;
1941 match
->wc
.masks
.recirc_id
= 0;
1943 case MFF_PACKET_TYPE
:
1944 match
->flow
.packet_type
= 0;
1945 match
->wc
.masks
.packet_type
= 0;
1948 match
->flow
.conj_id
= 0;
1949 match
->wc
.masks
.conj_id
= 0;
1952 match_set_tun_id_masked(match
, htonll(0), htonll(0));
1955 match_set_tun_src_masked(match
, htonl(0), htonl(0));
1958 match_set_tun_dst_masked(match
, htonl(0), htonl(0));
1960 case MFF_TUN_IPV6_SRC
:
1961 memset(&match
->wc
.masks
.tunnel
.ipv6_src
, 0,
1962 sizeof match
->wc
.masks
.tunnel
.ipv6_src
);
1963 memset(&match
->flow
.tunnel
.ipv6_src
, 0,
1964 sizeof match
->flow
.tunnel
.ipv6_src
);
1966 case MFF_TUN_IPV6_DST
:
1967 memset(&match
->wc
.masks
.tunnel
.ipv6_dst
, 0,
1968 sizeof match
->wc
.masks
.tunnel
.ipv6_dst
);
1969 memset(&match
->flow
.tunnel
.ipv6_dst
, 0,
1970 sizeof match
->flow
.tunnel
.ipv6_dst
);
1973 match_set_tun_flags_masked(match
, 0, 0);
1975 case MFF_TUN_GBP_ID
:
1976 match_set_tun_gbp_id_masked(match
, 0, 0);
1978 case MFF_TUN_GBP_FLAGS
:
1979 match_set_tun_gbp_flags_masked(match
, 0, 0);
1982 match_set_tun_tos_masked(match
, 0, 0);
1985 match_set_tun_ttl_masked(match
, 0, 0);
1987 case MFF_TUN_ERSPAN_VER
:
1988 match_set_tun_erspan_ver_masked(match
, 0, 0);
1990 case MFF_TUN_ERSPAN_IDX
:
1991 match_set_tun_erspan_idx_masked(match
, 0, 0);
1993 case MFF_TUN_ERSPAN_DIR
:
1994 match_set_tun_erspan_dir_masked(match
, 0, 0);
1996 case MFF_TUN_ERSPAN_HWID
:
1997 match_set_tun_erspan_hwid_masked(match
, 0, 0);
1999 case MFF_TUN_GTPU_FLAGS
:
2000 match_set_tun_gtpu_flags_masked(match
, 0, 0);
2002 case MFF_TUN_GTPU_MSGTYPE
:
2003 match_set_tun_gtpu_msgtype_masked(match
, 0, 0);
2005 CASE_MFF_TUN_METADATA
:
2006 tun_metadata_set_match(mf
, NULL
, NULL
, match
, err_str
);
2010 match_set_metadata_masked(match
, htonll(0), htonll(0));
2014 case MFF_IN_PORT_OXM
:
2015 match
->flow
.in_port
.ofp_port
= 0;
2016 match
->wc
.masks
.in_port
.ofp_port
= 0;
2018 case MFF_ACTSET_OUTPUT
:
2019 match
->flow
.actset_output
= 0;
2020 match
->wc
.masks
.actset_output
= 0;
2023 case MFF_SKB_PRIORITY
:
2024 match
->flow
.skb_priority
= 0;
2025 match
->wc
.masks
.skb_priority
= 0;
2029 match
->flow
.pkt_mark
= 0;
2030 match
->wc
.masks
.pkt_mark
= 0;
2034 match
->flow
.ct_state
= 0;
2035 match
->wc
.masks
.ct_state
= 0;
2039 match
->flow
.ct_zone
= 0;
2040 match
->wc
.masks
.ct_zone
= 0;
2044 match
->flow
.ct_mark
= 0;
2045 match
->wc
.masks
.ct_mark
= 0;
2049 memset(&match
->flow
.ct_label
, 0, sizeof(match
->flow
.ct_label
));
2050 memset(&match
->wc
.masks
.ct_label
, 0, sizeof(match
->wc
.masks
.ct_label
));
2053 case MFF_CT_NW_PROTO
:
2054 match
->flow
.ct_nw_proto
= 0;
2055 match
->wc
.masks
.ct_nw_proto
= 0;
2059 match
->flow
.ct_nw_src
= 0;
2060 match
->wc
.masks
.ct_nw_src
= 0;
2064 match
->flow
.ct_nw_dst
= 0;
2065 match
->wc
.masks
.ct_nw_dst
= 0;
2068 case MFF_CT_IPV6_SRC
:
2069 memset(&match
->flow
.ct_ipv6_src
, 0, sizeof(match
->flow
.ct_ipv6_src
));
2070 WC_UNMASK_FIELD(&match
->wc
, ct_ipv6_src
);
2073 case MFF_CT_IPV6_DST
:
2074 memset(&match
->flow
.ct_ipv6_dst
, 0, sizeof(match
->flow
.ct_ipv6_dst
));
2075 WC_UNMASK_FIELD(&match
->wc
, ct_ipv6_dst
);
2079 match
->flow
.ct_tp_src
= 0;
2080 match
->wc
.masks
.ct_tp_src
= 0;
2084 match
->flow
.ct_tp_dst
= 0;
2085 match
->wc
.masks
.ct_tp_dst
= 0;
2089 match_set_reg_masked(match
, mf
->id
- MFF_REG0
, 0, 0);
2093 match_set_xreg_masked(match
, mf
->id
- MFF_XREG0
, 0, 0);
2097 match_set_xxreg_masked(match
, mf
->id
- MFF_XXREG0
, OVS_U128_ZERO
,
2103 match
->flow
.dl_src
= eth_addr_zero
;
2104 match
->wc
.masks
.dl_src
= eth_addr_zero
;
2108 match
->flow
.dl_dst
= eth_addr_zero
;
2109 match
->wc
.masks
.dl_dst
= eth_addr_zero
;
2113 match
->flow
.dl_type
= htons(0);
2114 match
->wc
.masks
.dl_type
= htons(0);
2118 match_set_dl_tci_masked(match
, htons(0), htons(0));
2123 match_set_any_vid(match
);
2126 case MFF_DL_VLAN_PCP
:
2128 match_set_any_pcp(match
);
2131 case MFF_MPLS_LABEL
:
2132 match_set_any_mpls_label(match
, 0);
2136 match_set_any_mpls_tc(match
, 0);
2140 match_set_any_mpls_bos(match
, 0);
2144 match_set_any_mpls_ttl(match
, 0);
2149 match_set_nw_src_masked(match
, htonl(0), htonl(0));
2154 match_set_nw_dst_masked(match
, htonl(0), htonl(0));
2158 memset(&match
->wc
.masks
.ipv6_src
, 0, sizeof match
->wc
.masks
.ipv6_src
);
2159 memset(&match
->flow
.ipv6_src
, 0, sizeof match
->flow
.ipv6_src
);
2163 memset(&match
->wc
.masks
.ipv6_dst
, 0, sizeof match
->wc
.masks
.ipv6_dst
);
2164 memset(&match
->flow
.ipv6_dst
, 0, sizeof match
->flow
.ipv6_dst
);
2167 case MFF_IPV6_LABEL
:
2168 match
->wc
.masks
.ipv6_label
= htonl(0);
2169 match
->flow
.ipv6_label
= htonl(0);
2173 match
->wc
.masks
.nw_proto
= 0;
2174 match
->flow
.nw_proto
= 0;
2178 case MFF_IP_DSCP_SHIFTED
:
2179 match
->wc
.masks
.nw_tos
&= ~IP_DSCP_MASK
;
2180 match
->flow
.nw_tos
&= ~IP_DSCP_MASK
;
2184 match
->wc
.masks
.nw_tos
&= ~IP_ECN_MASK
;
2185 match
->flow
.nw_tos
&= ~IP_ECN_MASK
;
2189 match
->wc
.masks
.nw_ttl
= 0;
2190 match
->flow
.nw_ttl
= 0;
2194 match
->wc
.masks
.nw_frag
&= ~FLOW_NW_FRAG_MASK
;
2195 match
->flow
.nw_frag
&= ~FLOW_NW_FRAG_MASK
;
2199 match
->wc
.masks
.nw_proto
= 0;
2200 match
->flow
.nw_proto
= 0;
2205 match
->flow
.arp_sha
= eth_addr_zero
;
2206 match
->wc
.masks
.arp_sha
= eth_addr_zero
;
2211 match
->flow
.arp_tha
= eth_addr_zero
;
2212 match
->wc
.masks
.arp_tha
= eth_addr_zero
;
2215 case MFF_ND_RESERVED
:
2216 match
->wc
.masks
.igmp_group_ip4
= htonl(0);
2217 match
->flow
.igmp_group_ip4
= htonl(0);
2223 case MFF_ICMPV4_TYPE
:
2224 case MFF_ICMPV6_TYPE
:
2225 match
->wc
.masks
.tp_src
= htons(0);
2226 match
->flow
.tp_src
= htons(0);
2232 case MFF_ICMPV4_CODE
:
2233 case MFF_ICMPV6_CODE
:
2234 match
->wc
.masks
.tp_dst
= htons(0);
2235 match
->flow
.tp_dst
= htons(0);
2239 case MFF_ND_OPTIONS_TYPE
:
2240 match
->wc
.masks
.tcp_flags
= htons(0);
2241 match
->flow
.tcp_flags
= htons(0);
2245 memset(&match
->wc
.masks
.nd_target
, 0,
2246 sizeof match
->wc
.masks
.nd_target
);
2247 memset(&match
->flow
.nd_target
, 0, sizeof match
->flow
.nd_target
);
2251 MATCH_SET_FIELD_MASKED(match
, nsh
.flags
, 0, 0);
2254 MATCH_SET_FIELD_MASKED(match
, nsh
.ttl
, 0, 0);
2256 case MFF_NSH_MDTYPE
:
2257 MATCH_SET_FIELD_MASKED(match
, nsh
.mdtype
, 0, 0);
2260 MATCH_SET_FIELD_MASKED(match
, nsh
.np
, 0, 0);
2263 match
->wc
.masks
.nsh
.path_hdr
&= ~htonl(NSH_SPI_MASK
);
2264 nsh_path_hdr_set_spi(&match
->flow
.nsh
.path_hdr
, htonl(0));
2267 match
->wc
.masks
.nsh
.path_hdr
&= ~htonl(NSH_SI_MASK
);
2268 nsh_path_hdr_set_si(&match
->flow
.nsh
.path_hdr
, 0);
2274 MATCH_SET_FIELD_MASKED(match
, nsh
.context
[mf
->id
- MFF_NSH_C1
],
2275 htonl(0), htonl(0));
2284 /* Makes 'match' match field 'mf' with the specified 'value' and 'mask'.
2285 * 'value' specifies a value to match and 'mask' specifies a wildcard pattern,
2286 * with a 1-bit indicating that the corresponding value bit must match and a
2287 * 0-bit indicating a don't-care.
2289 * If 'mask' is NULL or points to all-1-bits, then this call is equivalent to
2290 * mf_set_value(mf, value, match). If 'mask' points to all-0-bits, then this
2291 * call is equivalent to mf_set_wild(mf, match).
2293 * 'mask' must be a valid mask for 'mf' (see mf_is_mask_valid()). The caller
2294 * is responsible for ensuring that 'match' meets 'mf''s prerequisites.
2296 * If non-NULL, 'err_str' returns a malloc'ed string describing any errors
2297 * with the request or NULL if there is no error. The caller is reponsible
2298 * for freeing the string.
2300 * Return a set of enum ofputil_protocol bits (as an uint32_t to avoid circular
2301 * dependency on enum ofputil_protocol definition) indicating which OpenFlow
2302 * protocol versions can support this functionality. */
2304 mf_set(const struct mf_field
*mf
,
2305 const union mf_value
*value
, const union mf_value
*mask
,
2306 struct match
*match
, char **err_str
)
2308 if (!mask
|| is_all_ones(mask
, mf
->n_bytes
)) {
2309 mf_set_value(mf
, value
, match
, err_str
);
2310 return mf
->usable_protocols_exact
;
2311 } else if (is_all_zeros(mask
, mf
->n_bytes
) && !mf_is_tun_metadata(mf
)) {
2312 /* Tunnel metadata matches on the existence of the field itself, so
2313 * it still needs to be encoded even if the value is wildcarded. */
2314 mf_set_wild(mf
, match
, err_str
);
2315 return OFPUTIL_P_ANY
;
2322 /* The cases where 'mask' is all-1-bits or all-0-bits were already handled
2323 * above[*], so the code below only needs to work for the remaining cases
2324 * of a nontrivial mask.
2326 * [*] Except where the field is a tunnel metadata field and 'mask' is
2327 * all-0-bits; see above. */
2330 case MFF_CT_NW_PROTO
:
2332 case MFF_PACKET_TYPE
:
2335 case MFF_IN_PORT_OXM
:
2336 case MFF_ACTSET_OUTPUT
:
2337 case MFF_SKB_PRIORITY
:
2340 case MFF_DL_VLAN_PCP
:
2342 case MFF_MPLS_LABEL
:
2349 case MFF_IP_DSCP_SHIFTED
:
2352 case MFF_ICMPV4_TYPE
:
2353 case MFF_ICMPV4_CODE
:
2354 case MFF_ICMPV6_TYPE
:
2355 case MFF_ICMPV6_CODE
:
2356 case MFF_ND_RESERVED
:
2357 case MFF_ND_OPTIONS_TYPE
:
2358 return OFPUTIL_P_NONE
;
2361 match_set_dp_hash_masked(match
, ntohl(value
->be32
), ntohl(mask
->be32
));
2364 match_set_tun_id_masked(match
, value
->be64
, mask
->be64
);
2367 match_set_tun_src_masked(match
, value
->be32
, mask
->be32
);
2370 match_set_tun_dst_masked(match
, value
->be32
, mask
->be32
);
2372 case MFF_TUN_IPV6_SRC
:
2373 match_set_tun_ipv6_src_masked(match
, &value
->ipv6
, &mask
->ipv6
);
2375 case MFF_TUN_IPV6_DST
:
2376 match_set_tun_ipv6_dst_masked(match
, &value
->ipv6
, &mask
->ipv6
);
2379 match_set_tun_flags_masked(match
, ntohs(value
->be16
), ntohs(mask
->be16
));
2381 case MFF_TUN_GBP_ID
:
2382 match_set_tun_gbp_id_masked(match
, value
->be16
, mask
->be16
);
2384 case MFF_TUN_GBP_FLAGS
:
2385 match_set_tun_gbp_flags_masked(match
, value
->u8
, mask
->u8
);
2388 match_set_tun_ttl_masked(match
, value
->u8
, mask
->u8
);
2391 match_set_tun_tos_masked(match
, value
->u8
, mask
->u8
);
2393 case MFF_TUN_ERSPAN_VER
:
2394 match_set_tun_erspan_ver_masked(match
, value
->u8
, mask
->u8
);
2396 case MFF_TUN_ERSPAN_IDX
:
2397 match_set_tun_erspan_idx_masked(match
, ntohl(value
->be32
),
2400 case MFF_TUN_ERSPAN_DIR
:
2401 match_set_tun_erspan_dir_masked(match
, value
->u8
, mask
->u8
);
2403 case MFF_TUN_ERSPAN_HWID
:
2404 match_set_tun_erspan_hwid_masked(match
, value
->u8
, mask
->u8
);
2406 case MFF_TUN_GTPU_FLAGS
:
2407 match_set_tun_gtpu_flags_masked(match
, value
->u8
, mask
->u8
);
2409 case MFF_TUN_GTPU_MSGTYPE
:
2410 match_set_tun_gtpu_msgtype_masked(match
, value
->u8
, mask
->u8
);
2412 CASE_MFF_TUN_METADATA
:
2413 tun_metadata_set_match(mf
, value
, mask
, match
, err_str
);
2417 match_set_metadata_masked(match
, value
->be64
, mask
->be64
);
2421 match_set_reg_masked(match
, mf
->id
- MFF_REG0
,
2422 ntohl(value
->be32
), ntohl(mask
->be32
));
2426 match_set_xreg_masked(match
, mf
->id
- MFF_XREG0
,
2427 ntohll(value
->be64
), ntohll(mask
->be64
));
2431 match_set_xxreg_masked(match
, mf
->id
- MFF_XXREG0
,
2432 ntoh128(value
->be128
), ntoh128(mask
->be128
));
2437 match_set_pkt_mark_masked(match
, ntohl(value
->be32
),
2442 match_set_ct_state_masked(match
, ntohl(value
->be32
), ntohl(mask
->be32
));
2446 match_set_ct_mark_masked(match
, ntohl(value
->be32
), ntohl(mask
->be32
));
2450 match_set_ct_label_masked(match
, ntoh128(value
->be128
),
2451 ntoh128(mask
->be128
));
2455 match_set_ct_nw_src_masked(match
, value
->be32
, mask
->be32
);
2459 match_set_ct_nw_dst_masked(match
, value
->be32
, mask
->be32
);
2462 case MFF_CT_IPV6_SRC
:
2463 match_set_ct_ipv6_src_masked(match
, &value
->ipv6
, &mask
->ipv6
);
2466 case MFF_CT_IPV6_DST
:
2467 match_set_ct_ipv6_dst_masked(match
, &value
->ipv6
, &mask
->ipv6
);
2471 match_set_ct_tp_src_masked(match
, value
->be16
, mask
->be16
);
2475 match_set_ct_tp_dst_masked(match
, value
->be16
, mask
->be16
);
2479 match_set_dl_dst_masked(match
, value
->mac
, mask
->mac
);
2483 match_set_dl_src_masked(match
, value
->mac
, mask
->mac
);
2488 match_set_arp_sha_masked(match
, value
->mac
, mask
->mac
);
2493 match_set_arp_tha_masked(match
, value
->mac
, mask
->mac
);
2497 match_set_dl_tci_masked(match
, value
->be16
, mask
->be16
);
2501 match_set_vlan_vid_masked(match
, value
->be16
, mask
->be16
);
2505 match_set_nw_src_masked(match
, value
->be32
, mask
->be32
);
2509 match_set_nw_dst_masked(match
, value
->be32
, mask
->be32
);
2513 match_set_ipv6_src_masked(match
, &value
->ipv6
, &mask
->ipv6
);
2517 match_set_ipv6_dst_masked(match
, &value
->ipv6
, &mask
->ipv6
);
2520 case MFF_IPV6_LABEL
:
2521 if ((mask
->be32
& htonl(IPV6_LABEL_MASK
)) == htonl(IPV6_LABEL_MASK
)) {
2522 mf_set_value(mf
, value
, match
, err_str
);
2524 match_set_ipv6_label_masked(match
, value
->be32
, mask
->be32
);
2529 match_set_nd_target_masked(match
, &value
->ipv6
, &mask
->ipv6
);
2533 match_set_nw_frag_masked(match
, value
->u8
, mask
->u8
);
2537 match_set_nw_src_masked(match
, value
->be32
, mask
->be32
);
2541 match_set_nw_dst_masked(match
, value
->be32
, mask
->be32
);
2547 match_set_tp_src_masked(match
, value
->be16
, mask
->be16
);
2553 match_set_tp_dst_masked(match
, value
->be16
, mask
->be16
);
2557 match_set_tcp_flags_masked(match
, value
->be16
, mask
->be16
);
2561 MATCH_SET_FIELD_MASKED(match
, nsh
.flags
, value
->u8
, mask
->u8
);
2564 MATCH_SET_FIELD_MASKED(match
, nsh
.ttl
, value
->u8
, mask
->u8
);
2566 case MFF_NSH_MDTYPE
:
2567 MATCH_SET_FIELD_MASKED(match
, nsh
.mdtype
, value
->u8
, mask
->u8
);
2570 MATCH_SET_FIELD_MASKED(match
, nsh
.np
, value
->u8
, mask
->u8
);
2573 match
->wc
.masks
.nsh
.path_hdr
|= mask
->be32
;
2574 nsh_path_hdr_set_spi(&match
->flow
.nsh
.path_hdr
,
2575 value
->be32
& mask
->be32
);
2578 match
->wc
.masks
.nsh
.path_hdr
|= htonl(mask
->u8
);
2579 nsh_path_hdr_set_si(&match
->flow
.nsh
.path_hdr
,
2580 value
->u8
& mask
->u8
);
2586 MATCH_SET_FIELD_MASKED(match
, nsh
.context
[mf
->id
- MFF_NSH_C1
],
2587 value
->be32
, mask
->be32
);
2595 return ((mf
->usable_protocols_bitwise
== mf
->usable_protocols_cidr
2596 || ip_is_cidr(mask
->be32
))
2597 ? mf
->usable_protocols_cidr
2598 : mf
->usable_protocols_bitwise
);
2602 mf_check__(const struct mf_subfield
*sf
, const struct match
*match
,
2606 VLOG_WARN_RL(&rl
, "unknown %s field", type
);
2607 return OFPERR_OFPBAC_BAD_SET_TYPE
;
2608 } else if (!sf
->n_bits
) {
2609 VLOG_WARN_RL(&rl
, "zero bit %s field %s", type
, sf
->field
->name
);
2610 return OFPERR_OFPBAC_BAD_SET_LEN
;
2611 } else if (sf
->ofs
>= sf
->field
->n_bits
) {
2612 VLOG_WARN_RL(&rl
, "bit offset %d exceeds %d-bit width of %s field %s",
2613 sf
->ofs
, sf
->field
->n_bits
, type
, sf
->field
->name
);
2614 return OFPERR_OFPBAC_BAD_SET_LEN
;
2615 } else if (sf
->ofs
+ sf
->n_bits
> sf
->field
->n_bits
) {
2616 VLOG_WARN_RL(&rl
, "bit offset %d and width %d exceeds %d-bit width "
2617 "of %s field %s", sf
->ofs
, sf
->n_bits
,
2618 sf
->field
->n_bits
, type
, sf
->field
->name
);
2619 return OFPERR_OFPBAC_BAD_SET_LEN
;
2620 } else if (match
&& !mf_are_match_prereqs_ok(sf
->field
, match
)) {
2621 VLOG_WARN_RL(&rl
, "%s field %s lacks correct prerequisites",
2622 type
, sf
->field
->name
);
2623 return OFPERR_OFPBAC_MATCH_INCONSISTENT
;
2629 /* Sets all the bits in 'sf' to 1 within 'wc', if 'wc' is nonnull. */
2631 unwildcard_subfield(const struct mf_subfield
*sf
, struct flow_wildcards
*wc
)
2634 union mf_value mask
;
2636 memset(&mask
, 0, sizeof mask
);
2637 bitwise_one(&mask
, sf
->field
->n_bytes
, sf
->ofs
, sf
->n_bits
);
2638 mf_mask_field_masked(sf
->field
, &mask
, wc
);
2642 /* Copies 'src' into 'dst' within 'flow', and sets all the bits in 'src' and
2643 * 'dst' to 1s in 'wc', if 'wc' is nonnull.
2645 * 'src' and 'dst' may overlap. */
2647 mf_subfield_copy(const struct mf_subfield
*src
,
2648 const struct mf_subfield
*dst
,
2649 struct flow
*flow
, struct flow_wildcards
*wc
)
2651 ovs_assert(src
->n_bits
== dst
->n_bits
);
2652 if (mf_are_prereqs_ok(dst
->field
, flow
, wc
)
2653 && mf_are_prereqs_ok(src
->field
, flow
, wc
)) {
2654 unwildcard_subfield(src
, wc
);
2655 unwildcard_subfield(dst
, wc
);
2657 union mf_value src_value
;
2658 union mf_value dst_value
;
2659 mf_get_value(dst
->field
, flow
, &dst_value
);
2660 mf_get_value(src
->field
, flow
, &src_value
);
2661 bitwise_copy(&src_value
, src
->field
->n_bytes
, src
->ofs
,
2662 &dst_value
, dst
->field
->n_bytes
, dst
->ofs
,
2664 mf_set_flow_value(dst
->field
, &dst_value
, flow
);
2668 /* Swaps the bits in 'src' and 'dst' within 'flow', and sets all the bits in
2669 * 'src' and 'dst' to 1s in 'wc', if 'wc' is nonnull.
2671 * 'src' and 'dst' may overlap. */
2673 mf_subfield_swap(const struct mf_subfield
*a
,
2674 const struct mf_subfield
*b
,
2675 struct flow
*flow
, struct flow_wildcards
*wc
)
2677 ovs_assert(a
->n_bits
== b
->n_bits
);
2678 if (mf_are_prereqs_ok(a
->field
, flow
, wc
)
2679 && mf_are_prereqs_ok(b
->field
, flow
, wc
)) {
2680 unwildcard_subfield(a
, wc
);
2681 unwildcard_subfield(b
, wc
);
2683 union mf_value a_value
;
2684 union mf_value b_value
;
2685 mf_get_value(a
->field
, flow
, &a_value
);
2686 mf_get_value(b
->field
, flow
, &b_value
);
2687 union mf_value b2_value
= b_value
;
2689 /* Copy 'a' into 'b'. */
2690 bitwise_copy(&a_value
, a
->field
->n_bytes
, a
->ofs
,
2691 &b_value
, b
->field
->n_bytes
, b
->ofs
,
2693 mf_set_flow_value(b
->field
, &b_value
, flow
);
2695 /* Copy original 'b' into 'a'. */
2696 bitwise_copy(&b2_value
, b
->field
->n_bytes
, b
->ofs
,
2697 &a_value
, a
->field
->n_bytes
, a
->ofs
,
2699 mf_set_flow_value(a
->field
, &a_value
, flow
);
2703 /* Checks whether 'sf' is valid for reading a subfield out of 'flow'. Returns
2704 * 0 if so, otherwise an OpenFlow error code (e.g. as returned by
2707 mf_check_src(const struct mf_subfield
*sf
, const struct match
*match
)
2709 return mf_check__(sf
, match
, "source");
2712 /* Checks whether 'sf' is valid for writing a subfield into 'flow'. Returns 0
2713 * if so, otherwise an OpenFlow error code (e.g. as returned by
2716 mf_check_dst(const struct mf_subfield
*sf
, const struct match
*match
)
2718 int error
= mf_check__(sf
, match
, "destination");
2719 if (!error
&& !sf
->field
->writable
) {
2720 VLOG_WARN_RL(&rl
, "destination field %s is not writable",
2722 return OFPERR_OFPBAC_BAD_SET_ARGUMENT
;
2727 /* Copies the value and wildcard bit pattern for 'mf' from 'match' into the
2728 * 'value' and 'mask', respectively. */
2730 mf_get(const struct mf_field
*mf
, const struct match
*match
,
2731 union mf_value
*value
, union mf_value
*mask
)
2733 mf_get_value(mf
, &match
->flow
, value
);
2734 mf_get_mask(mf
, &match
->wc
, mask
);
2738 mf_from_integer_string(const struct mf_field
*mf
, const char *s
,
2739 uint8_t *valuep
, uint8_t *maskp
)
2742 const char *err_str
= "";
2745 err
= parse_int_string(s
, valuep
, mf
->n_bytes
, &tail
);
2746 if (err
|| (*tail
!= '\0' && *tail
!= '/')) {
2752 err
= parse_int_string(tail
+ 1, maskp
, mf
->n_bytes
, &tail
);
2753 if (err
|| *tail
!= '\0') {
2758 memset(maskp
, 0xff, mf
->n_bytes
);
2764 if (err
== ERANGE
) {
2765 return xasprintf("%s: %s too large for %u-byte field %s",
2766 s
, err_str
, mf
->n_bytes
, mf
->name
);
2768 return xasprintf("%s: bad syntax for %s %s", s
, mf
->name
, err_str
);
2773 mf_from_packet_type_string(const char *s
, ovs_be32
*packet_type
)
2776 const char *err_str
= "";
2780 err_str
= "missing '('";
2784 err
= parse_int_string(s
, (uint8_t *)packet_type
, 2, &tail
);
2790 err_str
= "missing ','";
2794 err
= parse_int_string(s
, ((uint8_t *)packet_type
) + 2, 2, &tail
);
2796 err_str
= "ns_type";
2800 err_str
= "missing ')'";
2807 return xasprintf("%s: bad syntax for packet type %s", s
, err_str
);
2811 mf_from_ethernet_string(const struct mf_field
*mf
, const char *s
,
2812 struct eth_addr
*mac
, struct eth_addr
*mask
)
2816 ovs_assert(mf
->n_bytes
== ETH_ADDR_LEN
);
2819 if (ovs_scan(s
, ETH_ADDR_SCAN_FMT
"%n", ETH_ADDR_SCAN_ARGS(*mac
), &n
)
2820 && n
== strlen(s
)) {
2821 *mask
= eth_addr_exact
;
2826 if (ovs_scan(s
, ETH_ADDR_SCAN_FMT
"/"ETH_ADDR_SCAN_FMT
"%n",
2827 ETH_ADDR_SCAN_ARGS(*mac
), ETH_ADDR_SCAN_ARGS(*mask
), &n
)
2828 && n
== strlen(s
)) {
2832 return xasprintf("%s: invalid Ethernet address", s
);
2836 mf_from_ipv4_string(const struct mf_field
*mf
, const char *s
,
2837 ovs_be32
*ip
, ovs_be32
*mask
)
2839 ovs_assert(mf
->n_bytes
== sizeof *ip
);
2840 return ip_parse_masked(s
, ip
, mask
);
2844 mf_from_ipv6_string(const struct mf_field
*mf
, const char *s
,
2845 struct in6_addr
*ipv6
, struct in6_addr
*mask
)
2847 ovs_assert(mf
->n_bytes
== sizeof *ipv6
);
2848 return ipv6_parse_masked(s
, ipv6
, mask
);
2852 mf_from_ofp_port_string(const struct mf_field
*mf
, const char *s
,
2853 const struct ofputil_port_map
*port_map
,
2854 ovs_be16
*valuep
, ovs_be16
*maskp
)
2858 ovs_assert(mf
->n_bytes
== sizeof(ovs_be16
));
2860 if (ofputil_port_from_string(s
, port_map
, &port
)) {
2861 *valuep
= htons(ofp_to_u16(port
));
2862 *maskp
= OVS_BE16_MAX
;
2865 return xasprintf("%s: invalid or unknown port for %s", s
, mf
->name
);
2869 mf_from_ofp_port_string32(const struct mf_field
*mf
, const char *s
,
2870 const struct ofputil_port_map
*port_map
,
2871 ovs_be32
*valuep
, ovs_be32
*maskp
)
2875 ovs_assert(mf
->n_bytes
== sizeof(ovs_be32
));
2876 if (ofputil_port_from_string(s
, port_map
, &port
)) {
2877 *valuep
= ofputil_port_to_ofp11(port
);
2878 *maskp
= OVS_BE32_MAX
;
2881 return xasprintf("%s: port value out of range for %s", s
, mf
->name
);
2884 struct frag_handling
{
2890 static const struct frag_handling all_frags
[] = {
2891 #define A FLOW_NW_FRAG_ANY
2892 #define L FLOW_NW_FRAG_LATER
2893 /* name mask value */
2896 { "first", A
|L
, A
},
2897 { "later", A
|L
, A
|L
},
2902 { "not_later", L
, 0 },
2909 mf_from_frag_string(const char *s
, uint8_t *valuep
, uint8_t *maskp
)
2911 const struct frag_handling
*h
;
2913 for (h
= all_frags
; h
< &all_frags
[ARRAY_SIZE(all_frags
)]; h
++) {
2914 if (!strcasecmp(s
, h
->name
)) {
2915 /* We force the upper bits of the mask on to make mf_parse_value()
2916 * happy (otherwise it will never think it's an exact match.) */
2917 *maskp
= h
->mask
| ~FLOW_NW_FRAG_MASK
;
2923 return xasprintf("%s: unknown fragment type (valid types are \"no\", "
2924 "\"yes\", \"first\", \"later\", \"not_later\"", s
);
2928 parse_mf_flags(const char *s
, const char *(*bit_to_string
)(uint32_t),
2929 const char *field_name
, ovs_be16
*flagsp
, ovs_be16 allowed
,
2934 uint32_t flags
, mask
;
2936 err
= parse_flags(s
, bit_to_string
, '\0', field_name
, &err_str
,
2937 &flags
, ntohs(allowed
), maskp
? &mask
: NULL
);
2942 *flagsp
= htons(flags
);
2944 *maskp
= htons(mask
);
2951 mf_from_tcp_flags_string(const char *s
, ovs_be16
*flagsp
, ovs_be16
*maskp
)
2953 return parse_mf_flags(s
, packet_tcp_flag_to_string
, "TCP", flagsp
,
2954 TCP_FLAGS_BE16(OVS_BE16_MAX
), maskp
);
2958 mf_from_tun_flags_string(const char *s
, ovs_be16
*flagsp
, ovs_be16
*maskp
)
2960 return parse_mf_flags(s
, flow_tun_flag_to_string
, "tunnel", flagsp
,
2961 htons(FLOW_TNL_PUB_F_MASK
), maskp
);
2965 mf_from_ct_state_string(const char *s
, ovs_be32
*flagsp
, ovs_be32
*maskp
)
2969 uint32_t flags
, mask
;
2971 err
= parse_flags(s
, ct_state_to_string
, '\0', "ct_state", &err_str
,
2972 &flags
, CS_SUPPORTED_MASK
, maskp
? &mask
: NULL
);
2977 *flagsp
= htonl(flags
);
2979 *maskp
= htonl(mask
);
2985 /* Parses 's', a string value for field 'mf', into 'value' and 'mask'. Returns
2986 * NULL if successful, otherwise a malloc()'d string describing the error. */
2988 mf_parse(const struct mf_field
*mf
, const char *s
,
2989 const struct ofputil_port_map
*port_map
,
2990 union mf_value
*value
, union mf_value
*mask
)
2994 if (!strcmp(s
, "*")) {
2995 memset(value
, 0, mf
->n_bytes
);
2996 memset(mask
, 0, mf
->n_bytes
);
3000 switch (mf
->string
) {
3002 case MFS_HEXADECIMAL
:
3003 error
= mf_from_integer_string(mf
, s
,
3004 (uint8_t *) value
, (uint8_t *) mask
);
3008 ovs_assert(mf
->n_bytes
== sizeof(ovs_be32
));
3009 error
= mf_from_ct_state_string(s
, &value
->be32
, &mask
->be32
);
3013 error
= mf_from_ethernet_string(mf
, s
, &value
->mac
, &mask
->mac
);
3017 error
= mf_from_ipv4_string(mf
, s
, &value
->be32
, &mask
->be32
);
3021 error
= mf_from_ipv6_string(mf
, s
, &value
->ipv6
, &mask
->ipv6
);
3025 error
= mf_from_ofp_port_string(mf
, s
, port_map
,
3026 &value
->be16
, &mask
->be16
);
3029 case MFS_OFP_PORT_OXM
:
3030 error
= mf_from_ofp_port_string32(mf
, s
, port_map
,
3031 &value
->be32
, &mask
->be32
);
3035 error
= mf_from_frag_string(s
, &value
->u8
, &mask
->u8
);
3039 ovs_assert(mf
->n_bytes
== sizeof(ovs_be16
));
3040 error
= mf_from_tun_flags_string(s
, &value
->be16
, &mask
->be16
);
3044 ovs_assert(mf
->n_bytes
== sizeof(ovs_be16
));
3045 error
= mf_from_tcp_flags_string(s
, &value
->be16
, &mask
->be16
);
3048 case MFS_PACKET_TYPE
:
3049 ovs_assert(mf
->n_bytes
== sizeof(ovs_be32
));
3050 error
= mf_from_packet_type_string(s
, &value
->be32
);
3051 mask
->be32
= OVS_BE32_MAX
;
3058 if (!error
&& !mf_is_mask_valid(mf
, mask
)) {
3059 error
= xasprintf("%s: invalid mask for field %s", s
, mf
->name
);
3064 /* Parses 's', a string value for field 'mf', into 'value'. Returns NULL if
3065 * successful, otherwise a malloc()'d string describing the error. */
3067 mf_parse_value(const struct mf_field
*mf
, const char *s
,
3068 const struct ofputil_port_map
*port_map
, union mf_value
*value
)
3070 union mf_value mask
;
3073 error
= mf_parse(mf
, s
, port_map
, value
, &mask
);
3078 if (!is_all_ones((const uint8_t *) &mask
, mf
->n_bytes
)) {
3079 return xasprintf("%s: wildcards not allowed here", s
);
3085 mf_format_integer_string(const struct mf_field
*mf
, const uint8_t *valuep
,
3086 const uint8_t *maskp
, struct ds
*s
)
3088 if (mf
->string
== MFS_HEXADECIMAL
) {
3089 ds_put_hex(s
, valuep
, mf
->n_bytes
);
3091 unsigned long long int integer
= 0;
3094 ovs_assert(mf
->n_bytes
<= 8);
3095 for (i
= 0; i
< mf
->n_bytes
; i
++) {
3096 integer
= (integer
<< 8) | valuep
[i
];
3098 ds_put_format(s
, "%lld", integer
);
3102 /* I guess we could write the mask in decimal for MFS_DECIMAL but I'm
3103 * not sure that that a bit-mask written in decimal is ever easier to
3104 * understand than the same bit-mask written in hexadecimal. */
3105 ds_put_char(s
, '/');
3106 ds_put_hex(s
, maskp
, mf
->n_bytes
);
3111 mf_format_frag_string(uint8_t value
, uint8_t mask
, struct ds
*s
)
3113 const struct frag_handling
*h
;
3115 mask
&= FLOW_NW_FRAG_MASK
;
3118 for (h
= all_frags
; h
< &all_frags
[ARRAY_SIZE(all_frags
)]; h
++) {
3119 if (value
== h
->value
&& mask
== h
->mask
) {
3120 ds_put_cstr(s
, h
->name
);
3124 ds_put_cstr(s
, "<error>");
3128 mf_format_tnl_flags_string(ovs_be16 value
, ovs_be16 mask
, struct ds
*s
)
3130 format_flags_masked(s
, NULL
, flow_tun_flag_to_string
, ntohs(value
),
3131 ntohs(mask
) & FLOW_TNL_PUB_F_MASK
, FLOW_TNL_PUB_F_MASK
);
3135 mf_format_tcp_flags_string(ovs_be16 value
, ovs_be16 mask
, struct ds
*s
)
3137 format_flags_masked(s
, NULL
, packet_tcp_flag_to_string
, ntohs(value
),
3138 TCP_FLAGS(mask
), TCP_FLAGS(OVS_BE16_MAX
));
3142 mf_format_ct_state_string(ovs_be32 value
, ovs_be32 mask
, struct ds
*s
)
3144 format_flags_masked(s
, NULL
, ct_state_to_string
, ntohl(value
),
3145 ntohl(mask
), UINT16_MAX
);
3149 mf_format_packet_type_string(ovs_be32 value
, ovs_be32 mask
, struct ds
*s
)
3151 format_packet_type_masked(s
, value
, mask
);
3154 /* Appends to 's' a string representation of field 'mf' whose value is in
3155 * 'value' and 'mask'. 'mask' may be NULL to indicate an exact match. */
3157 mf_format(const struct mf_field
*mf
,
3158 const union mf_value
*value
, const union mf_value
*mask
,
3159 const struct ofputil_port_map
*port_map
,
3163 if (is_all_zeros(mask
, mf
->n_bytes
)) {
3164 ds_put_cstr(s
, "ANY");
3166 } else if (is_all_ones(mask
, mf
->n_bytes
)) {
3171 switch (mf
->string
) {
3172 case MFS_OFP_PORT_OXM
:
3175 ofputil_port_from_ofp11(value
->be32
, &port
);
3176 ofputil_format_port(port
, port_map
, s
);
3182 ofputil_format_port(u16_to_ofp(ntohs(value
->be16
)), port_map
, s
);
3187 case MFS_HEXADECIMAL
:
3188 mf_format_integer_string(mf
, (uint8_t *) value
, (uint8_t *) mask
, s
);
3192 mf_format_ct_state_string(value
->be32
,
3193 mask
? mask
->be32
: OVS_BE32_MAX
, s
);
3197 eth_format_masked(value
->mac
, mask
? &mask
->mac
: NULL
, s
);
3201 ip_format_masked(value
->be32
, mask
? mask
->be32
: OVS_BE32_MAX
, s
);
3205 ipv6_format_masked(&value
->ipv6
, mask
? &mask
->ipv6
: NULL
, s
);
3209 mf_format_frag_string(value
->u8
, mask
? mask
->u8
: UINT8_MAX
, s
);
3213 mf_format_tnl_flags_string(value
->be16
,
3214 mask
? mask
->be16
: OVS_BE16_MAX
, s
);
3218 mf_format_tcp_flags_string(value
->be16
,
3219 mask
? mask
->be16
: OVS_BE16_MAX
, s
);
3222 case MFS_PACKET_TYPE
:
3223 mf_format_packet_type_string(value
->be32
,
3224 mask
? mask
->be32
: OVS_BE32_MAX
, s
);
3232 /* Makes subfield 'sf' within 'flow' exactly match the 'sf->n_bits'
3233 * least-significant bits in 'x'.
3236 mf_write_subfield_flow(const struct mf_subfield
*sf
,
3237 const union mf_subvalue
*x
, struct flow
*flow
)
3239 const struct mf_field
*field
= sf
->field
;
3240 union mf_value value
;
3242 mf_get_value(field
, flow
, &value
);
3243 bitwise_copy(x
, sizeof *x
, 0, &value
, field
->n_bytes
,
3244 sf
->ofs
, sf
->n_bits
);
3245 mf_set_flow_value(field
, &value
, flow
);
3248 /* Makes subfield 'sf' within 'match' exactly match the 'sf->n_bits'
3249 * least-significant bits in 'x'.
3252 mf_write_subfield(const struct mf_subfield
*sf
, const union mf_subvalue
*x
,
3253 struct match
*match
)
3255 const struct mf_field
*field
= sf
->field
;
3256 union mf_value value
, mask
;
3258 mf_get(field
, match
, &value
, &mask
);
3259 bitwise_copy(x
, sizeof *x
, 0, &value
, field
->n_bytes
, sf
->ofs
, sf
->n_bits
);
3260 bitwise_one ( &mask
, field
->n_bytes
, sf
->ofs
, sf
->n_bits
);
3261 mf_set(field
, &value
, &mask
, match
, NULL
);
3265 mf_write_subfield_value(const struct mf_subfield
*sf
, const void *src
,
3266 struct match
*match
)
3268 const struct mf_field
*field
= sf
->field
;
3269 union mf_value value
, mask
;
3270 unsigned int size
= DIV_ROUND_UP(sf
->n_bits
, 8);
3272 mf_get(field
, match
, &value
, &mask
);
3273 bitwise_copy(src
, size
, 0, &value
, field
->n_bytes
, sf
->ofs
, sf
->n_bits
);
3274 bitwise_one ( &mask
, field
->n_bytes
, sf
->ofs
, sf
->n_bits
);
3275 mf_set(field
, &value
, &mask
, match
, NULL
);
3278 /* 'v' and 'm' correspond to values of 'field'. This function copies them into
3279 * 'match' in the correspond positions. */
3281 mf_mask_subfield(const struct mf_field
*field
,
3282 const union mf_subvalue
*v
,
3283 const union mf_subvalue
*m
,
3284 struct match
*match
)
3286 union mf_value value
, mask
;
3288 mf_get(field
, match
, &value
, &mask
);
3289 bitwise_copy(v
, sizeof *v
, 0, &value
, field
->n_bytes
, 0, field
->n_bits
);
3290 bitwise_copy(m
, sizeof *m
, 0, &mask
, field
->n_bytes
, 0, field
->n_bits
);
3291 mf_set(field
, &value
, &mask
, match
, NULL
);
3294 /* Initializes 'x' to the value of 'sf' within 'flow'. 'sf' must be valid for
3295 * reading 'flow', e.g. as checked by mf_check_src(). */
3297 mf_read_subfield(const struct mf_subfield
*sf
, const struct flow
*flow
,
3298 union mf_subvalue
*x
)
3300 union mf_value value
;
3302 mf_get_value(sf
->field
, flow
, &value
);
3304 memset(x
, 0, sizeof *x
);
3305 bitwise_copy(&value
, sf
->field
->n_bytes
, sf
->ofs
,
3310 /* Returns the value of 'sf' within 'flow'. 'sf' must be valid for reading
3311 * 'flow', e.g. as checked by mf_check_src() and sf->n_bits must be 64 or
3314 mf_get_subfield(const struct mf_subfield
*sf
, const struct flow
*flow
)
3316 union mf_value value
;
3318 mf_get_value(sf
->field
, flow
, &value
);
3319 return bitwise_get(&value
, sf
->field
->n_bytes
, sf
->ofs
, sf
->n_bits
);
3323 mf_format_subvalue(const union mf_subvalue
*subvalue
, struct ds
*s
)
3325 ds_put_hex(s
, subvalue
->u8
, sizeof subvalue
->u8
);
3329 field_array_set(enum mf_field_id id
, const union mf_value
*value
,
3330 struct field_array
*fa
)
3332 size_t i
, offset
= 0;
3334 ovs_assert(id
< MFF_N_IDS
);
3336 /* Find the spot for 'id'. */
3337 BITMAP_FOR_EACH_1 (i
, id
, fa
->used
.bm
) {
3338 offset
+= mf_from_id(i
)->n_bytes
;
3341 size_t value_size
= mf_from_id(id
)->n_bytes
;
3343 /* make room if necessary. */
3344 if (!bitmap_is_set(fa
->used
.bm
, id
)) {
3345 fa
->values
= xrealloc(fa
->values
, fa
->values_size
+ value_size
);
3346 /* Move remainder forward, if any. */
3347 if (offset
< fa
->values_size
) {
3348 memmove(fa
->values
+ offset
+ value_size
, fa
->values
+ offset
,
3349 fa
->values_size
- offset
);
3351 fa
->values_size
+= value_size
;
3353 bitmap_set1(fa
->used
.bm
, id
);
3355 memcpy(fa
->values
+ offset
, value
, value_size
);
3358 /* A wrapper for variable length mf_fields that is maintained by
3359 * struct vl_mff_map.*/
3360 struct vl_mf_field
{
3362 struct ovs_refcount ref_cnt
;
3363 struct cmap_node cmap_node
; /* In ofproto->vl_mff_map->cmap. */
3366 static inline uint32_t
3367 mf_field_hash(uint32_t key
)
3369 return hash_int(key
, 0);
3373 vmf_delete(struct vl_mf_field
*vmf
)
3375 if (ovs_refcount_unref(&vmf
->ref_cnt
) == 1) {
3376 /* Postpone as this function is typically called immediately
3377 * after removing from cmap. */
3378 ovsrcu_postpone(free
, vmf
);
3381 "Attempted to delete VMF %s but refcount is nonzero!",
3387 mf_vl_mff_map_clear(struct vl_mff_map
*vl_mff_map
, bool force
)
3388 OVS_REQUIRES(vl_mff_map
->mutex
)
3390 struct vl_mf_field
*vmf
;
3393 CMAP_FOR_EACH (vmf
, cmap_node
, &vl_mff_map
->cmap
) {
3394 if (ovs_refcount_read(&vmf
->ref_cnt
) != 1) {
3395 return OFPERR_NXTTMFC_INVALID_TLV_DEL
;
3400 CMAP_FOR_EACH (vmf
, cmap_node
, &vl_mff_map
->cmap
) {
3401 cmap_remove(&vl_mff_map
->cmap
, &vmf
->cmap_node
,
3402 mf_field_hash(vmf
->mf
.id
));
3409 static struct vl_mf_field
*
3410 mf_get_vl_mff__(uint32_t id
, const struct vl_mff_map
*vl_mff_map
)
3412 struct vl_mf_field
*vmf
;
3414 CMAP_FOR_EACH_WITH_HASH (vmf
, cmap_node
, mf_field_hash(id
),
3415 &vl_mff_map
->cmap
) {
3416 if (vmf
->mf
.id
== id
) {
3424 /* If 'mff' is a variable length field, looks up 'vl_mff_map', returns a
3425 * pointer to the variable length meta-flow field corresponding to 'mff'.
3426 * Returns NULL if no mapping is existed for 'mff'. */
3427 const struct mf_field
*
3428 mf_get_vl_mff(const struct mf_field
*mff
,
3429 const struct vl_mff_map
*vl_mff_map
)
3431 if (mff
&& mff
->variable_len
&& vl_mff_map
) {
3432 return &mf_get_vl_mff__(mff
->id
, vl_mff_map
)->mf
;
3439 mf_vl_mff_map_del(struct vl_mff_map
*vl_mff_map
,
3440 const struct ofputil_tlv_table_mod
*ttm
, bool force
)
3441 OVS_REQUIRES(vl_mff_map
->mutex
)
3443 struct ofputil_tlv_map
*tlv_map
;
3444 struct vl_mf_field
*vmf
;
3448 LIST_FOR_EACH (tlv_map
, list_node
, &ttm
->mappings
) {
3449 idx
= MFF_TUN_METADATA0
+ tlv_map
->index
;
3450 if (idx
>= MFF_TUN_METADATA0
+ TUN_METADATA_NUM_OPTS
) {
3451 return OFPERR_NXTTMFC_BAD_FIELD_IDX
;
3454 vmf
= mf_get_vl_mff__(idx
, vl_mff_map
);
3455 if (vmf
&& ovs_refcount_read(&vmf
->ref_cnt
) != 1) {
3456 return OFPERR_NXTTMFC_INVALID_TLV_DEL
;
3461 LIST_FOR_EACH (tlv_map
, list_node
, &ttm
->mappings
) {
3462 idx
= MFF_TUN_METADATA0
+ tlv_map
->index
;
3463 if (idx
>= MFF_TUN_METADATA0
+ TUN_METADATA_NUM_OPTS
) {
3464 return OFPERR_NXTTMFC_BAD_FIELD_IDX
;
3467 vmf
= mf_get_vl_mff__(idx
, vl_mff_map
);
3469 cmap_remove(&vl_mff_map
->cmap
, &vmf
->cmap_node
,
3470 mf_field_hash(idx
));
3479 mf_vl_mff_map_add(struct vl_mff_map
*vl_mff_map
,
3480 const struct ofputil_tlv_table_mod
*ttm
)
3481 OVS_REQUIRES(vl_mff_map
->mutex
)
3483 struct ofputil_tlv_map
*tlv_map
;
3484 struct vl_mf_field
*vmf
;
3487 LIST_FOR_EACH (tlv_map
, list_node
, &ttm
->mappings
) {
3488 idx
= MFF_TUN_METADATA0
+ tlv_map
->index
;
3489 if (idx
>= MFF_TUN_METADATA0
+ TUN_METADATA_NUM_OPTS
) {
3490 return OFPERR_NXTTMFC_BAD_FIELD_IDX
;
3493 vmf
= xmalloc(sizeof *vmf
);
3494 vmf
->mf
= mf_fields
[idx
];
3495 vmf
->mf
.n_bytes
= tlv_map
->option_len
;
3496 vmf
->mf
.n_bits
= tlv_map
->option_len
* 8;
3497 vmf
->mf
.mapped
= true;
3498 ovs_refcount_init(&vmf
->ref_cnt
);
3500 cmap_insert(&vl_mff_map
->cmap
, &vmf
->cmap_node
,
3501 mf_field_hash(idx
));
3507 /* Updates the tun_metadata mf_field in 'vl_mff_map' according to 'ttm'.
3508 * This function must be invoked after tun_metadata_table_mod().
3509 * Returns OFPERR_NXTTMFC_BAD_FIELD_IDX, if the index for the vl_mf_field is
3511 * Returns OFPERR_NXTTMFC_INVALID_TLV_DEL, if 'ttm' tries to delete an
3512 * vl_mf_field that is still used by any active flow.*/
3514 mf_vl_mff_map_mod_from_tun_metadata(struct vl_mff_map
*vl_mff_map
,
3515 const struct ofputil_tlv_table_mod
*ttm
)
3516 OVS_REQUIRES(vl_mff_map
->mutex
)
3518 switch (ttm
->command
) {
3520 return mf_vl_mff_map_add(vl_mff_map
, ttm
);
3523 return mf_vl_mff_map_del(vl_mff_map
, ttm
, false);
3526 return mf_vl_mff_map_clear(vl_mff_map
, false);
3535 /* Returns true if a variable length meta-flow field 'mff' is not mapped in
3536 * the 'vl_mff_map'. */
3538 mf_vl_mff_invalid(const struct mf_field
*mff
, const struct vl_mff_map
*map
)
3540 return map
&& mff
&& mff
->variable_len
&& !mff
->mapped
;
3544 mf_vl_mff_set_tlv_bitmap(const struct mf_field
*mff
, uint64_t *tlv_bitmap
)
3546 if (mff
&& mff
->mapped
) {
3547 ovs_assert(mf_is_tun_metadata(mff
));
3548 ULLONG_SET1(*tlv_bitmap
, mff
->id
- MFF_TUN_METADATA0
);
3553 mf_vl_mff_ref_cnt_mod(const struct vl_mff_map
*map
, uint64_t tlv_bitmap
,
3556 struct vl_mf_field
*vmf
;
3560 ULLONG_FOR_EACH_1 (i
, tlv_bitmap
) {
3561 vmf
= mf_get_vl_mff__(i
+ MFF_TUN_METADATA0
, map
);
3564 ovs_refcount_ref(&vmf
->ref_cnt
);
3566 ovs_refcount_unref(&vmf
->ref_cnt
);
3569 VLOG_WARN("Invalid TLV index %d.", i
);
3576 mf_vl_mff_ref(const struct vl_mff_map
*map
, uint64_t tlv_bitmap
)
3578 mf_vl_mff_ref_cnt_mod(map
, tlv_bitmap
, true);
3582 mf_vl_mff_unref(const struct vl_mff_map
*map
, uint64_t tlv_bitmap
)
3584 mf_vl_mff_ref_cnt_mod(map
, tlv_bitmap
, false);
3588 mf_vl_mff_nx_pull_header(struct ofpbuf
*b
, const struct vl_mff_map
*vl_mff_map
,
3589 const struct mf_field
**field
, bool *masked
,
3590 uint64_t *tlv_bitmap
)
3592 enum ofperr error
= nx_pull_header(b
, vl_mff_map
, field
, masked
);
3597 mf_vl_mff_set_tlv_bitmap(*field
, tlv_bitmap
);
3602 mf_vl_mff_nx_pull_entry(struct ofpbuf
*b
, const struct vl_mff_map
*vl_mff_map
,
3603 const struct mf_field
**field
, union mf_value
*value
,
3604 union mf_value
*mask
, uint64_t *tlv_bitmap
)
3606 enum ofperr error
= nx_pull_entry(b
, vl_mff_map
, field
, value
, mask
, true);
3611 mf_vl_mff_set_tlv_bitmap(*field
, tlv_bitmap
);
3616 mf_vl_mff_mf_from_nxm_header(uint32_t header
,
3617 const struct vl_mff_map
*vl_mff_map
,
3618 const struct mf_field
**field
,
3619 uint64_t *tlv_bitmap
)
3621 *field
= mf_from_nxm_header(header
, vl_mff_map
);
3623 return OFPERR_OFPBAC_BAD_SET_TYPE
;
3624 } else if (mf_vl_mff_invalid(*field
, vl_mff_map
)) {
3625 return OFPERR_NXFMFC_INVALID_TLV_FIELD
;
3628 mf_vl_mff_set_tlv_bitmap(*field
, tlv_bitmap
);
3632 /* Returns true if the 1-bits in 'super' are a superset of the 1-bits in 'sub',
3633 * false otherwise. */
3635 mf_bitmap_is_superset(const struct mf_bitmap
*super
,
3636 const struct mf_bitmap
*sub
)
3638 return bitmap_is_superset(super
->bm
, sub
->bm
, MFF_N_IDS
);
3641 /* Returns the bitwise-and of 'a' and 'b'. */
3643 mf_bitmap_and(struct mf_bitmap a
, struct mf_bitmap b
)
3645 bitmap_and(a
.bm
, b
.bm
, MFF_N_IDS
);
3649 /* Returns the bitwise-or of 'a' and 'b'. */
3651 mf_bitmap_or(struct mf_bitmap a
, struct mf_bitmap b
)
3653 bitmap_or(a
.bm
, b
.bm
, MFF_N_IDS
);
3657 /* Returns the bitwise-not of 'x'. */
3659 mf_bitmap_not(struct mf_bitmap x
)
3661 bitmap_not(x
.bm
, MFF_N_IDS
);