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 "openvswitch/ofp-util.h"
30 #include "ovs-atomic.h"
32 #include "ovs-thread.h"
35 #include "openvswitch/shash.h"
36 #include "socket-util.h"
37 #include "tun-metadata.h"
38 #include "unaligned.h"
40 #include "openvswitch/ofp-errors.h"
41 #include "openvswitch/vlog.h"
42 #include "vl-mff-map.h"
44 VLOG_DEFINE_THIS_MODULE(meta_flow
);
46 #define FLOW_U32OFS(FIELD) \
47 offsetof(struct flow, FIELD) % 4 ? -1 : offsetof(struct flow, FIELD) / 4
49 #define MF_FIELD_SIZES(MEMBER) \
50 sizeof ((union mf_value *)0)->MEMBER, \
51 8 * sizeof ((union mf_value *)0)->MEMBER
53 extern const struct mf_field mf_fields
[MFF_N_IDS
]; /* Silence a warning. */
55 const struct mf_field mf_fields
[MFF_N_IDS
] = {
56 #include "meta-flow.inc"
59 /* Maps from an mf_field's 'name' or 'extra_name' to the mf_field. */
60 static struct shash mf_by_name
;
62 /* Rate limit for parse errors. These always indicate a bug in an OpenFlow
63 * controller and so there's not much point in showing a lot of them. */
64 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
66 #define MF_VALUE_EXACT_8 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
67 #define MF_VALUE_EXACT_16 MF_VALUE_EXACT_8, MF_VALUE_EXACT_8
68 #define MF_VALUE_EXACT_32 MF_VALUE_EXACT_16, MF_VALUE_EXACT_16
69 #define MF_VALUE_EXACT_64 MF_VALUE_EXACT_32, MF_VALUE_EXACT_32
70 #define MF_VALUE_EXACT_128 MF_VALUE_EXACT_64, MF_VALUE_EXACT_64
71 #define MF_VALUE_EXACT_INITIALIZER { .tun_metadata = { MF_VALUE_EXACT_128 } }
73 const union mf_value exact_match_mask
= MF_VALUE_EXACT_INITIALIZER
;
75 static void nxm_init(void);
77 /* Returns the field with the given 'name', or a null pointer if no field has
79 const struct mf_field
*
80 mf_from_name(const char *name
)
83 return shash_find_data(&mf_by_name
, name
);
86 /* Returns the field with the given 'name' (which is 'len' bytes long), or a
87 * null pointer if no field has that name. */
88 const struct mf_field
*
89 mf_from_name_len(const char *name
, size_t len
)
93 struct shash_node
*node
= shash_find_len(&mf_by_name
, name
, len
);
94 return node
? node
->data
: NULL
;
102 shash_init(&mf_by_name
);
103 for (i
= 0; i
< MFF_N_IDS
; i
++) {
104 const struct mf_field
*mf
= &mf_fields
[i
];
106 ovs_assert(mf
->id
== i
); /* Fields must be in the enum order. */
108 shash_add_once(&mf_by_name
, mf
->name
, mf
);
109 if (mf
->extra_name
) {
110 shash_add_once(&mf_by_name
, mf
->extra_name
, mf
);
118 static pthread_once_t once
= PTHREAD_ONCE_INIT
;
119 pthread_once(&once
, nxm_do_init
);
122 /* Consider the two value/mask pairs 'a_value/a_mask' and 'b_value/b_mask' as
123 * restrictions on a field's value. Then, this function initializes
124 * 'dst_value/dst_mask' such that it combines the restrictions of both pairs.
125 * This is not always possible, i.e. if one pair insists on a value of 0 in
126 * some bit and the other pair insists on a value of 1 in that bit. This
127 * function returns false in a case where the combined restriction is
128 * impossible (in which case 'dst_value/dst_mask' is not fully initialized),
131 * (As usually true for value/mask pairs in OVS, any 1-bit in a value must have
132 * a corresponding 1-bit in its mask.) */
134 mf_subvalue_intersect(const union mf_subvalue
*a_value
,
135 const union mf_subvalue
*a_mask
,
136 const union mf_subvalue
*b_value
,
137 const union mf_subvalue
*b_mask
,
138 union mf_subvalue
*dst_value
,
139 union mf_subvalue
*dst_mask
)
141 for (int i
= 0; i
< ARRAY_SIZE(a_value
->be64
); i
++) {
142 ovs_be64 av
= a_value
->be64
[i
];
143 ovs_be64 am
= a_mask
->be64
[i
];
144 ovs_be64 bv
= b_value
->be64
[i
];
145 ovs_be64 bm
= b_mask
->be64
[i
];
146 ovs_be64
*dv
= &dst_value
->be64
[i
];
147 ovs_be64
*dm
= &dst_mask
->be64
[i
];
149 if ((av
^ bv
) & (am
& bm
)) {
158 /* Returns the "number of bits" in 'v', e.g. 1 if only the lowest-order bit is
159 * set, 2 if the second-lowest-order bit is set, and so on. */
161 mf_subvalue_width(const union mf_subvalue
*v
)
163 return 1 + bitwise_rscan(v
, sizeof *v
, true, sizeof *v
* 8 - 1, -1);
166 /* For positive 'n', shifts the bits in 'value' 'n' bits to the left, and for
167 * negative 'n', shifts the bits '-n' bits to the right. */
169 mf_subvalue_shift(union mf_subvalue
*value
, int n
)
172 union mf_subvalue tmp
;
173 memset(&tmp
, 0, sizeof tmp
);
175 if (n
> 0 && n
< 8 * sizeof tmp
) {
176 bitwise_copy(value
, sizeof *value
, 0,
179 } else if (n
< 0 && n
> -8 * sizeof tmp
) {
180 bitwise_copy(value
, sizeof *value
, -n
,
188 /* Appends a formatted representation of 'sv' to 's'. */
190 mf_subvalue_format(const union mf_subvalue
*sv
, struct ds
*s
)
192 ds_put_hex(s
, sv
, sizeof *sv
);
195 /* Returns true if 'wc' wildcards all the bits in field 'mf', false if 'wc'
196 * specifies at least one bit in the field.
198 * The caller is responsible for ensuring that 'wc' corresponds to a flow that
199 * meets 'mf''s prerequisites. */
201 mf_is_all_wild(const struct mf_field
*mf
, const struct flow_wildcards
*wc
)
205 return !wc
->masks
.dp_hash
;
207 return !wc
->masks
.recirc_id
;
209 return !wc
->masks
.conj_id
;
211 return !wc
->masks
.tunnel
.ip_src
;
213 return !wc
->masks
.tunnel
.ip_dst
;
214 case MFF_TUN_IPV6_SRC
:
215 return ipv6_mask_is_any(&wc
->masks
.tunnel
.ipv6_src
);
216 case MFF_TUN_IPV6_DST
:
217 return ipv6_mask_is_any(&wc
->masks
.tunnel
.ipv6_dst
);
219 return !wc
->masks
.tunnel
.tun_id
;
221 return !wc
->masks
.tunnel
.ip_tos
;
223 return !wc
->masks
.tunnel
.ip_ttl
;
225 return !(wc
->masks
.tunnel
.flags
& FLOW_TNL_PUB_F_MASK
);
227 return !wc
->masks
.tunnel
.gbp_id
;
228 case MFF_TUN_GBP_FLAGS
:
229 return !wc
->masks
.tunnel
.gbp_flags
;
230 CASE_MFF_TUN_METADATA
:
231 return !ULLONG_GET(wc
->masks
.tunnel
.metadata
.present
.map
,
232 mf
->id
- MFF_TUN_METADATA0
);
234 return !wc
->masks
.metadata
;
236 case MFF_IN_PORT_OXM
:
237 return !wc
->masks
.in_port
.ofp_port
;
238 case MFF_SKB_PRIORITY
:
239 return !wc
->masks
.skb_priority
;
241 return !wc
->masks
.pkt_mark
;
243 return !wc
->masks
.ct_state
;
245 return !wc
->masks
.ct_zone
;
247 return !wc
->masks
.ct_mark
;
249 return ovs_u128_is_zero(wc
->masks
.ct_label
);
250 case MFF_CT_NW_PROTO
:
251 return !wc
->masks
.ct_nw_proto
;
253 return !wc
->masks
.ct_nw_src
;
255 return !wc
->masks
.ct_nw_dst
;
257 return !wc
->masks
.ct_tp_src
;
259 return !wc
->masks
.ct_tp_dst
;
260 case MFF_CT_IPV6_SRC
:
261 return ipv6_mask_is_any(&wc
->masks
.ct_ipv6_src
);
262 case MFF_CT_IPV6_DST
:
263 return ipv6_mask_is_any(&wc
->masks
.ct_ipv6_dst
);
265 return !wc
->masks
.regs
[mf
->id
- MFF_REG0
];
267 return !flow_get_xreg(&wc
->masks
, mf
->id
- MFF_XREG0
);
269 ovs_u128 value
= flow_get_xxreg(&wc
->masks
, mf
->id
- MFF_XXREG0
);
270 return ovs_u128_is_zero(value
);
272 case MFF_ACTSET_OUTPUT
:
273 return !wc
->masks
.actset_output
;
276 return eth_addr_is_zero(wc
->masks
.dl_src
);
278 return eth_addr_is_zero(wc
->masks
.dl_dst
);
280 return !wc
->masks
.dl_type
;
284 return eth_addr_is_zero(wc
->masks
.arp_sha
);
288 return eth_addr_is_zero(wc
->masks
.arp_tha
);
291 return !wc
->masks
.vlans
[0].tci
;
293 return !(wc
->masks
.vlans
[0].tci
& htons(VLAN_VID_MASK
));
295 return !(wc
->masks
.vlans
[0].tci
& htons(VLAN_VID_MASK
| VLAN_CFI
));
296 case MFF_DL_VLAN_PCP
:
298 return !(wc
->masks
.vlans
[0].tci
& htons(VLAN_PCP_MASK
));
301 return !(wc
->masks
.mpls_lse
[0] & htonl(MPLS_LABEL_MASK
));
303 return !(wc
->masks
.mpls_lse
[0] & htonl(MPLS_TC_MASK
));
305 return !(wc
->masks
.mpls_lse
[0] & htonl(MPLS_BOS_MASK
));
307 return !(wc
->masks
.mpls_lse
[0] & htonl(MPLS_TTL_MASK
));
310 return !wc
->masks
.nw_src
;
312 return !wc
->masks
.nw_dst
;
315 return ipv6_mask_is_any(&wc
->masks
.ipv6_src
);
317 return ipv6_mask_is_any(&wc
->masks
.ipv6_dst
);
320 return !wc
->masks
.ipv6_label
;
323 return !wc
->masks
.nw_proto
;
325 case MFF_IP_DSCP_SHIFTED
:
326 return !(wc
->masks
.nw_tos
& IP_DSCP_MASK
);
328 return !(wc
->masks
.nw_tos
& IP_ECN_MASK
);
330 return !wc
->masks
.nw_ttl
;
333 return ipv6_mask_is_any(&wc
->masks
.nd_target
);
336 return !(wc
->masks
.nw_frag
& FLOW_NW_FRAG_MASK
);
339 return !wc
->masks
.nw_proto
;
341 return !wc
->masks
.nw_src
;
343 return !wc
->masks
.nw_dst
;
348 case MFF_ICMPV4_TYPE
:
349 case MFF_ICMPV6_TYPE
:
350 return !wc
->masks
.tp_src
;
354 case MFF_ICMPV4_CODE
:
355 case MFF_ICMPV6_CODE
:
356 return !wc
->masks
.tp_dst
;
358 return !wc
->masks
.tcp_flags
;
366 /* Initializes 'mask' with the wildcard bit pattern for field 'mf' within 'wc'.
367 * Each bit in 'mask' will be set to 1 if the bit is significant for matching
368 * purposes, or to 0 if it is wildcarded.
370 * The caller is responsible for ensuring that 'wc' corresponds to a flow that
371 * meets 'mf''s prerequisites. */
373 mf_get_mask(const struct mf_field
*mf
, const struct flow_wildcards
*wc
,
374 union mf_value
*mask
)
376 mf_get_value(mf
, &wc
->masks
, mask
);
379 /* Tests whether 'mask' is a valid wildcard bit pattern for 'mf'. Returns true
380 * if the mask is valid, false otherwise. */
382 mf_is_mask_valid(const struct mf_field
*mf
, const union mf_value
*mask
)
384 switch (mf
->maskable
) {
386 return (is_all_zeros(mask
, mf
->n_bytes
) ||
387 is_all_ones(mask
, mf
->n_bytes
));
396 /* Returns true if 'flow' meets the prerequisites for 'mf', false otherwise.
397 * If a non-NULL 'mask' is passed, zero-valued matches can also be verified.
398 * Sets inspected bits in 'wc', if non-NULL. */
400 mf_are_prereqs_ok__(const struct mf_field
*mf
, const struct flow
*flow
,
401 const struct flow_wildcards
*mask
,
402 struct flow_wildcards
*wc
)
404 switch (mf
->prereqs
) {
408 return (flow
->dl_type
== htons(ETH_TYPE_ARP
) ||
409 flow
->dl_type
== htons(ETH_TYPE_RARP
));
411 return flow
->dl_type
== htons(ETH_TYPE_IP
);
413 return flow
->dl_type
== htons(ETH_TYPE_IPV6
);
415 return is_vlan(flow
, wc
);
417 return eth_type_mpls(flow
->dl_type
);
419 return is_ip_any(flow
);
421 return is_ct_valid(flow
, mask
, wc
);
423 /* Matching !FRAG_LATER is not enforced (mask is not checked). */
424 return is_tcp(flow
, wc
) && !(flow
->nw_frag
& FLOW_NW_FRAG_LATER
);
426 return is_udp(flow
, wc
) && !(flow
->nw_frag
& FLOW_NW_FRAG_LATER
);
428 return is_sctp(flow
, wc
) && !(flow
->nw_frag
& FLOW_NW_FRAG_LATER
);
430 return is_icmpv4(flow
, wc
);
432 return is_icmpv6(flow
, wc
);
434 return is_nd(flow
, wc
);
436 return is_nd(flow
, wc
) && flow
->tp_src
== htons(ND_NEIGHBOR_SOLICIT
);
438 return is_nd(flow
, wc
) && flow
->tp_src
== htons(ND_NEIGHBOR_ADVERT
);
444 /* Returns true if 'flow' meets the prerequisites for 'mf', false otherwise.
445 * Sets inspected bits in 'wc', if non-NULL. */
447 mf_are_prereqs_ok(const struct mf_field
*mf
, const struct flow
*flow
,
448 struct flow_wildcards
*wc
)
450 return mf_are_prereqs_ok__(mf
, flow
, NULL
, wc
);
453 /* Returns true if 'match' meets the prerequisites for 'mf', false otherwise.
456 mf_are_match_prereqs_ok(const struct mf_field
*mf
, const struct match
*match
)
458 return mf_are_prereqs_ok__(mf
, &match
->flow
, &match
->wc
, NULL
);
461 /* Returns true if 'value' may be a valid value *as part of a masked match*,
464 * A value is not rejected just because it is not valid for the field in
465 * question, but only if it doesn't make sense to test the bits in question at
466 * all. For example, the MFF_VLAN_TCI field will never have a nonzero value
467 * without the VLAN_CFI bit being set, but we can't reject those values because
468 * it is still legitimate to test just for those bits (see the documentation
469 * for NXM_OF_VLAN_TCI in meta-flow.h). On the other hand, there is never a
470 * reason to set the low bit of MFF_IP_DSCP to 1, so we reject that. */
472 mf_is_value_valid(const struct mf_field
*mf
, const union mf_value
*value
)
481 case MFF_TUN_IPV6_SRC
:
482 case MFF_TUN_IPV6_DST
:
486 case MFF_TUN_GBP_FLAGS
:
487 CASE_MFF_TUN_METADATA
:
490 case MFF_SKB_PRIORITY
:
495 case MFF_CT_NW_PROTO
:
498 case MFF_CT_IPV6_SRC
:
499 case MFF_CT_IPV6_DST
:
526 case MFF_ICMPV4_TYPE
:
527 case MFF_ICMPV4_CODE
:
528 case MFF_ICMPV6_TYPE
:
529 case MFF_ICMPV6_CODE
:
535 case MFF_IN_PORT_OXM
:
536 case MFF_ACTSET_OUTPUT
: {
538 return !ofputil_port_from_ofp11(value
->be32
, &port
);
542 return !(value
->u8
& ~IP_DSCP_MASK
);
543 case MFF_IP_DSCP_SHIFTED
:
544 return !(value
->u8
& (~IP_DSCP_MASK
>> 2));
546 return !(value
->u8
& ~IP_ECN_MASK
);
548 return !(value
->u8
& ~FLOW_NW_FRAG_MASK
);
550 return !(value
->be16
& ~htons(0x0fff));
553 return !(value
->be16
& htons(0xff00));
556 return !(value
->be16
& htons(VLAN_CFI
| VLAN_PCP_MASK
));
558 return !(value
->be16
& htons(VLAN_PCP_MASK
));
560 case MFF_DL_VLAN_PCP
:
562 return !(value
->u8
& ~(VLAN_PCP_MASK
>> VLAN_PCP_SHIFT
));
565 return !(value
->be32
& ~htonl(IPV6_LABEL_MASK
));
568 return !(value
->be32
& ~htonl(MPLS_LABEL_MASK
>> MPLS_LABEL_SHIFT
));
571 return !(value
->u8
& ~(MPLS_TC_MASK
>> MPLS_TC_SHIFT
));
574 return !(value
->u8
& ~(MPLS_BOS_MASK
>> MPLS_BOS_SHIFT
));
577 return !(value
->be16
& ~htons(FLOW_TNL_PUB_F_MASK
));
580 return !(value
->be32
& ~htonl(CS_SUPPORTED_MASK
));
588 /* Copies the value of field 'mf' from 'flow' into 'value'. The caller is
589 * responsible for ensuring that 'flow' meets 'mf''s prerequisites. */
591 mf_get_value(const struct mf_field
*mf
, const struct flow
*flow
,
592 union mf_value
*value
)
596 value
->be32
= htonl(flow
->dp_hash
);
599 value
->be32
= htonl(flow
->recirc_id
);
602 value
->be32
= htonl(flow
->conj_id
);
605 value
->be64
= flow
->tunnel
.tun_id
;
608 value
->be32
= flow
->tunnel
.ip_src
;
611 value
->be32
= flow
->tunnel
.ip_dst
;
613 case MFF_TUN_IPV6_SRC
:
614 value
->ipv6
= flow
->tunnel
.ipv6_src
;
616 case MFF_TUN_IPV6_DST
:
617 value
->ipv6
= flow
->tunnel
.ipv6_dst
;
620 value
->be16
= htons(flow
->tunnel
.flags
& FLOW_TNL_PUB_F_MASK
);
623 value
->be16
= flow
->tunnel
.gbp_id
;
625 case MFF_TUN_GBP_FLAGS
:
626 value
->u8
= flow
->tunnel
.gbp_flags
;
629 value
->u8
= flow
->tunnel
.ip_ttl
;
632 value
->u8
= flow
->tunnel
.ip_tos
;
634 CASE_MFF_TUN_METADATA
:
635 tun_metadata_read(&flow
->tunnel
, mf
, value
);
639 value
->be64
= flow
->metadata
;
643 value
->be16
= htons(ofp_to_u16(flow
->in_port
.ofp_port
));
645 case MFF_IN_PORT_OXM
:
646 value
->be32
= ofputil_port_to_ofp11(flow
->in_port
.ofp_port
);
648 case MFF_ACTSET_OUTPUT
:
649 value
->be32
= ofputil_port_to_ofp11(flow
->actset_output
);
652 case MFF_SKB_PRIORITY
:
653 value
->be32
= htonl(flow
->skb_priority
);
657 value
->be32
= htonl(flow
->pkt_mark
);
661 value
->be32
= htonl(flow
->ct_state
);
665 value
->be16
= htons(flow
->ct_zone
);
669 value
->be32
= htonl(flow
->ct_mark
);
673 value
->be128
= hton128(flow
->ct_label
);
676 case MFF_CT_NW_PROTO
:
677 value
->u8
= flow
->ct_nw_proto
;
681 value
->be32
= flow
->ct_nw_src
;
685 value
->be32
= flow
->ct_nw_dst
;
688 case MFF_CT_IPV6_SRC
:
689 value
->ipv6
= flow
->ct_ipv6_src
;
692 case MFF_CT_IPV6_DST
:
693 value
->ipv6
= flow
->ct_ipv6_dst
;
697 value
->be16
= flow
->ct_tp_src
;
701 value
->be16
= flow
->ct_tp_dst
;
705 value
->be32
= htonl(flow
->regs
[mf
->id
- MFF_REG0
]);
709 value
->be64
= htonll(flow_get_xreg(flow
, mf
->id
- MFF_XREG0
));
713 value
->be128
= hton128(flow_get_xxreg(flow
, mf
->id
- MFF_XXREG0
));
717 value
->mac
= flow
->dl_src
;
721 value
->mac
= flow
->dl_dst
;
725 value
->be16
= flow
->dl_type
;
729 value
->be16
= flow
->vlans
[0].tci
;
733 value
->be16
= flow
->vlans
[0].tci
& htons(VLAN_VID_MASK
);
736 value
->be16
= flow
->vlans
[0].tci
& htons(VLAN_VID_MASK
| VLAN_CFI
);
739 case MFF_DL_VLAN_PCP
:
741 value
->u8
= vlan_tci_to_pcp(flow
->vlans
[0].tci
);
745 value
->be32
= htonl(mpls_lse_to_label(flow
->mpls_lse
[0]));
749 value
->u8
= mpls_lse_to_tc(flow
->mpls_lse
[0]);
753 value
->u8
= mpls_lse_to_bos(flow
->mpls_lse
[0]);
757 value
->u8
= mpls_lse_to_ttl(flow
->mpls_lse
[0]);
761 value
->be32
= flow
->nw_src
;
765 value
->be32
= flow
->nw_dst
;
769 value
->ipv6
= flow
->ipv6_src
;
773 value
->ipv6
= flow
->ipv6_dst
;
777 value
->be32
= flow
->ipv6_label
;
781 value
->u8
= flow
->nw_proto
;
785 value
->u8
= flow
->nw_tos
& IP_DSCP_MASK
;
788 case MFF_IP_DSCP_SHIFTED
:
789 value
->u8
= flow
->nw_tos
>> 2;
793 value
->u8
= flow
->nw_tos
& IP_ECN_MASK
;
797 value
->u8
= flow
->nw_ttl
;
801 value
->u8
= flow
->nw_frag
;
805 value
->be16
= htons(flow
->nw_proto
);
809 value
->be32
= flow
->nw_src
;
813 value
->be32
= flow
->nw_dst
;
818 value
->mac
= flow
->arp_sha
;
823 value
->mac
= flow
->arp_tha
;
829 value
->be16
= flow
->tp_src
;
835 value
->be16
= flow
->tp_dst
;
839 value
->be16
= flow
->tcp_flags
;
842 case MFF_ICMPV4_TYPE
:
843 case MFF_ICMPV6_TYPE
:
844 value
->u8
= ntohs(flow
->tp_src
);
847 case MFF_ICMPV4_CODE
:
848 case MFF_ICMPV6_CODE
:
849 value
->u8
= ntohs(flow
->tp_dst
);
853 value
->ipv6
= flow
->nd_target
;
862 /* Makes 'match' match field 'mf' exactly, with the value matched taken from
863 * 'value'. The caller is responsible for ensuring that 'match' meets 'mf''s
866 * If non-NULL, 'err_str' returns a malloc'ed string describing any errors
867 * with the request or NULL if there is no error. The caller is reponsible
868 * for freeing the string. */
870 mf_set_value(const struct mf_field
*mf
,
871 const union mf_value
*value
, struct match
*match
, char **err_str
)
879 match_set_dp_hash(match
, ntohl(value
->be32
));
882 match_set_recirc_id(match
, ntohl(value
->be32
));
885 match_set_conj_id(match
, ntohl(value
->be32
));
888 match_set_tun_id(match
, value
->be64
);
891 match_set_tun_src(match
, value
->be32
);
894 match_set_tun_dst(match
, value
->be32
);
896 case MFF_TUN_IPV6_SRC
:
897 match_set_tun_ipv6_src(match
, &value
->ipv6
);
899 case MFF_TUN_IPV6_DST
:
900 match_set_tun_ipv6_dst(match
, &value
->ipv6
);
903 match_set_tun_flags(match
, ntohs(value
->be16
));
906 match_set_tun_gbp_id(match
, value
->be16
);
908 case MFF_TUN_GBP_FLAGS
:
909 match_set_tun_gbp_flags(match
, value
->u8
);
912 match_set_tun_tos(match
, value
->u8
);
915 match_set_tun_ttl(match
, value
->u8
);
917 CASE_MFF_TUN_METADATA
:
918 tun_metadata_set_match(mf
, value
, NULL
, match
, err_str
);
922 match_set_metadata(match
, value
->be64
);
926 match_set_in_port(match
, u16_to_ofp(ntohs(value
->be16
)));
929 case MFF_IN_PORT_OXM
: {
931 ofputil_port_from_ofp11(value
->be32
, &port
);
932 match_set_in_port(match
, port
);
935 case MFF_ACTSET_OUTPUT
: {
937 ofputil_port_from_ofp11(value
->be32
, &port
);
938 match_set_actset_output(match
, port
);
942 case MFF_SKB_PRIORITY
:
943 match_set_skb_priority(match
, ntohl(value
->be32
));
947 match_set_pkt_mark(match
, ntohl(value
->be32
));
951 match_set_ct_state(match
, ntohl(value
->be32
));
955 match_set_ct_zone(match
, ntohs(value
->be16
));
959 match_set_ct_mark(match
, ntohl(value
->be32
));
963 match_set_ct_label(match
, ntoh128(value
->be128
));
966 case MFF_CT_NW_PROTO
:
967 match_set_ct_nw_proto(match
, value
->u8
);
971 match_set_ct_nw_src(match
, value
->be32
);
975 match_set_ct_nw_dst(match
, value
->be32
);
978 case MFF_CT_IPV6_SRC
:
979 match_set_ct_ipv6_src(match
, &value
->ipv6
);
982 case MFF_CT_IPV6_DST
:
983 match_set_ct_ipv6_dst(match
, &value
->ipv6
);
987 match_set_ct_tp_src(match
, value
->be16
);
991 match_set_ct_tp_dst(match
, value
->be16
);
995 match_set_reg(match
, mf
->id
- MFF_REG0
, ntohl(value
->be32
));
999 match_set_xreg(match
, mf
->id
- MFF_XREG0
, ntohll(value
->be64
));
1003 match_set_xxreg(match
, mf
->id
- MFF_XXREG0
, ntoh128(value
->be128
));
1007 match_set_dl_src(match
, value
->mac
);
1011 match_set_dl_dst(match
, value
->mac
);
1015 match_set_dl_type(match
, value
->be16
);
1019 match_set_dl_tci(match
, value
->be16
);
1023 match_set_dl_vlan(match
, value
->be16
);
1026 match_set_vlan_vid(match
, value
->be16
);
1029 case MFF_DL_VLAN_PCP
:
1031 match_set_dl_vlan_pcp(match
, value
->u8
);
1034 case MFF_MPLS_LABEL
:
1035 match_set_mpls_label(match
, 0, value
->be32
);
1039 match_set_mpls_tc(match
, 0, value
->u8
);
1043 match_set_mpls_bos(match
, 0, value
->u8
);
1047 match_set_mpls_ttl(match
, 0, value
->u8
);
1051 match_set_nw_src(match
, value
->be32
);
1055 match_set_nw_dst(match
, value
->be32
);
1059 match_set_ipv6_src(match
, &value
->ipv6
);
1063 match_set_ipv6_dst(match
, &value
->ipv6
);
1066 case MFF_IPV6_LABEL
:
1067 match_set_ipv6_label(match
, value
->be32
);
1071 match_set_nw_proto(match
, value
->u8
);
1075 match_set_nw_dscp(match
, value
->u8
);
1078 case MFF_IP_DSCP_SHIFTED
:
1079 match_set_nw_dscp(match
, value
->u8
<< 2);
1083 match_set_nw_ecn(match
, value
->u8
);
1087 match_set_nw_ttl(match
, value
->u8
);
1091 match_set_nw_frag(match
, value
->u8
);
1095 match_set_nw_proto(match
, ntohs(value
->be16
));
1099 match_set_nw_src(match
, value
->be32
);
1103 match_set_nw_dst(match
, value
->be32
);
1108 match_set_arp_sha(match
, value
->mac
);
1113 match_set_arp_tha(match
, value
->mac
);
1119 match_set_tp_src(match
, value
->be16
);
1125 match_set_tp_dst(match
, value
->be16
);
1129 match_set_tcp_flags(match
, value
->be16
);
1132 case MFF_ICMPV4_TYPE
:
1133 case MFF_ICMPV6_TYPE
:
1134 match_set_icmp_type(match
, value
->u8
);
1137 case MFF_ICMPV4_CODE
:
1138 case MFF_ICMPV6_CODE
:
1139 match_set_icmp_code(match
, value
->u8
);
1143 match_set_nd_target(match
, &value
->ipv6
);
1152 /* Unwildcard the bits in 'mask' of the 'wc' member field described by 'mf'.
1153 * The caller is responsible for ensuring that 'wc' meets 'mf''s
1156 mf_mask_field_masked(const struct mf_field
*mf
, const union mf_value
*mask
,
1157 struct flow_wildcards
*wc
)
1159 union mf_value temp_mask
;
1160 /* For MFF_DL_VLAN, we cannot send a all 1's to flow_set_dl_vlan() as that
1161 * will be considered as OFP10_VLAN_NONE. So make sure the mask only has
1162 * valid bits in this case. */
1163 if (mf
->id
== MFF_DL_VLAN
) {
1164 temp_mask
.be16
= htons(VLAN_VID_MASK
) & mask
->be16
;
1168 union mf_value mask_value
;
1170 mf_get_value(mf
, &wc
->masks
, &mask_value
);
1171 for (size_t i
= 0; i
< mf
->n_bytes
; i
++) {
1172 mask_value
.b
[i
] |= mask
->b
[i
];
1174 mf_set_flow_value(mf
, &mask_value
, &wc
->masks
);
1177 /* Unwildcard 'wc' member field described by 'mf'. The caller is
1178 * responsible for ensuring that 'mask' meets 'mf''s prerequisites. */
1180 mf_mask_field(const struct mf_field
*mf
, struct flow_wildcards
*wc
)
1182 mf_mask_field_masked(mf
, &exact_match_mask
, wc
);
1186 field_len(const struct mf_field
*mf
, const union mf_value
*value_
)
1188 const uint8_t *value
= &value_
->u8
;
1191 if (!mf
->variable_len
) {
1199 for (i
= 0; i
< mf
->n_bytes
; i
++) {
1200 if (value
[i
] != 0) {
1205 return mf
->n_bytes
- i
;
1208 /* Returns the effective length of the field. For fixed length fields,
1209 * this is just the defined length. For variable length fields, it is
1210 * the minimum size encoding that retains the same meaning (i.e.
1211 * discarding leading zeros).
1213 * 'is_masked' returns (if non-NULL) whether the original contained
1214 * a mask. Otherwise, a mask that is the same length as the value
1215 * might be misinterpreted as an exact match. */
1217 mf_field_len(const struct mf_field
*mf
, const union mf_value
*value
,
1218 const union mf_value
*mask
, bool *is_masked_
)
1221 bool is_masked
= mask
&& !is_all_ones(mask
, mf
->n_bytes
);
1223 len
= field_len(mf
, value
);
1225 mask_len
= field_len(mf
, mask
);
1226 len
= MAX(len
, mask_len
);
1230 *is_masked_
= is_masked
;
1236 /* Sets 'flow' member field described by 'mf' to 'value'. The caller is
1237 * responsible for ensuring that 'flow' meets 'mf''s prerequisites.*/
1239 mf_set_flow_value(const struct mf_field
*mf
,
1240 const union mf_value
*value
, struct flow
*flow
)
1244 flow
->dp_hash
= ntohl(value
->be32
);
1247 flow
->recirc_id
= ntohl(value
->be32
);
1250 flow
->conj_id
= ntohl(value
->be32
);
1253 flow
->tunnel
.tun_id
= value
->be64
;
1256 flow
->tunnel
.ip_src
= value
->be32
;
1259 flow
->tunnel
.ip_dst
= value
->be32
;
1261 case MFF_TUN_IPV6_SRC
:
1262 flow
->tunnel
.ipv6_src
= value
->ipv6
;
1264 case MFF_TUN_IPV6_DST
:
1265 flow
->tunnel
.ipv6_dst
= value
->ipv6
;
1268 flow
->tunnel
.flags
= (flow
->tunnel
.flags
& ~FLOW_TNL_PUB_F_MASK
) |
1271 case MFF_TUN_GBP_ID
:
1272 flow
->tunnel
.gbp_id
= value
->be16
;
1274 case MFF_TUN_GBP_FLAGS
:
1275 flow
->tunnel
.gbp_flags
= value
->u8
;
1278 flow
->tunnel
.ip_tos
= value
->u8
;
1281 flow
->tunnel
.ip_ttl
= value
->u8
;
1283 CASE_MFF_TUN_METADATA
:
1284 tun_metadata_write(&flow
->tunnel
, mf
, value
);
1287 flow
->metadata
= value
->be64
;
1291 flow
->in_port
.ofp_port
= u16_to_ofp(ntohs(value
->be16
));
1294 case MFF_IN_PORT_OXM
:
1295 ofputil_port_from_ofp11(value
->be32
, &flow
->in_port
.ofp_port
);
1297 case MFF_ACTSET_OUTPUT
:
1298 ofputil_port_from_ofp11(value
->be32
, &flow
->actset_output
);
1301 case MFF_SKB_PRIORITY
:
1302 flow
->skb_priority
= ntohl(value
->be32
);
1306 flow
->pkt_mark
= ntohl(value
->be32
);
1310 flow
->ct_state
= ntohl(value
->be32
);
1314 flow
->ct_zone
= ntohs(value
->be16
);
1318 flow
->ct_mark
= ntohl(value
->be32
);
1322 flow
->ct_label
= ntoh128(value
->be128
);
1325 case MFF_CT_NW_PROTO
:
1326 flow
->ct_nw_proto
= value
->u8
;
1330 flow
->ct_nw_src
= value
->be32
;
1334 flow
->ct_nw_dst
= value
->be32
;
1337 case MFF_CT_IPV6_SRC
:
1338 flow
->ct_ipv6_src
= value
->ipv6
;
1341 case MFF_CT_IPV6_DST
:
1342 flow
->ct_ipv6_dst
= value
->ipv6
;
1346 flow
->ct_tp_src
= value
->be16
;
1350 flow
->ct_tp_dst
= value
->be16
;
1354 flow
->regs
[mf
->id
- MFF_REG0
] = ntohl(value
->be32
);
1358 flow_set_xreg(flow
, mf
->id
- MFF_XREG0
, ntohll(value
->be64
));
1362 flow_set_xxreg(flow
, mf
->id
- MFF_XXREG0
, ntoh128(value
->be128
));
1366 flow
->dl_src
= value
->mac
;
1370 flow
->dl_dst
= value
->mac
;
1374 flow
->dl_type
= value
->be16
;
1378 flow
->vlans
[0].tci
= value
->be16
;
1379 flow_fix_vlan_tpid(flow
);
1383 flow_set_dl_vlan(flow
, value
->be16
);
1384 flow_fix_vlan_tpid(flow
);
1388 flow_set_vlan_vid(flow
, value
->be16
);
1389 flow_fix_vlan_tpid(flow
);
1392 case MFF_DL_VLAN_PCP
:
1394 flow_set_vlan_pcp(flow
, value
->u8
);
1395 flow_fix_vlan_tpid(flow
);
1398 case MFF_MPLS_LABEL
:
1399 flow_set_mpls_label(flow
, 0, value
->be32
);
1403 flow_set_mpls_tc(flow
, 0, value
->u8
);
1407 flow_set_mpls_bos(flow
, 0, value
->u8
);
1411 flow_set_mpls_ttl(flow
, 0, value
->u8
);
1415 flow
->nw_src
= value
->be32
;
1419 flow
->nw_dst
= value
->be32
;
1423 flow
->ipv6_src
= value
->ipv6
;
1427 flow
->ipv6_dst
= value
->ipv6
;
1430 case MFF_IPV6_LABEL
:
1431 flow
->ipv6_label
= value
->be32
& htonl(IPV6_LABEL_MASK
);
1435 flow
->nw_proto
= value
->u8
;
1439 flow
->nw_tos
&= ~IP_DSCP_MASK
;
1440 flow
->nw_tos
|= value
->u8
& IP_DSCP_MASK
;
1443 case MFF_IP_DSCP_SHIFTED
:
1444 flow
->nw_tos
&= ~IP_DSCP_MASK
;
1445 flow
->nw_tos
|= value
->u8
<< 2;
1449 flow
->nw_tos
&= ~IP_ECN_MASK
;
1450 flow
->nw_tos
|= value
->u8
& IP_ECN_MASK
;
1454 flow
->nw_ttl
= value
->u8
;
1458 flow
->nw_frag
= value
->u8
& FLOW_NW_FRAG_MASK
;
1462 flow
->nw_proto
= ntohs(value
->be16
);
1466 flow
->nw_src
= value
->be32
;
1470 flow
->nw_dst
= value
->be32
;
1475 flow
->arp_sha
= value
->mac
;
1480 flow
->arp_tha
= value
->mac
;
1486 flow
->tp_src
= value
->be16
;
1492 flow
->tp_dst
= value
->be16
;
1496 flow
->tcp_flags
= value
->be16
;
1499 case MFF_ICMPV4_TYPE
:
1500 case MFF_ICMPV6_TYPE
:
1501 flow
->tp_src
= htons(value
->u8
);
1504 case MFF_ICMPV4_CODE
:
1505 case MFF_ICMPV6_CODE
:
1506 flow
->tp_dst
= htons(value
->u8
);
1510 flow
->nd_target
= value
->ipv6
;
1519 /* Consider each of 'src', 'mask', and 'dst' as if they were arrays of 8*n
1520 * bits. Then, for each 0 <= i < 8 * n such that mask[i] == 1, sets dst[i] =
1523 apply_mask(const uint8_t *src
, const uint8_t *mask
, uint8_t *dst
, size_t n
)
1527 for (i
= 0; i
< n
; i
++) {
1528 dst
[i
] = (src
[i
] & mask
[i
]) | (dst
[i
] & ~mask
[i
]);
1532 /* Sets 'flow' member field described by 'field' to 'value', except that bits
1533 * for which 'mask' has a 0-bit keep their existing values. The caller is
1534 * responsible for ensuring that 'flow' meets 'field''s prerequisites.*/
1536 mf_set_flow_value_masked(const struct mf_field
*field
,
1537 const union mf_value
*value
,
1538 const union mf_value
*mask
,
1543 mf_get_value(field
, flow
, &tmp
);
1544 apply_mask((const uint8_t *) value
, (const uint8_t *) mask
,
1545 (uint8_t *) &tmp
, field
->n_bytes
);
1546 mf_set_flow_value(field
, &tmp
, flow
);
1550 mf_is_tun_metadata(const struct mf_field
*mf
)
1552 return mf
->id
>= MFF_TUN_METADATA0
&&
1553 mf
->id
< MFF_TUN_METADATA0
+ TUN_METADATA_NUM_OPTS
;
1557 mf_is_pipeline_field(const struct mf_field
*mf
)
1563 case MFF_TUN_IPV6_SRC
:
1564 case MFF_TUN_IPV6_DST
:
1566 case MFF_TUN_GBP_ID
:
1567 case MFF_TUN_GBP_FLAGS
:
1568 CASE_MFF_TUN_METADATA
:
1571 case MFF_IN_PORT_OXM
:
1582 case MFF_ACTSET_OUTPUT
:
1583 case MFF_SKB_PRIORITY
:
1589 case MFF_CT_NW_PROTO
:
1592 case MFF_CT_IPV6_SRC
:
1593 case MFF_CT_IPV6_DST
:
1602 case MFF_DL_VLAN_PCP
:
1604 case MFF_MPLS_LABEL
:
1612 case MFF_IPV6_LABEL
:
1615 case MFF_IP_DSCP_SHIFTED
:
1631 case MFF_ICMPV4_TYPE
:
1632 case MFF_ICMPV4_CODE
:
1633 case MFF_ICMPV6_TYPE
:
1634 case MFF_ICMPV6_CODE
:
1646 /* Returns true if 'mf' has previously been set in 'flow', false if
1647 * it contains a non-default value.
1649 * The caller is responsible for ensuring that 'flow' meets 'mf''s
1652 mf_is_set(const struct mf_field
*mf
, const struct flow
*flow
)
1654 if (!mf_is_tun_metadata(mf
)) {
1655 union mf_value value
;
1657 mf_get_value(mf
, flow
, &value
);
1658 return !is_all_zeros(&value
, mf
->n_bytes
);
1660 return ULLONG_GET(flow
->tunnel
.metadata
.present
.map
,
1661 mf
->id
- MFF_TUN_METADATA0
);
1665 /* Makes 'match' wildcard field 'mf'.
1667 * The caller is responsible for ensuring that 'match' meets 'mf''s
1670 * If non-NULL, 'err_str' returns a malloc'ed string describing any errors
1671 * with the request or NULL if there is no error. The caller is reponsible
1672 * for freeing the string. */
1674 mf_set_wild(const struct mf_field
*mf
, struct match
*match
, char **err_str
)
1682 match
->flow
.dp_hash
= 0;
1683 match
->wc
.masks
.dp_hash
= 0;
1686 match
->flow
.recirc_id
= 0;
1687 match
->wc
.masks
.recirc_id
= 0;
1690 match
->flow
.conj_id
= 0;
1691 match
->wc
.masks
.conj_id
= 0;
1694 match_set_tun_id_masked(match
, htonll(0), htonll(0));
1697 match_set_tun_src_masked(match
, htonl(0), htonl(0));
1700 match_set_tun_dst_masked(match
, htonl(0), htonl(0));
1702 case MFF_TUN_IPV6_SRC
:
1703 memset(&match
->wc
.masks
.tunnel
.ipv6_src
, 0,
1704 sizeof match
->wc
.masks
.tunnel
.ipv6_src
);
1705 memset(&match
->flow
.tunnel
.ipv6_src
, 0,
1706 sizeof match
->flow
.tunnel
.ipv6_src
);
1708 case MFF_TUN_IPV6_DST
:
1709 memset(&match
->wc
.masks
.tunnel
.ipv6_dst
, 0,
1710 sizeof match
->wc
.masks
.tunnel
.ipv6_dst
);
1711 memset(&match
->flow
.tunnel
.ipv6_dst
, 0,
1712 sizeof match
->flow
.tunnel
.ipv6_dst
);
1715 match_set_tun_flags_masked(match
, 0, 0);
1717 case MFF_TUN_GBP_ID
:
1718 match_set_tun_gbp_id_masked(match
, 0, 0);
1720 case MFF_TUN_GBP_FLAGS
:
1721 match_set_tun_gbp_flags_masked(match
, 0, 0);
1724 match_set_tun_tos_masked(match
, 0, 0);
1727 match_set_tun_ttl_masked(match
, 0, 0);
1729 CASE_MFF_TUN_METADATA
:
1730 tun_metadata_set_match(mf
, NULL
, NULL
, match
, err_str
);
1734 match_set_metadata_masked(match
, htonll(0), htonll(0));
1738 case MFF_IN_PORT_OXM
:
1739 match
->flow
.in_port
.ofp_port
= 0;
1740 match
->wc
.masks
.in_port
.ofp_port
= 0;
1742 case MFF_ACTSET_OUTPUT
:
1743 match
->flow
.actset_output
= 0;
1744 match
->wc
.masks
.actset_output
= 0;
1747 case MFF_SKB_PRIORITY
:
1748 match
->flow
.skb_priority
= 0;
1749 match
->wc
.masks
.skb_priority
= 0;
1753 match
->flow
.pkt_mark
= 0;
1754 match
->wc
.masks
.pkt_mark
= 0;
1758 match
->flow
.ct_state
= 0;
1759 match
->wc
.masks
.ct_state
= 0;
1763 match
->flow
.ct_zone
= 0;
1764 match
->wc
.masks
.ct_zone
= 0;
1768 match
->flow
.ct_mark
= 0;
1769 match
->wc
.masks
.ct_mark
= 0;
1773 memset(&match
->flow
.ct_label
, 0, sizeof(match
->flow
.ct_label
));
1774 memset(&match
->wc
.masks
.ct_label
, 0, sizeof(match
->wc
.masks
.ct_label
));
1777 case MFF_CT_NW_PROTO
:
1778 match
->flow
.ct_nw_proto
= 0;
1779 match
->wc
.masks
.ct_nw_proto
= 0;
1783 match
->flow
.ct_nw_src
= 0;
1784 match
->wc
.masks
.ct_nw_src
= 0;
1788 match
->flow
.ct_nw_dst
= 0;
1789 match
->wc
.masks
.ct_nw_dst
= 0;
1792 case MFF_CT_IPV6_SRC
:
1793 memset(&match
->flow
.ct_ipv6_src
, 0, sizeof(match
->flow
.ct_ipv6_src
));
1794 WC_UNMASK_FIELD(&match
->wc
, ct_ipv6_src
);
1797 case MFF_CT_IPV6_DST
:
1798 memset(&match
->flow
.ct_ipv6_dst
, 0, sizeof(match
->flow
.ct_ipv6_dst
));
1799 WC_UNMASK_FIELD(&match
->wc
, ct_ipv6_dst
);
1803 match
->flow
.ct_tp_src
= 0;
1804 match
->wc
.masks
.ct_tp_src
= 0;
1808 match
->flow
.ct_tp_dst
= 0;
1809 match
->wc
.masks
.ct_tp_dst
= 0;
1813 match_set_reg_masked(match
, mf
->id
- MFF_REG0
, 0, 0);
1817 match_set_xreg_masked(match
, mf
->id
- MFF_XREG0
, 0, 0);
1821 match_set_xxreg_masked(match
, mf
->id
- MFF_XXREG0
, OVS_U128_ZERO
,
1827 match
->flow
.dl_src
= eth_addr_zero
;
1828 match
->wc
.masks
.dl_src
= eth_addr_zero
;
1832 match
->flow
.dl_dst
= eth_addr_zero
;
1833 match
->wc
.masks
.dl_dst
= eth_addr_zero
;
1837 match
->flow
.dl_type
= htons(0);
1838 match
->wc
.masks
.dl_type
= htons(0);
1842 match_set_dl_tci_masked(match
, htons(0), htons(0));
1847 match_set_any_vid(match
);
1850 case MFF_DL_VLAN_PCP
:
1852 match_set_any_pcp(match
);
1855 case MFF_MPLS_LABEL
:
1856 match_set_any_mpls_label(match
, 0);
1860 match_set_any_mpls_tc(match
, 0);
1864 match_set_any_mpls_bos(match
, 0);
1868 match_set_any_mpls_ttl(match
, 0);
1873 match_set_nw_src_masked(match
, htonl(0), htonl(0));
1878 match_set_nw_dst_masked(match
, htonl(0), htonl(0));
1882 memset(&match
->wc
.masks
.ipv6_src
, 0, sizeof match
->wc
.masks
.ipv6_src
);
1883 memset(&match
->flow
.ipv6_src
, 0, sizeof match
->flow
.ipv6_src
);
1887 memset(&match
->wc
.masks
.ipv6_dst
, 0, sizeof match
->wc
.masks
.ipv6_dst
);
1888 memset(&match
->flow
.ipv6_dst
, 0, sizeof match
->flow
.ipv6_dst
);
1891 case MFF_IPV6_LABEL
:
1892 match
->wc
.masks
.ipv6_label
= htonl(0);
1893 match
->flow
.ipv6_label
= htonl(0);
1897 match
->wc
.masks
.nw_proto
= 0;
1898 match
->flow
.nw_proto
= 0;
1902 case MFF_IP_DSCP_SHIFTED
:
1903 match
->wc
.masks
.nw_tos
&= ~IP_DSCP_MASK
;
1904 match
->flow
.nw_tos
&= ~IP_DSCP_MASK
;
1908 match
->wc
.masks
.nw_tos
&= ~IP_ECN_MASK
;
1909 match
->flow
.nw_tos
&= ~IP_ECN_MASK
;
1913 match
->wc
.masks
.nw_ttl
= 0;
1914 match
->flow
.nw_ttl
= 0;
1918 match
->wc
.masks
.nw_frag
&= ~FLOW_NW_FRAG_MASK
;
1919 match
->flow
.nw_frag
&= ~FLOW_NW_FRAG_MASK
;
1923 match
->wc
.masks
.nw_proto
= 0;
1924 match
->flow
.nw_proto
= 0;
1929 match
->flow
.arp_sha
= eth_addr_zero
;
1930 match
->wc
.masks
.arp_sha
= eth_addr_zero
;
1935 match
->flow
.arp_tha
= eth_addr_zero
;
1936 match
->wc
.masks
.arp_tha
= eth_addr_zero
;
1942 case MFF_ICMPV4_TYPE
:
1943 case MFF_ICMPV6_TYPE
:
1944 match
->wc
.masks
.tp_src
= htons(0);
1945 match
->flow
.tp_src
= htons(0);
1951 case MFF_ICMPV4_CODE
:
1952 case MFF_ICMPV6_CODE
:
1953 match
->wc
.masks
.tp_dst
= htons(0);
1954 match
->flow
.tp_dst
= htons(0);
1958 match
->wc
.masks
.tcp_flags
= htons(0);
1959 match
->flow
.tcp_flags
= htons(0);
1963 memset(&match
->wc
.masks
.nd_target
, 0,
1964 sizeof match
->wc
.masks
.nd_target
);
1965 memset(&match
->flow
.nd_target
, 0, sizeof match
->flow
.nd_target
);
1974 /* Makes 'match' match field 'mf' with the specified 'value' and 'mask'.
1975 * 'value' specifies a value to match and 'mask' specifies a wildcard pattern,
1976 * with a 1-bit indicating that the corresponding value bit must match and a
1977 * 0-bit indicating a don't-care.
1979 * If 'mask' is NULL or points to all-1-bits, then this call is equivalent to
1980 * mf_set_value(mf, value, match). If 'mask' points to all-0-bits, then this
1981 * call is equivalent to mf_set_wild(mf, match).
1983 * 'mask' must be a valid mask for 'mf' (see mf_is_mask_valid()). The caller
1984 * is responsible for ensuring that 'match' meets 'mf''s prerequisites.
1986 * If non-NULL, 'err_str' returns a malloc'ed string describing any errors
1987 * with the request or NULL if there is no error. The caller is reponsible
1988 * for freeing the string.
1990 * Return a set of enum ofputil_protocol bits (as an uint32_t to avoid circular
1991 * dependency on enum ofputil_protocol definition) indicating which OpenFlow
1992 * protocol versions can support this functionality. */
1994 mf_set(const struct mf_field
*mf
,
1995 const union mf_value
*value
, const union mf_value
*mask
,
1996 struct match
*match
, char **err_str
)
1998 if (!mask
|| is_all_ones(mask
, mf
->n_bytes
)) {
1999 mf_set_value(mf
, value
, match
, err_str
);
2000 return mf
->usable_protocols_exact
;
2001 } else if (is_all_zeros(mask
, mf
->n_bytes
) && !mf_is_tun_metadata(mf
)) {
2002 /* Tunnel metadata matches on the existence of the field itself, so
2003 * it still needs to be encoded even if the value is wildcarded. */
2004 mf_set_wild(mf
, match
, err_str
);
2005 return OFPUTIL_P_ANY
;
2014 case MFF_CT_NW_PROTO
:
2017 case MFF_CT_IPV6_SRC
:
2018 case MFF_CT_IPV6_DST
:
2024 case MFF_IN_PORT_OXM
:
2025 case MFF_ACTSET_OUTPUT
:
2026 case MFF_SKB_PRIORITY
:
2029 case MFF_DL_VLAN_PCP
:
2031 case MFF_MPLS_LABEL
:
2038 case MFF_IP_DSCP_SHIFTED
:
2041 case MFF_ICMPV4_TYPE
:
2042 case MFF_ICMPV4_CODE
:
2043 case MFF_ICMPV6_TYPE
:
2044 case MFF_ICMPV6_CODE
:
2045 return OFPUTIL_P_NONE
;
2048 match_set_dp_hash_masked(match
, ntohl(value
->be32
), ntohl(mask
->be32
));
2051 match_set_tun_id_masked(match
, value
->be64
, mask
->be64
);
2054 match_set_tun_src_masked(match
, value
->be32
, mask
->be32
);
2057 match_set_tun_dst_masked(match
, value
->be32
, mask
->be32
);
2059 case MFF_TUN_IPV6_SRC
:
2060 match_set_tun_ipv6_src_masked(match
, &value
->ipv6
, &mask
->ipv6
);
2062 case MFF_TUN_IPV6_DST
:
2063 match_set_tun_ipv6_dst_masked(match
, &value
->ipv6
, &mask
->ipv6
);
2066 match_set_tun_flags_masked(match
, ntohs(value
->be16
), ntohs(mask
->be16
));
2068 case MFF_TUN_GBP_ID
:
2069 match_set_tun_gbp_id_masked(match
, value
->be16
, mask
->be16
);
2071 case MFF_TUN_GBP_FLAGS
:
2072 match_set_tun_gbp_flags_masked(match
, value
->u8
, mask
->u8
);
2075 match_set_tun_ttl_masked(match
, value
->u8
, mask
->u8
);
2078 match_set_tun_tos_masked(match
, value
->u8
, mask
->u8
);
2080 CASE_MFF_TUN_METADATA
:
2081 tun_metadata_set_match(mf
, value
, mask
, match
, err_str
);
2085 match_set_metadata_masked(match
, value
->be64
, mask
->be64
);
2089 match_set_reg_masked(match
, mf
->id
- MFF_REG0
,
2090 ntohl(value
->be32
), ntohl(mask
->be32
));
2094 match_set_xreg_masked(match
, mf
->id
- MFF_XREG0
,
2095 ntohll(value
->be64
), ntohll(mask
->be64
));
2099 match_set_xxreg_masked(match
, mf
->id
- MFF_XXREG0
,
2100 ntoh128(value
->be128
), ntoh128(mask
->be128
));
2105 match_set_pkt_mark_masked(match
, ntohl(value
->be32
),
2110 match_set_ct_state_masked(match
, ntohl(value
->be32
), ntohl(mask
->be32
));
2114 match_set_ct_mark_masked(match
, ntohl(value
->be32
), ntohl(mask
->be32
));
2118 match_set_ct_label_masked(match
, ntoh128(value
->be128
),
2119 ntoh128(mask
->be128
));
2123 match_set_dl_dst_masked(match
, value
->mac
, mask
->mac
);
2127 match_set_dl_src_masked(match
, value
->mac
, mask
->mac
);
2132 match_set_arp_sha_masked(match
, value
->mac
, mask
->mac
);
2137 match_set_arp_tha_masked(match
, value
->mac
, mask
->mac
);
2141 match_set_dl_tci_masked(match
, value
->be16
, mask
->be16
);
2145 match_set_vlan_vid_masked(match
, value
->be16
, mask
->be16
);
2149 match_set_nw_src_masked(match
, value
->be32
, mask
->be32
);
2153 match_set_nw_dst_masked(match
, value
->be32
, mask
->be32
);
2157 match_set_ipv6_src_masked(match
, &value
->ipv6
, &mask
->ipv6
);
2161 match_set_ipv6_dst_masked(match
, &value
->ipv6
, &mask
->ipv6
);
2164 case MFF_IPV6_LABEL
:
2165 if ((mask
->be32
& htonl(IPV6_LABEL_MASK
)) == htonl(IPV6_LABEL_MASK
)) {
2166 mf_set_value(mf
, value
, match
, err_str
);
2168 match_set_ipv6_label_masked(match
, value
->be32
, mask
->be32
);
2173 match_set_nd_target_masked(match
, &value
->ipv6
, &mask
->ipv6
);
2177 match_set_nw_frag_masked(match
, value
->u8
, mask
->u8
);
2181 match_set_nw_src_masked(match
, value
->be32
, mask
->be32
);
2185 match_set_nw_dst_masked(match
, value
->be32
, mask
->be32
);
2191 match_set_tp_src_masked(match
, value
->be16
, mask
->be16
);
2197 match_set_tp_dst_masked(match
, value
->be16
, mask
->be16
);
2201 match_set_tcp_flags_masked(match
, value
->be16
, mask
->be16
);
2209 return ((mf
->usable_protocols_bitwise
== mf
->usable_protocols_cidr
2210 || ip_is_cidr(mask
->be32
))
2211 ? mf
->usable_protocols_cidr
2212 : mf
->usable_protocols_bitwise
);
2216 mf_check__(const struct mf_subfield
*sf
, const struct match
*match
,
2220 VLOG_WARN_RL(&rl
, "unknown %s field", type
);
2221 return OFPERR_OFPBAC_BAD_SET_TYPE
;
2222 } else if (!sf
->n_bits
) {
2223 VLOG_WARN_RL(&rl
, "zero bit %s field %s", type
, sf
->field
->name
);
2224 return OFPERR_OFPBAC_BAD_SET_LEN
;
2225 } else if (sf
->ofs
>= sf
->field
->n_bits
) {
2226 VLOG_WARN_RL(&rl
, "bit offset %d exceeds %d-bit width of %s field %s",
2227 sf
->ofs
, sf
->field
->n_bits
, type
, sf
->field
->name
);
2228 return OFPERR_OFPBAC_BAD_SET_LEN
;
2229 } else if (sf
->ofs
+ sf
->n_bits
> sf
->field
->n_bits
) {
2230 VLOG_WARN_RL(&rl
, "bit offset %d and width %d exceeds %d-bit width "
2231 "of %s field %s", sf
->ofs
, sf
->n_bits
,
2232 sf
->field
->n_bits
, type
, sf
->field
->name
);
2233 return OFPERR_OFPBAC_BAD_SET_LEN
;
2234 } else if (match
&& !mf_are_match_prereqs_ok(sf
->field
, match
)) {
2235 VLOG_WARN_RL(&rl
, "%s field %s lacks correct prerequisites",
2236 type
, sf
->field
->name
);
2237 return OFPERR_OFPBAC_MATCH_INCONSISTENT
;
2243 /* Sets all the bits in 'sf' to 1 within 'wc', if 'wc' is nonnull. */
2245 unwildcard_subfield(const struct mf_subfield
*sf
, struct flow_wildcards
*wc
)
2248 union mf_value mask
;
2250 memset(&mask
, 0, sizeof mask
);
2251 bitwise_one(&mask
, sf
->field
->n_bytes
, sf
->ofs
, sf
->n_bits
);
2252 mf_mask_field_masked(sf
->field
, &mask
, wc
);
2256 /* Copies 'src' into 'dst' within 'flow', and sets all the bits in 'src' and
2257 * 'dst' to 1s in 'wc', if 'wc' is nonnull.
2259 * 'src' and 'dst' may overlap. */
2261 mf_subfield_copy(const struct mf_subfield
*src
,
2262 const struct mf_subfield
*dst
,
2263 struct flow
*flow
, struct flow_wildcards
*wc
)
2265 ovs_assert(src
->n_bits
== dst
->n_bits
);
2266 if (mf_are_prereqs_ok(dst
->field
, flow
, wc
)
2267 && mf_are_prereqs_ok(src
->field
, flow
, wc
)) {
2268 unwildcard_subfield(src
, wc
);
2269 unwildcard_subfield(dst
, wc
);
2271 union mf_value src_value
;
2272 union mf_value dst_value
;
2273 mf_get_value(dst
->field
, flow
, &dst_value
);
2274 mf_get_value(src
->field
, flow
, &src_value
);
2275 bitwise_copy(&src_value
, src
->field
->n_bytes
, src
->ofs
,
2276 &dst_value
, dst
->field
->n_bytes
, dst
->ofs
,
2278 mf_set_flow_value(dst
->field
, &dst_value
, flow
);
2282 /* Swaps the bits in 'src' and 'dst' within 'flow', and sets all the bits in
2283 * 'src' and 'dst' to 1s in 'wc', if 'wc' is nonnull.
2285 * 'src' and 'dst' may overlap. */
2287 mf_subfield_swap(const struct mf_subfield
*a
,
2288 const struct mf_subfield
*b
,
2289 struct flow
*flow
, struct flow_wildcards
*wc
)
2291 ovs_assert(a
->n_bits
== b
->n_bits
);
2292 if (mf_are_prereqs_ok(a
->field
, flow
, wc
)
2293 && mf_are_prereqs_ok(b
->field
, flow
, wc
)) {
2294 unwildcard_subfield(a
, wc
);
2295 unwildcard_subfield(b
, wc
);
2297 union mf_value a_value
;
2298 union mf_value b_value
;
2299 mf_get_value(a
->field
, flow
, &a_value
);
2300 mf_get_value(b
->field
, flow
, &b_value
);
2301 union mf_value b2_value
= b_value
;
2303 /* Copy 'a' into 'b'. */
2304 bitwise_copy(&a_value
, a
->field
->n_bytes
, a
->ofs
,
2305 &b_value
, b
->field
->n_bytes
, b
->ofs
,
2307 mf_set_flow_value(b
->field
, &b_value
, flow
);
2309 /* Copy original 'b' into 'a'. */
2310 bitwise_copy(&b2_value
, b
->field
->n_bytes
, b
->ofs
,
2311 &a_value
, a
->field
->n_bytes
, a
->ofs
,
2313 mf_set_flow_value(a
->field
, &a_value
, flow
);
2317 /* Checks whether 'sf' is valid for reading a subfield out of 'flow'. Returns
2318 * 0 if so, otherwise an OpenFlow error code (e.g. as returned by
2321 mf_check_src(const struct mf_subfield
*sf
, const struct match
*match
)
2323 return mf_check__(sf
, match
, "source");
2326 /* Checks whether 'sf' is valid for writing a subfield into 'flow'. Returns 0
2327 * if so, otherwise an OpenFlow error code (e.g. as returned by
2330 mf_check_dst(const struct mf_subfield
*sf
, const struct match
*match
)
2332 int error
= mf_check__(sf
, match
, "destination");
2333 if (!error
&& !sf
->field
->writable
) {
2334 VLOG_WARN_RL(&rl
, "destination field %s is not writable",
2336 return OFPERR_OFPBAC_BAD_SET_ARGUMENT
;
2341 /* Copies the value and wildcard bit pattern for 'mf' from 'match' into the
2342 * 'value' and 'mask', respectively. */
2344 mf_get(const struct mf_field
*mf
, const struct match
*match
,
2345 union mf_value
*value
, union mf_value
*mask
)
2347 mf_get_value(mf
, &match
->flow
, value
);
2348 mf_get_mask(mf
, &match
->wc
, mask
);
2352 mf_from_integer_string(const struct mf_field
*mf
, const char *s
,
2353 uint8_t *valuep
, uint8_t *maskp
)
2356 const char *err_str
= "";
2359 err
= parse_int_string(s
, valuep
, mf
->n_bytes
, &tail
);
2360 if (err
|| (*tail
!= '\0' && *tail
!= '/')) {
2366 err
= parse_int_string(tail
+ 1, maskp
, mf
->n_bytes
, &tail
);
2367 if (err
|| *tail
!= '\0') {
2372 memset(maskp
, 0xff, mf
->n_bytes
);
2378 if (err
== ERANGE
) {
2379 return xasprintf("%s: %s too large for %u-byte field %s",
2380 s
, err_str
, mf
->n_bytes
, mf
->name
);
2382 return xasprintf("%s: bad syntax for %s %s", s
, mf
->name
, err_str
);
2387 mf_from_ethernet_string(const struct mf_field
*mf
, const char *s
,
2388 struct eth_addr
*mac
, struct eth_addr
*mask
)
2392 ovs_assert(mf
->n_bytes
== ETH_ADDR_LEN
);
2395 if (ovs_scan(s
, ETH_ADDR_SCAN_FMT
"%n", ETH_ADDR_SCAN_ARGS(*mac
), &n
)
2396 && n
== strlen(s
)) {
2397 *mask
= eth_addr_exact
;
2402 if (ovs_scan(s
, ETH_ADDR_SCAN_FMT
"/"ETH_ADDR_SCAN_FMT
"%n",
2403 ETH_ADDR_SCAN_ARGS(*mac
), ETH_ADDR_SCAN_ARGS(*mask
), &n
)
2404 && n
== strlen(s
)) {
2408 return xasprintf("%s: invalid Ethernet address", s
);
2412 mf_from_ipv4_string(const struct mf_field
*mf
, const char *s
,
2413 ovs_be32
*ip
, ovs_be32
*mask
)
2415 ovs_assert(mf
->n_bytes
== sizeof *ip
);
2416 return ip_parse_masked(s
, ip
, mask
);
2420 mf_from_ipv6_string(const struct mf_field
*mf
, const char *s
,
2421 struct in6_addr
*ipv6
, struct in6_addr
*mask
)
2423 ovs_assert(mf
->n_bytes
== sizeof *ipv6
);
2424 return ipv6_parse_masked(s
, ipv6
, mask
);
2428 mf_from_ofp_port_string(const struct mf_field
*mf
, const char *s
,
2429 const struct ofputil_port_map
*port_map
,
2430 ovs_be16
*valuep
, ovs_be16
*maskp
)
2434 ovs_assert(mf
->n_bytes
== sizeof(ovs_be16
));
2436 if (ofputil_port_from_string(s
, port_map
, &port
)) {
2437 *valuep
= htons(ofp_to_u16(port
));
2438 *maskp
= OVS_BE16_MAX
;
2441 return xasprintf("%s: invalid or unknown port for %s", s
, mf
->name
);
2445 mf_from_ofp_port_string32(const struct mf_field
*mf
, const char *s
,
2446 const struct ofputil_port_map
*port_map
,
2447 ovs_be32
*valuep
, ovs_be32
*maskp
)
2451 ovs_assert(mf
->n_bytes
== sizeof(ovs_be32
));
2452 if (ofputil_port_from_string(s
, port_map
, &port
)) {
2453 *valuep
= ofputil_port_to_ofp11(port
);
2454 *maskp
= OVS_BE32_MAX
;
2457 return xasprintf("%s: port value out of range for %s", s
, mf
->name
);
2460 struct frag_handling
{
2466 static const struct frag_handling all_frags
[] = {
2467 #define A FLOW_NW_FRAG_ANY
2468 #define L FLOW_NW_FRAG_LATER
2469 /* name mask value */
2472 { "first", A
|L
, A
},
2473 { "later", A
|L
, A
|L
},
2478 { "not_later", L
, 0 },
2485 mf_from_frag_string(const char *s
, uint8_t *valuep
, uint8_t *maskp
)
2487 const struct frag_handling
*h
;
2489 for (h
= all_frags
; h
< &all_frags
[ARRAY_SIZE(all_frags
)]; h
++) {
2490 if (!strcasecmp(s
, h
->name
)) {
2491 /* We force the upper bits of the mask on to make mf_parse_value()
2492 * happy (otherwise it will never think it's an exact match.) */
2493 *maskp
= h
->mask
| ~FLOW_NW_FRAG_MASK
;
2499 return xasprintf("%s: unknown fragment type (valid types are \"no\", "
2500 "\"yes\", \"first\", \"later\", \"not_first\"", s
);
2504 parse_mf_flags(const char *s
, const char *(*bit_to_string
)(uint32_t),
2505 const char *field_name
, ovs_be16
*flagsp
, ovs_be16 allowed
,
2510 uint32_t flags
, mask
;
2512 err
= parse_flags(s
, bit_to_string
, '\0', field_name
, &err_str
,
2513 &flags
, ntohs(allowed
), maskp
? &mask
: NULL
);
2518 *flagsp
= htons(flags
);
2520 *maskp
= htons(mask
);
2527 mf_from_tcp_flags_string(const char *s
, ovs_be16
*flagsp
, ovs_be16
*maskp
)
2529 return parse_mf_flags(s
, packet_tcp_flag_to_string
, "TCP", flagsp
,
2530 TCP_FLAGS_BE16(OVS_BE16_MAX
), maskp
);
2534 mf_from_tun_flags_string(const char *s
, ovs_be16
*flagsp
, ovs_be16
*maskp
)
2536 return parse_mf_flags(s
, flow_tun_flag_to_string
, "tunnel", flagsp
,
2537 htons(FLOW_TNL_PUB_F_MASK
), maskp
);
2541 mf_from_ct_state_string(const char *s
, ovs_be32
*flagsp
, ovs_be32
*maskp
)
2545 uint32_t flags
, mask
;
2547 err
= parse_flags(s
, ct_state_to_string
, '\0', "ct_state", &err_str
,
2548 &flags
, CS_SUPPORTED_MASK
, maskp
? &mask
: NULL
);
2553 *flagsp
= htonl(flags
);
2555 *maskp
= htonl(mask
);
2561 /* Parses 's', a string value for field 'mf', into 'value' and 'mask'. Returns
2562 * NULL if successful, otherwise a malloc()'d string describing the error. */
2564 mf_parse(const struct mf_field
*mf
, const char *s
,
2565 const struct ofputil_port_map
*port_map
,
2566 union mf_value
*value
, union mf_value
*mask
)
2570 if (!strcmp(s
, "*")) {
2571 memset(value
, 0, mf
->n_bytes
);
2572 memset(mask
, 0, mf
->n_bytes
);
2576 switch (mf
->string
) {
2578 case MFS_HEXADECIMAL
:
2579 error
= mf_from_integer_string(mf
, s
,
2580 (uint8_t *) value
, (uint8_t *) mask
);
2584 ovs_assert(mf
->n_bytes
== sizeof(ovs_be32
));
2585 error
= mf_from_ct_state_string(s
, &value
->be32
, &mask
->be32
);
2589 error
= mf_from_ethernet_string(mf
, s
, &value
->mac
, &mask
->mac
);
2593 error
= mf_from_ipv4_string(mf
, s
, &value
->be32
, &mask
->be32
);
2597 error
= mf_from_ipv6_string(mf
, s
, &value
->ipv6
, &mask
->ipv6
);
2601 error
= mf_from_ofp_port_string(mf
, s
, port_map
,
2602 &value
->be16
, &mask
->be16
);
2605 case MFS_OFP_PORT_OXM
:
2606 error
= mf_from_ofp_port_string32(mf
, s
, port_map
,
2607 &value
->be32
, &mask
->be32
);
2611 error
= mf_from_frag_string(s
, &value
->u8
, &mask
->u8
);
2615 ovs_assert(mf
->n_bytes
== sizeof(ovs_be16
));
2616 error
= mf_from_tun_flags_string(s
, &value
->be16
, &mask
->be16
);
2620 ovs_assert(mf
->n_bytes
== sizeof(ovs_be16
));
2621 error
= mf_from_tcp_flags_string(s
, &value
->be16
, &mask
->be16
);
2628 if (!error
&& !mf_is_mask_valid(mf
, mask
)) {
2629 error
= xasprintf("%s: invalid mask for field %s", s
, mf
->name
);
2634 /* Parses 's', a string value for field 'mf', into 'value'. Returns NULL if
2635 * successful, otherwise a malloc()'d string describing the error. */
2637 mf_parse_value(const struct mf_field
*mf
, const char *s
,
2638 const struct ofputil_port_map
*port_map
, union mf_value
*value
)
2640 union mf_value mask
;
2643 error
= mf_parse(mf
, s
, port_map
, value
, &mask
);
2648 if (!is_all_ones((const uint8_t *) &mask
, mf
->n_bytes
)) {
2649 return xasprintf("%s: wildcards not allowed here", s
);
2655 mf_format_integer_string(const struct mf_field
*mf
, const uint8_t *valuep
,
2656 const uint8_t *maskp
, struct ds
*s
)
2658 if (mf
->string
== MFS_HEXADECIMAL
) {
2659 ds_put_hex(s
, valuep
, mf
->n_bytes
);
2661 unsigned long long int integer
= 0;
2664 ovs_assert(mf
->n_bytes
<= 8);
2665 for (i
= 0; i
< mf
->n_bytes
; i
++) {
2666 integer
= (integer
<< 8) | valuep
[i
];
2668 ds_put_format(s
, "%lld", integer
);
2672 /* I guess we could write the mask in decimal for MFS_DECIMAL but I'm
2673 * not sure that that a bit-mask written in decimal is ever easier to
2674 * understand than the same bit-mask written in hexadecimal. */
2675 ds_put_char(s
, '/');
2676 ds_put_hex(s
, maskp
, mf
->n_bytes
);
2681 mf_format_frag_string(uint8_t value
, uint8_t mask
, struct ds
*s
)
2683 const struct frag_handling
*h
;
2685 mask
&= FLOW_NW_FRAG_MASK
;
2688 for (h
= all_frags
; h
< &all_frags
[ARRAY_SIZE(all_frags
)]; h
++) {
2689 if (value
== h
->value
&& mask
== h
->mask
) {
2690 ds_put_cstr(s
, h
->name
);
2694 ds_put_cstr(s
, "<error>");
2698 mf_format_tnl_flags_string(ovs_be16 value
, ovs_be16 mask
, struct ds
*s
)
2700 format_flags_masked(s
, NULL
, flow_tun_flag_to_string
, ntohs(value
),
2701 ntohs(mask
) & FLOW_TNL_PUB_F_MASK
, FLOW_TNL_PUB_F_MASK
);
2705 mf_format_tcp_flags_string(ovs_be16 value
, ovs_be16 mask
, struct ds
*s
)
2707 format_flags_masked(s
, NULL
, packet_tcp_flag_to_string
, ntohs(value
),
2708 TCP_FLAGS(mask
), TCP_FLAGS(OVS_BE16_MAX
));
2712 mf_format_ct_state_string(ovs_be32 value
, ovs_be32 mask
, struct ds
*s
)
2714 format_flags_masked(s
, NULL
, ct_state_to_string
, ntohl(value
),
2715 ntohl(mask
), UINT16_MAX
);
2718 /* Appends to 's' a string representation of field 'mf' whose value is in
2719 * 'value' and 'mask'. 'mask' may be NULL to indicate an exact match. */
2721 mf_format(const struct mf_field
*mf
,
2722 const union mf_value
*value
, const union mf_value
*mask
,
2723 const struct ofputil_port_map
*port_map
,
2727 if (is_all_zeros(mask
, mf
->n_bytes
)) {
2728 ds_put_cstr(s
, "ANY");
2730 } else if (is_all_ones(mask
, mf
->n_bytes
)) {
2735 switch (mf
->string
) {
2736 case MFS_OFP_PORT_OXM
:
2739 ofputil_port_from_ofp11(value
->be32
, &port
);
2740 ofputil_format_port(port
, port_map
, s
);
2746 ofputil_format_port(u16_to_ofp(ntohs(value
->be16
)), port_map
, s
);
2751 case MFS_HEXADECIMAL
:
2752 mf_format_integer_string(mf
, (uint8_t *) value
, (uint8_t *) mask
, s
);
2756 mf_format_ct_state_string(value
->be32
,
2757 mask
? mask
->be32
: OVS_BE32_MAX
, s
);
2761 eth_format_masked(value
->mac
, mask
? &mask
->mac
: NULL
, s
);
2765 ip_format_masked(value
->be32
, mask
? mask
->be32
: OVS_BE32_MAX
, s
);
2769 ipv6_format_masked(&value
->ipv6
, mask
? &mask
->ipv6
: NULL
, s
);
2773 mf_format_frag_string(value
->u8
, mask
? mask
->u8
: UINT8_MAX
, s
);
2777 mf_format_tnl_flags_string(value
->be16
,
2778 mask
? mask
->be16
: OVS_BE16_MAX
, s
);
2782 mf_format_tcp_flags_string(value
->be16
,
2783 mask
? mask
->be16
: OVS_BE16_MAX
, s
);
2791 /* Makes subfield 'sf' within 'flow' exactly match the 'sf->n_bits'
2792 * least-significant bits in 'x'.
2795 mf_write_subfield_flow(const struct mf_subfield
*sf
,
2796 const union mf_subvalue
*x
, struct flow
*flow
)
2798 const struct mf_field
*field
= sf
->field
;
2799 union mf_value value
;
2801 mf_get_value(field
, flow
, &value
);
2802 bitwise_copy(x
, sizeof *x
, 0, &value
, field
->n_bytes
,
2803 sf
->ofs
, sf
->n_bits
);
2804 mf_set_flow_value(field
, &value
, flow
);
2807 /* Makes subfield 'sf' within 'match' exactly match the 'sf->n_bits'
2808 * least-significant bits in 'x'.
2811 mf_write_subfield(const struct mf_subfield
*sf
, const union mf_subvalue
*x
,
2812 struct match
*match
)
2814 const struct mf_field
*field
= sf
->field
;
2815 union mf_value value
, mask
;
2817 mf_get(field
, match
, &value
, &mask
);
2818 bitwise_copy(x
, sizeof *x
, 0, &value
, field
->n_bytes
, sf
->ofs
, sf
->n_bits
);
2819 bitwise_one ( &mask
, field
->n_bytes
, sf
->ofs
, sf
->n_bits
);
2820 mf_set(field
, &value
, &mask
, match
, NULL
);
2824 mf_write_subfield_value(const struct mf_subfield
*sf
, const void *src
,
2825 struct match
*match
)
2827 const struct mf_field
*field
= sf
->field
;
2828 union mf_value value
, mask
;
2829 unsigned int size
= DIV_ROUND_UP(sf
->n_bits
, 8);
2831 mf_get(field
, match
, &value
, &mask
);
2832 bitwise_copy(src
, size
, 0, &value
, field
->n_bytes
, sf
->ofs
, sf
->n_bits
);
2833 bitwise_one ( &mask
, field
->n_bytes
, sf
->ofs
, sf
->n_bits
);
2834 mf_set(field
, &value
, &mask
, match
, NULL
);
2837 /* 'v' and 'm' correspond to values of 'field'. This function copies them into
2838 * 'match' in the correspond positions. */
2840 mf_mask_subfield(const struct mf_field
*field
,
2841 const union mf_subvalue
*v
,
2842 const union mf_subvalue
*m
,
2843 struct match
*match
)
2845 union mf_value value
, mask
;
2847 mf_get(field
, match
, &value
, &mask
);
2848 bitwise_copy(v
, sizeof *v
, 0, &value
, field
->n_bytes
, 0, field
->n_bits
);
2849 bitwise_copy(m
, sizeof *m
, 0, &mask
, field
->n_bytes
, 0, field
->n_bits
);
2850 mf_set(field
, &value
, &mask
, match
, NULL
);
2853 /* Initializes 'x' to the value of 'sf' within 'flow'. 'sf' must be valid for
2854 * reading 'flow', e.g. as checked by mf_check_src(). */
2856 mf_read_subfield(const struct mf_subfield
*sf
, const struct flow
*flow
,
2857 union mf_subvalue
*x
)
2859 union mf_value value
;
2861 mf_get_value(sf
->field
, flow
, &value
);
2863 memset(x
, 0, sizeof *x
);
2864 bitwise_copy(&value
, sf
->field
->n_bytes
, sf
->ofs
,
2869 /* Returns the value of 'sf' within 'flow'. 'sf' must be valid for reading
2870 * 'flow', e.g. as checked by mf_check_src() and sf->n_bits must be 64 or
2873 mf_get_subfield(const struct mf_subfield
*sf
, const struct flow
*flow
)
2875 union mf_value value
;
2877 mf_get_value(sf
->field
, flow
, &value
);
2878 return bitwise_get(&value
, sf
->field
->n_bytes
, sf
->ofs
, sf
->n_bits
);
2882 mf_format_subvalue(const union mf_subvalue
*subvalue
, struct ds
*s
)
2884 ds_put_hex(s
, subvalue
->u8
, sizeof subvalue
->u8
);
2888 field_array_set(enum mf_field_id id
, const union mf_value
*value
,
2889 struct field_array
*fa
)
2891 size_t i
, offset
= 0;
2893 ovs_assert(id
< MFF_N_IDS
);
2895 /* Find the spot for 'id'. */
2896 BITMAP_FOR_EACH_1 (i
, id
, fa
->used
.bm
) {
2897 offset
+= mf_from_id(i
)->n_bytes
;
2900 size_t value_size
= mf_from_id(id
)->n_bytes
;
2902 /* make room if necessary. */
2903 if (!bitmap_is_set(fa
->used
.bm
, id
)) {
2904 fa
->values
= xrealloc(fa
->values
, fa
->values_size
+ value_size
);
2905 /* Move remainder forward, if any. */
2906 if (offset
< fa
->values_size
) {
2907 memmove(fa
->values
+ offset
+ value_size
, fa
->values
+ offset
,
2908 fa
->values_size
- offset
);
2910 fa
->values_size
+= value_size
;
2912 bitmap_set1(fa
->used
.bm
, id
);
2914 memcpy(fa
->values
+ offset
, value
, value_size
);
2917 /* A wrapper for variable length mf_fields that is maintained by
2918 * struct vl_mff_map.*/
2919 struct vl_mf_field
{
2921 struct ovs_refcount ref_cnt
;
2922 struct cmap_node cmap_node
; /* In ofproto->vl_mff_map->cmap. */
2925 static inline uint32_t
2926 mf_field_hash(uint32_t key
)
2928 return hash_int(key
, 0);
2932 vmf_delete(struct vl_mf_field
*vmf
)
2934 if (ovs_refcount_unref(&vmf
->ref_cnt
) == 1) {
2935 /* Postpone as this function is typically called immediately
2936 * after removing from cmap. */
2937 ovsrcu_postpone(free
, vmf
);
2940 "Attempted to delete VMF %s but refcount is nonzero!",
2946 mf_vl_mff_map_clear(struct vl_mff_map
*vl_mff_map
, bool force
)
2947 OVS_REQUIRES(vl_mff_map
->mutex
)
2949 struct vl_mf_field
*vmf
;
2952 CMAP_FOR_EACH (vmf
, cmap_node
, &vl_mff_map
->cmap
) {
2953 if (ovs_refcount_read(&vmf
->ref_cnt
) != 1) {
2954 return OFPERR_NXTTMFC_INVALID_TLV_DEL
;
2959 CMAP_FOR_EACH (vmf
, cmap_node
, &vl_mff_map
->cmap
) {
2960 cmap_remove(&vl_mff_map
->cmap
, &vmf
->cmap_node
,
2961 mf_field_hash(vmf
->mf
.id
));
2968 static struct vl_mf_field
*
2969 mf_get_vl_mff__(uint32_t id
, const struct vl_mff_map
*vl_mff_map
)
2971 struct vl_mf_field
*vmf
;
2973 CMAP_FOR_EACH_WITH_HASH (vmf
, cmap_node
, mf_field_hash(id
),
2974 &vl_mff_map
->cmap
) {
2975 if (vmf
->mf
.id
== id
) {
2983 /* If 'mff' is a variable length field, looks up 'vl_mff_map', returns a
2984 * pointer to the variable length meta-flow field corresponding to 'mff'.
2985 * Returns NULL if no mapping is existed for 'mff'. */
2986 const struct mf_field
*
2987 mf_get_vl_mff(const struct mf_field
*mff
,
2988 const struct vl_mff_map
*vl_mff_map
)
2990 if (mff
&& mff
->variable_len
&& vl_mff_map
) {
2991 return &mf_get_vl_mff__(mff
->id
, vl_mff_map
)->mf
;
2998 mf_vl_mff_map_del(struct vl_mff_map
*vl_mff_map
,
2999 const struct ofputil_tlv_table_mod
*ttm
, bool force
)
3000 OVS_REQUIRES(vl_mff_map
->mutex
)
3002 struct ofputil_tlv_map
*tlv_map
;
3003 struct vl_mf_field
*vmf
;
3007 LIST_FOR_EACH (tlv_map
, list_node
, &ttm
->mappings
) {
3008 idx
= MFF_TUN_METADATA0
+ tlv_map
->index
;
3009 if (idx
>= MFF_TUN_METADATA0
+ TUN_METADATA_NUM_OPTS
) {
3010 return OFPERR_NXTTMFC_BAD_FIELD_IDX
;
3013 vmf
= mf_get_vl_mff__(idx
, vl_mff_map
);
3014 if (vmf
&& ovs_refcount_read(&vmf
->ref_cnt
) != 1) {
3015 return OFPERR_NXTTMFC_INVALID_TLV_DEL
;
3020 LIST_FOR_EACH (tlv_map
, list_node
, &ttm
->mappings
) {
3021 idx
= MFF_TUN_METADATA0
+ tlv_map
->index
;
3022 if (idx
>= MFF_TUN_METADATA0
+ TUN_METADATA_NUM_OPTS
) {
3023 return OFPERR_NXTTMFC_BAD_FIELD_IDX
;
3026 vmf
= mf_get_vl_mff__(idx
, vl_mff_map
);
3028 cmap_remove(&vl_mff_map
->cmap
, &vmf
->cmap_node
,
3029 mf_field_hash(idx
));
3038 mf_vl_mff_map_add(struct vl_mff_map
*vl_mff_map
,
3039 const struct ofputil_tlv_table_mod
*ttm
)
3040 OVS_REQUIRES(vl_mff_map
->mutex
)
3042 struct ofputil_tlv_map
*tlv_map
;
3043 struct vl_mf_field
*vmf
;
3046 LIST_FOR_EACH (tlv_map
, list_node
, &ttm
->mappings
) {
3047 idx
= MFF_TUN_METADATA0
+ tlv_map
->index
;
3048 if (idx
>= MFF_TUN_METADATA0
+ TUN_METADATA_NUM_OPTS
) {
3049 return OFPERR_NXTTMFC_BAD_FIELD_IDX
;
3052 vmf
= xmalloc(sizeof *vmf
);
3053 vmf
->mf
= mf_fields
[idx
];
3054 vmf
->mf
.n_bytes
= tlv_map
->option_len
;
3055 vmf
->mf
.n_bits
= tlv_map
->option_len
* 8;
3056 vmf
->mf
.mapped
= true;
3057 ovs_refcount_init(&vmf
->ref_cnt
);
3059 cmap_insert(&vl_mff_map
->cmap
, &vmf
->cmap_node
,
3060 mf_field_hash(idx
));
3066 /* Updates the tun_metadata mf_field in 'vl_mff_map' according to 'ttm'.
3067 * This function must be invoked after tun_metadata_table_mod().
3068 * Returns OFPERR_NXTTMFC_BAD_FIELD_IDX, if the index for the vl_mf_field is
3070 * Returns OFPERR_NXTTMFC_INVALID_TLV_DEL, if 'ttm' tries to delete an
3071 * vl_mf_field that is still used by any active flow.*/
3073 mf_vl_mff_map_mod_from_tun_metadata(struct vl_mff_map
*vl_mff_map
,
3074 const struct ofputil_tlv_table_mod
*ttm
)
3075 OVS_REQUIRES(vl_mff_map
->mutex
)
3077 switch (ttm
->command
) {
3079 return mf_vl_mff_map_add(vl_mff_map
, ttm
);
3082 return mf_vl_mff_map_del(vl_mff_map
, ttm
, false);
3085 return mf_vl_mff_map_clear(vl_mff_map
, false);
3094 /* Returns true if a variable length meta-flow field 'mff' is not mapped in
3095 * the 'vl_mff_map'. */
3097 mf_vl_mff_invalid(const struct mf_field
*mff
, const struct vl_mff_map
*map
)
3099 return map
&& mff
&& mff
->variable_len
&& !mff
->mapped
;
3103 mf_vl_mff_set_tlv_bitmap(const struct mf_field
*mff
, uint64_t *tlv_bitmap
)
3105 if (mff
&& mff
->mapped
) {
3106 ovs_assert(mf_is_tun_metadata(mff
));
3107 ULLONG_SET1(*tlv_bitmap
, mff
->id
- MFF_TUN_METADATA0
);
3112 mf_vl_mff_ref_cnt_mod(const struct vl_mff_map
*map
, uint64_t tlv_bitmap
,
3115 struct vl_mf_field
*vmf
;
3119 ULLONG_FOR_EACH_1 (i
, tlv_bitmap
) {
3120 vmf
= mf_get_vl_mff__(i
+ MFF_TUN_METADATA0
, map
);
3123 ovs_refcount_ref(&vmf
->ref_cnt
);
3125 ovs_refcount_unref(&vmf
->ref_cnt
);
3128 VLOG_WARN("Invalid TLV index %d.", i
);
3135 mf_vl_mff_ref(const struct vl_mff_map
*map
, uint64_t tlv_bitmap
)
3137 mf_vl_mff_ref_cnt_mod(map
, tlv_bitmap
, true);
3141 mf_vl_mff_unref(const struct vl_mff_map
*map
, uint64_t tlv_bitmap
)
3143 mf_vl_mff_ref_cnt_mod(map
, tlv_bitmap
, false);
3147 mf_vl_mff_nx_pull_header(struct ofpbuf
*b
, const struct vl_mff_map
*vl_mff_map
,
3148 const struct mf_field
**field
, bool *masked
,
3149 uint64_t *tlv_bitmap
)
3151 enum ofperr error
= nx_pull_header(b
, vl_mff_map
, field
, masked
);
3156 mf_vl_mff_set_tlv_bitmap(*field
, tlv_bitmap
);
3161 mf_vl_mff_nx_pull_entry(struct ofpbuf
*b
, const struct vl_mff_map
*vl_mff_map
,
3162 const struct mf_field
**field
, union mf_value
*value
,
3163 union mf_value
*mask
, uint64_t *tlv_bitmap
)
3165 enum ofperr error
= nx_pull_entry(b
, vl_mff_map
, field
, value
, mask
);
3170 mf_vl_mff_set_tlv_bitmap(*field
, tlv_bitmap
);
3175 mf_vl_mff_mf_from_nxm_header(uint32_t header
,
3176 const struct vl_mff_map
*vl_mff_map
,
3177 const struct mf_field
**field
,
3178 uint64_t *tlv_bitmap
)
3180 *field
= mf_from_nxm_header(header
, vl_mff_map
);
3181 if (mf_vl_mff_invalid(*field
, vl_mff_map
)) {
3182 return OFPERR_NXFMFC_INVALID_TLV_FIELD
;
3185 mf_vl_mff_set_tlv_bitmap(*field
, tlv_bitmap
);