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 is_ethernet(flow
, wc
);
410 return (flow
->dl_type
== htons(ETH_TYPE_ARP
) ||
411 flow
->dl_type
== htons(ETH_TYPE_RARP
));
413 return flow
->dl_type
== htons(ETH_TYPE_IP
);
415 return flow
->dl_type
== htons(ETH_TYPE_IPV6
);
417 return is_vlan(flow
, wc
);
419 return eth_type_mpls(flow
->dl_type
);
421 return is_ip_any(flow
);
423 return is_ct_valid(flow
, mask
, wc
);
425 /* Matching !FRAG_LATER is not enforced (mask is not checked). */
426 return is_tcp(flow
, wc
) && !(flow
->nw_frag
& FLOW_NW_FRAG_LATER
);
428 return is_udp(flow
, wc
) && !(flow
->nw_frag
& FLOW_NW_FRAG_LATER
);
430 return is_sctp(flow
, wc
) && !(flow
->nw_frag
& FLOW_NW_FRAG_LATER
);
432 return is_icmpv4(flow
, wc
);
434 return is_icmpv6(flow
, wc
);
436 return is_nd(flow
, wc
);
438 return is_nd(flow
, wc
) && flow
->tp_src
== htons(ND_NEIGHBOR_SOLICIT
);
440 return is_nd(flow
, wc
) && flow
->tp_src
== htons(ND_NEIGHBOR_ADVERT
);
446 /* Returns true if 'flow' meets the prerequisites for 'mf', false otherwise.
447 * Sets inspected bits in 'wc', if non-NULL. */
449 mf_are_prereqs_ok(const struct mf_field
*mf
, const struct flow
*flow
,
450 struct flow_wildcards
*wc
)
452 return mf_are_prereqs_ok__(mf
, flow
, NULL
, wc
);
455 /* Returns true if 'match' meets the prerequisites for 'mf', false otherwise.
458 mf_are_match_prereqs_ok(const struct mf_field
*mf
, const struct match
*match
)
460 return mf_are_prereqs_ok__(mf
, &match
->flow
, &match
->wc
, NULL
);
463 /* Returns true if 'value' may be a valid value *as part of a masked match*,
466 * A value is not rejected just because it is not valid for the field in
467 * question, but only if it doesn't make sense to test the bits in question at
468 * all. For example, the MFF_VLAN_TCI field will never have a nonzero value
469 * without the VLAN_CFI bit being set, but we can't reject those values because
470 * it is still legitimate to test just for those bits (see the documentation
471 * for NXM_OF_VLAN_TCI in meta-flow.h). On the other hand, there is never a
472 * reason to set the low bit of MFF_IP_DSCP to 1, so we reject that. */
474 mf_is_value_valid(const struct mf_field
*mf
, const union mf_value
*value
)
483 case MFF_TUN_IPV6_SRC
:
484 case MFF_TUN_IPV6_DST
:
488 case MFF_TUN_GBP_FLAGS
:
489 CASE_MFF_TUN_METADATA
:
492 case MFF_SKB_PRIORITY
:
497 case MFF_CT_NW_PROTO
:
500 case MFF_CT_IPV6_SRC
:
501 case MFF_CT_IPV6_DST
:
528 case MFF_ICMPV4_TYPE
:
529 case MFF_ICMPV4_CODE
:
530 case MFF_ICMPV6_TYPE
:
531 case MFF_ICMPV6_CODE
:
537 case MFF_IN_PORT_OXM
:
538 case MFF_ACTSET_OUTPUT
: {
540 return !ofputil_port_from_ofp11(value
->be32
, &port
);
544 return !(value
->u8
& ~IP_DSCP_MASK
);
545 case MFF_IP_DSCP_SHIFTED
:
546 return !(value
->u8
& (~IP_DSCP_MASK
>> 2));
548 return !(value
->u8
& ~IP_ECN_MASK
);
550 return !(value
->u8
& ~FLOW_NW_FRAG_MASK
);
552 return !(value
->be16
& ~htons(0x0fff));
555 return !(value
->be16
& htons(0xff00));
558 return !(value
->be16
& htons(VLAN_CFI
| VLAN_PCP_MASK
));
560 return !(value
->be16
& htons(VLAN_PCP_MASK
));
562 case MFF_DL_VLAN_PCP
:
564 return !(value
->u8
& ~(VLAN_PCP_MASK
>> VLAN_PCP_SHIFT
));
567 return !(value
->be32
& ~htonl(IPV6_LABEL_MASK
));
570 return !(value
->be32
& ~htonl(MPLS_LABEL_MASK
>> MPLS_LABEL_SHIFT
));
573 return !(value
->u8
& ~(MPLS_TC_MASK
>> MPLS_TC_SHIFT
));
576 return !(value
->u8
& ~(MPLS_BOS_MASK
>> MPLS_BOS_SHIFT
));
579 return !(value
->be16
& ~htons(FLOW_TNL_PUB_F_MASK
));
582 return !(value
->be32
& ~htonl(CS_SUPPORTED_MASK
));
590 /* Copies the value of field 'mf' from 'flow' into 'value'. The caller is
591 * responsible for ensuring that 'flow' meets 'mf''s prerequisites. */
593 mf_get_value(const struct mf_field
*mf
, const struct flow
*flow
,
594 union mf_value
*value
)
598 value
->be32
= htonl(flow
->dp_hash
);
601 value
->be32
= htonl(flow
->recirc_id
);
604 value
->be32
= htonl(flow
->conj_id
);
607 value
->be64
= flow
->tunnel
.tun_id
;
610 value
->be32
= flow
->tunnel
.ip_src
;
613 value
->be32
= flow
->tunnel
.ip_dst
;
615 case MFF_TUN_IPV6_SRC
:
616 value
->ipv6
= flow
->tunnel
.ipv6_src
;
618 case MFF_TUN_IPV6_DST
:
619 value
->ipv6
= flow
->tunnel
.ipv6_dst
;
622 value
->be16
= htons(flow
->tunnel
.flags
& FLOW_TNL_PUB_F_MASK
);
625 value
->be16
= flow
->tunnel
.gbp_id
;
627 case MFF_TUN_GBP_FLAGS
:
628 value
->u8
= flow
->tunnel
.gbp_flags
;
631 value
->u8
= flow
->tunnel
.ip_ttl
;
634 value
->u8
= flow
->tunnel
.ip_tos
;
636 CASE_MFF_TUN_METADATA
:
637 tun_metadata_read(&flow
->tunnel
, mf
, value
);
641 value
->be64
= flow
->metadata
;
645 value
->be16
= htons(ofp_to_u16(flow
->in_port
.ofp_port
));
647 case MFF_IN_PORT_OXM
:
648 value
->be32
= ofputil_port_to_ofp11(flow
->in_port
.ofp_port
);
650 case MFF_ACTSET_OUTPUT
:
651 value
->be32
= ofputil_port_to_ofp11(flow
->actset_output
);
654 case MFF_SKB_PRIORITY
:
655 value
->be32
= htonl(flow
->skb_priority
);
659 value
->be32
= htonl(flow
->pkt_mark
);
663 value
->be32
= htonl(flow
->ct_state
);
667 value
->be16
= htons(flow
->ct_zone
);
671 value
->be32
= htonl(flow
->ct_mark
);
675 value
->be128
= hton128(flow
->ct_label
);
678 case MFF_CT_NW_PROTO
:
679 value
->u8
= flow
->ct_nw_proto
;
683 value
->be32
= flow
->ct_nw_src
;
687 value
->be32
= flow
->ct_nw_dst
;
690 case MFF_CT_IPV6_SRC
:
691 value
->ipv6
= flow
->ct_ipv6_src
;
694 case MFF_CT_IPV6_DST
:
695 value
->ipv6
= flow
->ct_ipv6_dst
;
699 value
->be16
= flow
->ct_tp_src
;
703 value
->be16
= flow
->ct_tp_dst
;
707 value
->be32
= htonl(flow
->regs
[mf
->id
- MFF_REG0
]);
711 value
->be64
= htonll(flow_get_xreg(flow
, mf
->id
- MFF_XREG0
));
715 value
->be128
= hton128(flow_get_xxreg(flow
, mf
->id
- MFF_XXREG0
));
719 value
->mac
= flow
->dl_src
;
723 value
->mac
= flow
->dl_dst
;
727 value
->be16
= flow
->dl_type
;
731 value
->be16
= flow
->vlans
[0].tci
;
735 value
->be16
= flow
->vlans
[0].tci
& htons(VLAN_VID_MASK
);
738 value
->be16
= flow
->vlans
[0].tci
& htons(VLAN_VID_MASK
| VLAN_CFI
);
741 case MFF_DL_VLAN_PCP
:
743 value
->u8
= vlan_tci_to_pcp(flow
->vlans
[0].tci
);
747 value
->be32
= htonl(mpls_lse_to_label(flow
->mpls_lse
[0]));
751 value
->u8
= mpls_lse_to_tc(flow
->mpls_lse
[0]);
755 value
->u8
= mpls_lse_to_bos(flow
->mpls_lse
[0]);
759 value
->u8
= mpls_lse_to_ttl(flow
->mpls_lse
[0]);
763 value
->be32
= flow
->nw_src
;
767 value
->be32
= flow
->nw_dst
;
771 value
->ipv6
= flow
->ipv6_src
;
775 value
->ipv6
= flow
->ipv6_dst
;
779 value
->be32
= flow
->ipv6_label
;
783 value
->u8
= flow
->nw_proto
;
787 value
->u8
= flow
->nw_tos
& IP_DSCP_MASK
;
790 case MFF_IP_DSCP_SHIFTED
:
791 value
->u8
= flow
->nw_tos
>> 2;
795 value
->u8
= flow
->nw_tos
& IP_ECN_MASK
;
799 value
->u8
= flow
->nw_ttl
;
803 value
->u8
= flow
->nw_frag
;
807 value
->be16
= htons(flow
->nw_proto
);
811 value
->be32
= flow
->nw_src
;
815 value
->be32
= flow
->nw_dst
;
820 value
->mac
= flow
->arp_sha
;
825 value
->mac
= flow
->arp_tha
;
831 value
->be16
= flow
->tp_src
;
837 value
->be16
= flow
->tp_dst
;
841 value
->be16
= flow
->tcp_flags
;
844 case MFF_ICMPV4_TYPE
:
845 case MFF_ICMPV6_TYPE
:
846 value
->u8
= ntohs(flow
->tp_src
);
849 case MFF_ICMPV4_CODE
:
850 case MFF_ICMPV6_CODE
:
851 value
->u8
= ntohs(flow
->tp_dst
);
855 value
->ipv6
= flow
->nd_target
;
864 /* Makes 'match' match field 'mf' exactly, with the value matched taken from
865 * 'value'. The caller is responsible for ensuring that 'match' meets 'mf''s
868 * If non-NULL, 'err_str' returns a malloc'ed string describing any errors
869 * with the request or NULL if there is no error. The caller is reponsible
870 * for freeing the string. */
872 mf_set_value(const struct mf_field
*mf
,
873 const union mf_value
*value
, struct match
*match
, char **err_str
)
881 match_set_dp_hash(match
, ntohl(value
->be32
));
884 match_set_recirc_id(match
, ntohl(value
->be32
));
887 match_set_conj_id(match
, ntohl(value
->be32
));
890 match_set_tun_id(match
, value
->be64
);
893 match_set_tun_src(match
, value
->be32
);
896 match_set_tun_dst(match
, value
->be32
);
898 case MFF_TUN_IPV6_SRC
:
899 match_set_tun_ipv6_src(match
, &value
->ipv6
);
901 case MFF_TUN_IPV6_DST
:
902 match_set_tun_ipv6_dst(match
, &value
->ipv6
);
905 match_set_tun_flags(match
, ntohs(value
->be16
));
908 match_set_tun_gbp_id(match
, value
->be16
);
910 case MFF_TUN_GBP_FLAGS
:
911 match_set_tun_gbp_flags(match
, value
->u8
);
914 match_set_tun_tos(match
, value
->u8
);
917 match_set_tun_ttl(match
, value
->u8
);
919 CASE_MFF_TUN_METADATA
:
920 tun_metadata_set_match(mf
, value
, NULL
, match
, err_str
);
924 match_set_metadata(match
, value
->be64
);
928 match_set_in_port(match
, u16_to_ofp(ntohs(value
->be16
)));
931 case MFF_IN_PORT_OXM
: {
933 ofputil_port_from_ofp11(value
->be32
, &port
);
934 match_set_in_port(match
, port
);
937 case MFF_ACTSET_OUTPUT
: {
939 ofputil_port_from_ofp11(value
->be32
, &port
);
940 match_set_actset_output(match
, port
);
944 case MFF_SKB_PRIORITY
:
945 match_set_skb_priority(match
, ntohl(value
->be32
));
949 match_set_pkt_mark(match
, ntohl(value
->be32
));
953 match_set_ct_state(match
, ntohl(value
->be32
));
957 match_set_ct_zone(match
, ntohs(value
->be16
));
961 match_set_ct_mark(match
, ntohl(value
->be32
));
965 match_set_ct_label(match
, ntoh128(value
->be128
));
968 case MFF_CT_NW_PROTO
:
969 match_set_ct_nw_proto(match
, value
->u8
);
973 match_set_ct_nw_src(match
, value
->be32
);
977 match_set_ct_nw_dst(match
, value
->be32
);
980 case MFF_CT_IPV6_SRC
:
981 match_set_ct_ipv6_src(match
, &value
->ipv6
);
984 case MFF_CT_IPV6_DST
:
985 match_set_ct_ipv6_dst(match
, &value
->ipv6
);
989 match_set_ct_tp_src(match
, value
->be16
);
993 match_set_ct_tp_dst(match
, value
->be16
);
997 match_set_reg(match
, mf
->id
- MFF_REG0
, ntohl(value
->be32
));
1001 match_set_xreg(match
, mf
->id
- MFF_XREG0
, ntohll(value
->be64
));
1005 match_set_xxreg(match
, mf
->id
- MFF_XXREG0
, ntoh128(value
->be128
));
1009 match_set_dl_src(match
, value
->mac
);
1013 match_set_dl_dst(match
, value
->mac
);
1017 match_set_dl_type(match
, value
->be16
);
1021 match_set_dl_tci(match
, value
->be16
);
1025 match_set_dl_vlan(match
, value
->be16
);
1028 match_set_vlan_vid(match
, value
->be16
);
1031 case MFF_DL_VLAN_PCP
:
1033 match_set_dl_vlan_pcp(match
, value
->u8
);
1036 case MFF_MPLS_LABEL
:
1037 match_set_mpls_label(match
, 0, value
->be32
);
1041 match_set_mpls_tc(match
, 0, value
->u8
);
1045 match_set_mpls_bos(match
, 0, value
->u8
);
1049 match_set_mpls_ttl(match
, 0, value
->u8
);
1053 match_set_nw_src(match
, value
->be32
);
1057 match_set_nw_dst(match
, value
->be32
);
1061 match_set_ipv6_src(match
, &value
->ipv6
);
1065 match_set_ipv6_dst(match
, &value
->ipv6
);
1068 case MFF_IPV6_LABEL
:
1069 match_set_ipv6_label(match
, value
->be32
);
1073 match_set_nw_proto(match
, value
->u8
);
1077 match_set_nw_dscp(match
, value
->u8
);
1080 case MFF_IP_DSCP_SHIFTED
:
1081 match_set_nw_dscp(match
, value
->u8
<< 2);
1085 match_set_nw_ecn(match
, value
->u8
);
1089 match_set_nw_ttl(match
, value
->u8
);
1093 match_set_nw_frag(match
, value
->u8
);
1097 match_set_nw_proto(match
, ntohs(value
->be16
));
1101 match_set_nw_src(match
, value
->be32
);
1105 match_set_nw_dst(match
, value
->be32
);
1110 match_set_arp_sha(match
, value
->mac
);
1115 match_set_arp_tha(match
, value
->mac
);
1121 match_set_tp_src(match
, value
->be16
);
1127 match_set_tp_dst(match
, value
->be16
);
1131 match_set_tcp_flags(match
, value
->be16
);
1134 case MFF_ICMPV4_TYPE
:
1135 case MFF_ICMPV6_TYPE
:
1136 match_set_icmp_type(match
, value
->u8
);
1139 case MFF_ICMPV4_CODE
:
1140 case MFF_ICMPV6_CODE
:
1141 match_set_icmp_code(match
, value
->u8
);
1145 match_set_nd_target(match
, &value
->ipv6
);
1154 /* Unwildcard the bits in 'mask' of the 'wc' member field described by 'mf'.
1155 * The caller is responsible for ensuring that 'wc' meets 'mf''s
1158 mf_mask_field_masked(const struct mf_field
*mf
, const union mf_value
*mask
,
1159 struct flow_wildcards
*wc
)
1161 union mf_value temp_mask
;
1162 /* For MFF_DL_VLAN, we cannot send a all 1's to flow_set_dl_vlan() as that
1163 * will be considered as OFP10_VLAN_NONE. So make sure the mask only has
1164 * valid bits in this case. */
1165 if (mf
->id
== MFF_DL_VLAN
) {
1166 temp_mask
.be16
= htons(VLAN_VID_MASK
) & mask
->be16
;
1170 union mf_value mask_value
;
1172 mf_get_value(mf
, &wc
->masks
, &mask_value
);
1173 for (size_t i
= 0; i
< mf
->n_bytes
; i
++) {
1174 mask_value
.b
[i
] |= mask
->b
[i
];
1176 mf_set_flow_value(mf
, &mask_value
, &wc
->masks
);
1179 /* Unwildcard 'wc' member field described by 'mf'. The caller is
1180 * responsible for ensuring that 'mask' meets 'mf''s prerequisites. */
1182 mf_mask_field(const struct mf_field
*mf
, struct flow_wildcards
*wc
)
1184 mf_mask_field_masked(mf
, &exact_match_mask
, wc
);
1188 field_len(const struct mf_field
*mf
, const union mf_value
*value_
)
1190 const uint8_t *value
= &value_
->u8
;
1193 if (!mf
->variable_len
) {
1201 for (i
= 0; i
< mf
->n_bytes
; i
++) {
1202 if (value
[i
] != 0) {
1207 return mf
->n_bytes
- i
;
1210 /* Returns the effective length of the field. For fixed length fields,
1211 * this is just the defined length. For variable length fields, it is
1212 * the minimum size encoding that retains the same meaning (i.e.
1213 * discarding leading zeros).
1215 * 'is_masked' returns (if non-NULL) whether the original contained
1216 * a mask. Otherwise, a mask that is the same length as the value
1217 * might be misinterpreted as an exact match. */
1219 mf_field_len(const struct mf_field
*mf
, const union mf_value
*value
,
1220 const union mf_value
*mask
, bool *is_masked_
)
1223 bool is_masked
= mask
&& !is_all_ones(mask
, mf
->n_bytes
);
1225 len
= field_len(mf
, value
);
1227 mask_len
= field_len(mf
, mask
);
1228 len
= MAX(len
, mask_len
);
1232 *is_masked_
= is_masked
;
1238 /* Sets 'flow' member field described by 'mf' to 'value'. The caller is
1239 * responsible for ensuring that 'flow' meets 'mf''s prerequisites.*/
1241 mf_set_flow_value(const struct mf_field
*mf
,
1242 const union mf_value
*value
, struct flow
*flow
)
1246 flow
->dp_hash
= ntohl(value
->be32
);
1249 flow
->recirc_id
= ntohl(value
->be32
);
1252 flow
->conj_id
= ntohl(value
->be32
);
1255 flow
->tunnel
.tun_id
= value
->be64
;
1258 flow
->tunnel
.ip_src
= value
->be32
;
1261 flow
->tunnel
.ip_dst
= value
->be32
;
1263 case MFF_TUN_IPV6_SRC
:
1264 flow
->tunnel
.ipv6_src
= value
->ipv6
;
1266 case MFF_TUN_IPV6_DST
:
1267 flow
->tunnel
.ipv6_dst
= value
->ipv6
;
1270 flow
->tunnel
.flags
= (flow
->tunnel
.flags
& ~FLOW_TNL_PUB_F_MASK
) |
1273 case MFF_TUN_GBP_ID
:
1274 flow
->tunnel
.gbp_id
= value
->be16
;
1276 case MFF_TUN_GBP_FLAGS
:
1277 flow
->tunnel
.gbp_flags
= value
->u8
;
1280 flow
->tunnel
.ip_tos
= value
->u8
;
1283 flow
->tunnel
.ip_ttl
= value
->u8
;
1285 CASE_MFF_TUN_METADATA
:
1286 tun_metadata_write(&flow
->tunnel
, mf
, value
);
1289 flow
->metadata
= value
->be64
;
1293 flow
->in_port
.ofp_port
= u16_to_ofp(ntohs(value
->be16
));
1296 case MFF_IN_PORT_OXM
:
1297 ofputil_port_from_ofp11(value
->be32
, &flow
->in_port
.ofp_port
);
1299 case MFF_ACTSET_OUTPUT
:
1300 ofputil_port_from_ofp11(value
->be32
, &flow
->actset_output
);
1303 case MFF_SKB_PRIORITY
:
1304 flow
->skb_priority
= ntohl(value
->be32
);
1308 flow
->pkt_mark
= ntohl(value
->be32
);
1312 flow
->ct_state
= ntohl(value
->be32
);
1316 flow
->ct_zone
= ntohs(value
->be16
);
1320 flow
->ct_mark
= ntohl(value
->be32
);
1324 flow
->ct_label
= ntoh128(value
->be128
);
1327 case MFF_CT_NW_PROTO
:
1328 flow
->ct_nw_proto
= value
->u8
;
1332 flow
->ct_nw_src
= value
->be32
;
1336 flow
->ct_nw_dst
= value
->be32
;
1339 case MFF_CT_IPV6_SRC
:
1340 flow
->ct_ipv6_src
= value
->ipv6
;
1343 case MFF_CT_IPV6_DST
:
1344 flow
->ct_ipv6_dst
= value
->ipv6
;
1348 flow
->ct_tp_src
= value
->be16
;
1352 flow
->ct_tp_dst
= value
->be16
;
1356 flow
->regs
[mf
->id
- MFF_REG0
] = ntohl(value
->be32
);
1360 flow_set_xreg(flow
, mf
->id
- MFF_XREG0
, ntohll(value
->be64
));
1364 flow_set_xxreg(flow
, mf
->id
- MFF_XXREG0
, ntoh128(value
->be128
));
1368 flow
->dl_src
= value
->mac
;
1372 flow
->dl_dst
= value
->mac
;
1376 flow
->dl_type
= value
->be16
;
1380 flow
->vlans
[0].tci
= value
->be16
;
1381 flow_fix_vlan_tpid(flow
);
1385 flow_set_dl_vlan(flow
, value
->be16
);
1386 flow_fix_vlan_tpid(flow
);
1390 flow_set_vlan_vid(flow
, value
->be16
);
1391 flow_fix_vlan_tpid(flow
);
1394 case MFF_DL_VLAN_PCP
:
1396 flow_set_vlan_pcp(flow
, value
->u8
);
1397 flow_fix_vlan_tpid(flow
);
1400 case MFF_MPLS_LABEL
:
1401 flow_set_mpls_label(flow
, 0, value
->be32
);
1405 flow_set_mpls_tc(flow
, 0, value
->u8
);
1409 flow_set_mpls_bos(flow
, 0, value
->u8
);
1413 flow_set_mpls_ttl(flow
, 0, value
->u8
);
1417 flow
->nw_src
= value
->be32
;
1421 flow
->nw_dst
= value
->be32
;
1425 flow
->ipv6_src
= value
->ipv6
;
1429 flow
->ipv6_dst
= value
->ipv6
;
1432 case MFF_IPV6_LABEL
:
1433 flow
->ipv6_label
= value
->be32
& htonl(IPV6_LABEL_MASK
);
1437 flow
->nw_proto
= value
->u8
;
1441 flow
->nw_tos
&= ~IP_DSCP_MASK
;
1442 flow
->nw_tos
|= value
->u8
& IP_DSCP_MASK
;
1445 case MFF_IP_DSCP_SHIFTED
:
1446 flow
->nw_tos
&= ~IP_DSCP_MASK
;
1447 flow
->nw_tos
|= value
->u8
<< 2;
1451 flow
->nw_tos
&= ~IP_ECN_MASK
;
1452 flow
->nw_tos
|= value
->u8
& IP_ECN_MASK
;
1456 flow
->nw_ttl
= value
->u8
;
1460 flow
->nw_frag
= value
->u8
& FLOW_NW_FRAG_MASK
;
1464 flow
->nw_proto
= ntohs(value
->be16
);
1468 flow
->nw_src
= value
->be32
;
1472 flow
->nw_dst
= value
->be32
;
1477 flow
->arp_sha
= value
->mac
;
1482 flow
->arp_tha
= value
->mac
;
1488 flow
->tp_src
= value
->be16
;
1494 flow
->tp_dst
= value
->be16
;
1498 flow
->tcp_flags
= value
->be16
;
1501 case MFF_ICMPV4_TYPE
:
1502 case MFF_ICMPV6_TYPE
:
1503 flow
->tp_src
= htons(value
->u8
);
1506 case MFF_ICMPV4_CODE
:
1507 case MFF_ICMPV6_CODE
:
1508 flow
->tp_dst
= htons(value
->u8
);
1512 flow
->nd_target
= value
->ipv6
;
1521 /* Consider each of 'src', 'mask', and 'dst' as if they were arrays of 8*n
1522 * bits. Then, for each 0 <= i < 8 * n such that mask[i] == 1, sets dst[i] =
1525 apply_mask(const uint8_t *src
, const uint8_t *mask
, uint8_t *dst
, size_t n
)
1529 for (i
= 0; i
< n
; i
++) {
1530 dst
[i
] = (src
[i
] & mask
[i
]) | (dst
[i
] & ~mask
[i
]);
1534 /* Sets 'flow' member field described by 'field' to 'value', except that bits
1535 * for which 'mask' has a 0-bit keep their existing values. The caller is
1536 * responsible for ensuring that 'flow' meets 'field''s prerequisites.*/
1538 mf_set_flow_value_masked(const struct mf_field
*field
,
1539 const union mf_value
*value
,
1540 const union mf_value
*mask
,
1545 mf_get_value(field
, flow
, &tmp
);
1546 apply_mask((const uint8_t *) value
, (const uint8_t *) mask
,
1547 (uint8_t *) &tmp
, field
->n_bytes
);
1548 mf_set_flow_value(field
, &tmp
, flow
);
1552 mf_is_tun_metadata(const struct mf_field
*mf
)
1554 return mf
->id
>= MFF_TUN_METADATA0
&&
1555 mf
->id
< MFF_TUN_METADATA0
+ TUN_METADATA_NUM_OPTS
;
1559 mf_is_pipeline_field(const struct mf_field
*mf
)
1565 case MFF_TUN_IPV6_SRC
:
1566 case MFF_TUN_IPV6_DST
:
1568 case MFF_TUN_GBP_ID
:
1569 case MFF_TUN_GBP_FLAGS
:
1570 CASE_MFF_TUN_METADATA
:
1573 case MFF_IN_PORT_OXM
:
1584 case MFF_ACTSET_OUTPUT
:
1585 case MFF_SKB_PRIORITY
:
1591 case MFF_CT_NW_PROTO
:
1594 case MFF_CT_IPV6_SRC
:
1595 case MFF_CT_IPV6_DST
:
1604 case MFF_DL_VLAN_PCP
:
1606 case MFF_MPLS_LABEL
:
1614 case MFF_IPV6_LABEL
:
1617 case MFF_IP_DSCP_SHIFTED
:
1633 case MFF_ICMPV4_TYPE
:
1634 case MFF_ICMPV4_CODE
:
1635 case MFF_ICMPV6_TYPE
:
1636 case MFF_ICMPV6_CODE
:
1648 /* Returns true if 'mf' has previously been set in 'flow', false if
1649 * it contains a non-default value.
1651 * The caller is responsible for ensuring that 'flow' meets 'mf''s
1654 mf_is_set(const struct mf_field
*mf
, const struct flow
*flow
)
1656 if (!mf_is_tun_metadata(mf
)) {
1657 union mf_value value
;
1659 mf_get_value(mf
, flow
, &value
);
1660 return !is_all_zeros(&value
, mf
->n_bytes
);
1662 return ULLONG_GET(flow
->tunnel
.metadata
.present
.map
,
1663 mf
->id
- MFF_TUN_METADATA0
);
1667 /* Makes 'match' wildcard field 'mf'.
1669 * The caller is responsible for ensuring that 'match' meets 'mf''s
1672 * If non-NULL, 'err_str' returns a malloc'ed string describing any errors
1673 * with the request or NULL if there is no error. The caller is reponsible
1674 * for freeing the string. */
1676 mf_set_wild(const struct mf_field
*mf
, struct match
*match
, char **err_str
)
1684 match
->flow
.dp_hash
= 0;
1685 match
->wc
.masks
.dp_hash
= 0;
1688 match
->flow
.recirc_id
= 0;
1689 match
->wc
.masks
.recirc_id
= 0;
1692 match
->flow
.conj_id
= 0;
1693 match
->wc
.masks
.conj_id
= 0;
1696 match_set_tun_id_masked(match
, htonll(0), htonll(0));
1699 match_set_tun_src_masked(match
, htonl(0), htonl(0));
1702 match_set_tun_dst_masked(match
, htonl(0), htonl(0));
1704 case MFF_TUN_IPV6_SRC
:
1705 memset(&match
->wc
.masks
.tunnel
.ipv6_src
, 0,
1706 sizeof match
->wc
.masks
.tunnel
.ipv6_src
);
1707 memset(&match
->flow
.tunnel
.ipv6_src
, 0,
1708 sizeof match
->flow
.tunnel
.ipv6_src
);
1710 case MFF_TUN_IPV6_DST
:
1711 memset(&match
->wc
.masks
.tunnel
.ipv6_dst
, 0,
1712 sizeof match
->wc
.masks
.tunnel
.ipv6_dst
);
1713 memset(&match
->flow
.tunnel
.ipv6_dst
, 0,
1714 sizeof match
->flow
.tunnel
.ipv6_dst
);
1717 match_set_tun_flags_masked(match
, 0, 0);
1719 case MFF_TUN_GBP_ID
:
1720 match_set_tun_gbp_id_masked(match
, 0, 0);
1722 case MFF_TUN_GBP_FLAGS
:
1723 match_set_tun_gbp_flags_masked(match
, 0, 0);
1726 match_set_tun_tos_masked(match
, 0, 0);
1729 match_set_tun_ttl_masked(match
, 0, 0);
1731 CASE_MFF_TUN_METADATA
:
1732 tun_metadata_set_match(mf
, NULL
, NULL
, match
, err_str
);
1736 match_set_metadata_masked(match
, htonll(0), htonll(0));
1740 case MFF_IN_PORT_OXM
:
1741 match
->flow
.in_port
.ofp_port
= 0;
1742 match
->wc
.masks
.in_port
.ofp_port
= 0;
1744 case MFF_ACTSET_OUTPUT
:
1745 match
->flow
.actset_output
= 0;
1746 match
->wc
.masks
.actset_output
= 0;
1749 case MFF_SKB_PRIORITY
:
1750 match
->flow
.skb_priority
= 0;
1751 match
->wc
.masks
.skb_priority
= 0;
1755 match
->flow
.pkt_mark
= 0;
1756 match
->wc
.masks
.pkt_mark
= 0;
1760 match
->flow
.ct_state
= 0;
1761 match
->wc
.masks
.ct_state
= 0;
1765 match
->flow
.ct_zone
= 0;
1766 match
->wc
.masks
.ct_zone
= 0;
1770 match
->flow
.ct_mark
= 0;
1771 match
->wc
.masks
.ct_mark
= 0;
1775 memset(&match
->flow
.ct_label
, 0, sizeof(match
->flow
.ct_label
));
1776 memset(&match
->wc
.masks
.ct_label
, 0, sizeof(match
->wc
.masks
.ct_label
));
1779 case MFF_CT_NW_PROTO
:
1780 match
->flow
.ct_nw_proto
= 0;
1781 match
->wc
.masks
.ct_nw_proto
= 0;
1785 match
->flow
.ct_nw_src
= 0;
1786 match
->wc
.masks
.ct_nw_src
= 0;
1790 match
->flow
.ct_nw_dst
= 0;
1791 match
->wc
.masks
.ct_nw_dst
= 0;
1794 case MFF_CT_IPV6_SRC
:
1795 memset(&match
->flow
.ct_ipv6_src
, 0, sizeof(match
->flow
.ct_ipv6_src
));
1796 WC_UNMASK_FIELD(&match
->wc
, ct_ipv6_src
);
1799 case MFF_CT_IPV6_DST
:
1800 memset(&match
->flow
.ct_ipv6_dst
, 0, sizeof(match
->flow
.ct_ipv6_dst
));
1801 WC_UNMASK_FIELD(&match
->wc
, ct_ipv6_dst
);
1805 match
->flow
.ct_tp_src
= 0;
1806 match
->wc
.masks
.ct_tp_src
= 0;
1810 match
->flow
.ct_tp_dst
= 0;
1811 match
->wc
.masks
.ct_tp_dst
= 0;
1815 match_set_reg_masked(match
, mf
->id
- MFF_REG0
, 0, 0);
1819 match_set_xreg_masked(match
, mf
->id
- MFF_XREG0
, 0, 0);
1823 match_set_xxreg_masked(match
, mf
->id
- MFF_XXREG0
, OVS_U128_ZERO
,
1829 match
->flow
.dl_src
= eth_addr_zero
;
1830 match
->wc
.masks
.dl_src
= eth_addr_zero
;
1834 match
->flow
.dl_dst
= eth_addr_zero
;
1835 match
->wc
.masks
.dl_dst
= eth_addr_zero
;
1839 match
->flow
.dl_type
= htons(0);
1840 match
->wc
.masks
.dl_type
= htons(0);
1844 match_set_dl_tci_masked(match
, htons(0), htons(0));
1849 match_set_any_vid(match
);
1852 case MFF_DL_VLAN_PCP
:
1854 match_set_any_pcp(match
);
1857 case MFF_MPLS_LABEL
:
1858 match_set_any_mpls_label(match
, 0);
1862 match_set_any_mpls_tc(match
, 0);
1866 match_set_any_mpls_bos(match
, 0);
1870 match_set_any_mpls_ttl(match
, 0);
1875 match_set_nw_src_masked(match
, htonl(0), htonl(0));
1880 match_set_nw_dst_masked(match
, htonl(0), htonl(0));
1884 memset(&match
->wc
.masks
.ipv6_src
, 0, sizeof match
->wc
.masks
.ipv6_src
);
1885 memset(&match
->flow
.ipv6_src
, 0, sizeof match
->flow
.ipv6_src
);
1889 memset(&match
->wc
.masks
.ipv6_dst
, 0, sizeof match
->wc
.masks
.ipv6_dst
);
1890 memset(&match
->flow
.ipv6_dst
, 0, sizeof match
->flow
.ipv6_dst
);
1893 case MFF_IPV6_LABEL
:
1894 match
->wc
.masks
.ipv6_label
= htonl(0);
1895 match
->flow
.ipv6_label
= htonl(0);
1899 match
->wc
.masks
.nw_proto
= 0;
1900 match
->flow
.nw_proto
= 0;
1904 case MFF_IP_DSCP_SHIFTED
:
1905 match
->wc
.masks
.nw_tos
&= ~IP_DSCP_MASK
;
1906 match
->flow
.nw_tos
&= ~IP_DSCP_MASK
;
1910 match
->wc
.masks
.nw_tos
&= ~IP_ECN_MASK
;
1911 match
->flow
.nw_tos
&= ~IP_ECN_MASK
;
1915 match
->wc
.masks
.nw_ttl
= 0;
1916 match
->flow
.nw_ttl
= 0;
1920 match
->wc
.masks
.nw_frag
&= ~FLOW_NW_FRAG_MASK
;
1921 match
->flow
.nw_frag
&= ~FLOW_NW_FRAG_MASK
;
1925 match
->wc
.masks
.nw_proto
= 0;
1926 match
->flow
.nw_proto
= 0;
1931 match
->flow
.arp_sha
= eth_addr_zero
;
1932 match
->wc
.masks
.arp_sha
= eth_addr_zero
;
1937 match
->flow
.arp_tha
= eth_addr_zero
;
1938 match
->wc
.masks
.arp_tha
= eth_addr_zero
;
1944 case MFF_ICMPV4_TYPE
:
1945 case MFF_ICMPV6_TYPE
:
1946 match
->wc
.masks
.tp_src
= htons(0);
1947 match
->flow
.tp_src
= htons(0);
1953 case MFF_ICMPV4_CODE
:
1954 case MFF_ICMPV6_CODE
:
1955 match
->wc
.masks
.tp_dst
= htons(0);
1956 match
->flow
.tp_dst
= htons(0);
1960 match
->wc
.masks
.tcp_flags
= htons(0);
1961 match
->flow
.tcp_flags
= htons(0);
1965 memset(&match
->wc
.masks
.nd_target
, 0,
1966 sizeof match
->wc
.masks
.nd_target
);
1967 memset(&match
->flow
.nd_target
, 0, sizeof match
->flow
.nd_target
);
1976 /* Makes 'match' match field 'mf' with the specified 'value' and 'mask'.
1977 * 'value' specifies a value to match and 'mask' specifies a wildcard pattern,
1978 * with a 1-bit indicating that the corresponding value bit must match and a
1979 * 0-bit indicating a don't-care.
1981 * If 'mask' is NULL or points to all-1-bits, then this call is equivalent to
1982 * mf_set_value(mf, value, match). If 'mask' points to all-0-bits, then this
1983 * call is equivalent to mf_set_wild(mf, match).
1985 * 'mask' must be a valid mask for 'mf' (see mf_is_mask_valid()). The caller
1986 * is responsible for ensuring that 'match' meets 'mf''s prerequisites.
1988 * If non-NULL, 'err_str' returns a malloc'ed string describing any errors
1989 * with the request or NULL if there is no error. The caller is reponsible
1990 * for freeing the string.
1992 * Return a set of enum ofputil_protocol bits (as an uint32_t to avoid circular
1993 * dependency on enum ofputil_protocol definition) indicating which OpenFlow
1994 * protocol versions can support this functionality. */
1996 mf_set(const struct mf_field
*mf
,
1997 const union mf_value
*value
, const union mf_value
*mask
,
1998 struct match
*match
, char **err_str
)
2000 if (!mask
|| is_all_ones(mask
, mf
->n_bytes
)) {
2001 mf_set_value(mf
, value
, match
, err_str
);
2002 return mf
->usable_protocols_exact
;
2003 } else if (is_all_zeros(mask
, mf
->n_bytes
) && !mf_is_tun_metadata(mf
)) {
2004 /* Tunnel metadata matches on the existence of the field itself, so
2005 * it still needs to be encoded even if the value is wildcarded. */
2006 mf_set_wild(mf
, match
, err_str
);
2007 return OFPUTIL_P_ANY
;
2016 case MFF_CT_NW_PROTO
:
2019 case MFF_CT_IPV6_SRC
:
2020 case MFF_CT_IPV6_DST
:
2026 case MFF_IN_PORT_OXM
:
2027 case MFF_ACTSET_OUTPUT
:
2028 case MFF_SKB_PRIORITY
:
2031 case MFF_DL_VLAN_PCP
:
2033 case MFF_MPLS_LABEL
:
2040 case MFF_IP_DSCP_SHIFTED
:
2043 case MFF_ICMPV4_TYPE
:
2044 case MFF_ICMPV4_CODE
:
2045 case MFF_ICMPV6_TYPE
:
2046 case MFF_ICMPV6_CODE
:
2047 return OFPUTIL_P_NONE
;
2050 match_set_dp_hash_masked(match
, ntohl(value
->be32
), ntohl(mask
->be32
));
2053 match_set_tun_id_masked(match
, value
->be64
, mask
->be64
);
2056 match_set_tun_src_masked(match
, value
->be32
, mask
->be32
);
2059 match_set_tun_dst_masked(match
, value
->be32
, mask
->be32
);
2061 case MFF_TUN_IPV6_SRC
:
2062 match_set_tun_ipv6_src_masked(match
, &value
->ipv6
, &mask
->ipv6
);
2064 case MFF_TUN_IPV6_DST
:
2065 match_set_tun_ipv6_dst_masked(match
, &value
->ipv6
, &mask
->ipv6
);
2068 match_set_tun_flags_masked(match
, ntohs(value
->be16
), ntohs(mask
->be16
));
2070 case MFF_TUN_GBP_ID
:
2071 match_set_tun_gbp_id_masked(match
, value
->be16
, mask
->be16
);
2073 case MFF_TUN_GBP_FLAGS
:
2074 match_set_tun_gbp_flags_masked(match
, value
->u8
, mask
->u8
);
2077 match_set_tun_ttl_masked(match
, value
->u8
, mask
->u8
);
2080 match_set_tun_tos_masked(match
, value
->u8
, mask
->u8
);
2082 CASE_MFF_TUN_METADATA
:
2083 tun_metadata_set_match(mf
, value
, mask
, match
, err_str
);
2087 match_set_metadata_masked(match
, value
->be64
, mask
->be64
);
2091 match_set_reg_masked(match
, mf
->id
- MFF_REG0
,
2092 ntohl(value
->be32
), ntohl(mask
->be32
));
2096 match_set_xreg_masked(match
, mf
->id
- MFF_XREG0
,
2097 ntohll(value
->be64
), ntohll(mask
->be64
));
2101 match_set_xxreg_masked(match
, mf
->id
- MFF_XXREG0
,
2102 ntoh128(value
->be128
), ntoh128(mask
->be128
));
2107 match_set_pkt_mark_masked(match
, ntohl(value
->be32
),
2112 match_set_ct_state_masked(match
, ntohl(value
->be32
), ntohl(mask
->be32
));
2116 match_set_ct_mark_masked(match
, ntohl(value
->be32
), ntohl(mask
->be32
));
2120 match_set_ct_label_masked(match
, ntoh128(value
->be128
),
2121 ntoh128(mask
->be128
));
2125 match_set_dl_dst_masked(match
, value
->mac
, mask
->mac
);
2129 match_set_dl_src_masked(match
, value
->mac
, mask
->mac
);
2134 match_set_arp_sha_masked(match
, value
->mac
, mask
->mac
);
2139 match_set_arp_tha_masked(match
, value
->mac
, mask
->mac
);
2143 match_set_dl_tci_masked(match
, value
->be16
, mask
->be16
);
2147 match_set_vlan_vid_masked(match
, value
->be16
, mask
->be16
);
2151 match_set_nw_src_masked(match
, value
->be32
, mask
->be32
);
2155 match_set_nw_dst_masked(match
, value
->be32
, mask
->be32
);
2159 match_set_ipv6_src_masked(match
, &value
->ipv6
, &mask
->ipv6
);
2163 match_set_ipv6_dst_masked(match
, &value
->ipv6
, &mask
->ipv6
);
2166 case MFF_IPV6_LABEL
:
2167 if ((mask
->be32
& htonl(IPV6_LABEL_MASK
)) == htonl(IPV6_LABEL_MASK
)) {
2168 mf_set_value(mf
, value
, match
, err_str
);
2170 match_set_ipv6_label_masked(match
, value
->be32
, mask
->be32
);
2175 match_set_nd_target_masked(match
, &value
->ipv6
, &mask
->ipv6
);
2179 match_set_nw_frag_masked(match
, value
->u8
, mask
->u8
);
2183 match_set_nw_src_masked(match
, value
->be32
, mask
->be32
);
2187 match_set_nw_dst_masked(match
, value
->be32
, mask
->be32
);
2193 match_set_tp_src_masked(match
, value
->be16
, mask
->be16
);
2199 match_set_tp_dst_masked(match
, value
->be16
, mask
->be16
);
2203 match_set_tcp_flags_masked(match
, value
->be16
, mask
->be16
);
2211 return ((mf
->usable_protocols_bitwise
== mf
->usable_protocols_cidr
2212 || ip_is_cidr(mask
->be32
))
2213 ? mf
->usable_protocols_cidr
2214 : mf
->usable_protocols_bitwise
);
2218 mf_check__(const struct mf_subfield
*sf
, const struct match
*match
,
2222 VLOG_WARN_RL(&rl
, "unknown %s field", type
);
2223 return OFPERR_OFPBAC_BAD_SET_TYPE
;
2224 } else if (!sf
->n_bits
) {
2225 VLOG_WARN_RL(&rl
, "zero bit %s field %s", type
, sf
->field
->name
);
2226 return OFPERR_OFPBAC_BAD_SET_LEN
;
2227 } else if (sf
->ofs
>= sf
->field
->n_bits
) {
2228 VLOG_WARN_RL(&rl
, "bit offset %d exceeds %d-bit width of %s field %s",
2229 sf
->ofs
, sf
->field
->n_bits
, type
, sf
->field
->name
);
2230 return OFPERR_OFPBAC_BAD_SET_LEN
;
2231 } else if (sf
->ofs
+ sf
->n_bits
> sf
->field
->n_bits
) {
2232 VLOG_WARN_RL(&rl
, "bit offset %d and width %d exceeds %d-bit width "
2233 "of %s field %s", sf
->ofs
, sf
->n_bits
,
2234 sf
->field
->n_bits
, type
, sf
->field
->name
);
2235 return OFPERR_OFPBAC_BAD_SET_LEN
;
2236 } else if (match
&& !mf_are_match_prereqs_ok(sf
->field
, match
)) {
2237 VLOG_WARN_RL(&rl
, "%s field %s lacks correct prerequisites",
2238 type
, sf
->field
->name
);
2239 return OFPERR_OFPBAC_MATCH_INCONSISTENT
;
2245 /* Sets all the bits in 'sf' to 1 within 'wc', if 'wc' is nonnull. */
2247 unwildcard_subfield(const struct mf_subfield
*sf
, struct flow_wildcards
*wc
)
2250 union mf_value mask
;
2252 memset(&mask
, 0, sizeof mask
);
2253 bitwise_one(&mask
, sf
->field
->n_bytes
, sf
->ofs
, sf
->n_bits
);
2254 mf_mask_field_masked(sf
->field
, &mask
, wc
);
2258 /* Copies 'src' into 'dst' within 'flow', and sets all the bits in 'src' and
2259 * 'dst' to 1s in 'wc', if 'wc' is nonnull.
2261 * 'src' and 'dst' may overlap. */
2263 mf_subfield_copy(const struct mf_subfield
*src
,
2264 const struct mf_subfield
*dst
,
2265 struct flow
*flow
, struct flow_wildcards
*wc
)
2267 ovs_assert(src
->n_bits
== dst
->n_bits
);
2268 if (mf_are_prereqs_ok(dst
->field
, flow
, wc
)
2269 && mf_are_prereqs_ok(src
->field
, flow
, wc
)) {
2270 unwildcard_subfield(src
, wc
);
2271 unwildcard_subfield(dst
, wc
);
2273 union mf_value src_value
;
2274 union mf_value dst_value
;
2275 mf_get_value(dst
->field
, flow
, &dst_value
);
2276 mf_get_value(src
->field
, flow
, &src_value
);
2277 bitwise_copy(&src_value
, src
->field
->n_bytes
, src
->ofs
,
2278 &dst_value
, dst
->field
->n_bytes
, dst
->ofs
,
2280 mf_set_flow_value(dst
->field
, &dst_value
, flow
);
2284 /* Swaps the bits in 'src' and 'dst' within 'flow', and sets all the bits in
2285 * 'src' and 'dst' to 1s in 'wc', if 'wc' is nonnull.
2287 * 'src' and 'dst' may overlap. */
2289 mf_subfield_swap(const struct mf_subfield
*a
,
2290 const struct mf_subfield
*b
,
2291 struct flow
*flow
, struct flow_wildcards
*wc
)
2293 ovs_assert(a
->n_bits
== b
->n_bits
);
2294 if (mf_are_prereqs_ok(a
->field
, flow
, wc
)
2295 && mf_are_prereqs_ok(b
->field
, flow
, wc
)) {
2296 unwildcard_subfield(a
, wc
);
2297 unwildcard_subfield(b
, wc
);
2299 union mf_value a_value
;
2300 union mf_value b_value
;
2301 mf_get_value(a
->field
, flow
, &a_value
);
2302 mf_get_value(b
->field
, flow
, &b_value
);
2303 union mf_value b2_value
= b_value
;
2305 /* Copy 'a' into 'b'. */
2306 bitwise_copy(&a_value
, a
->field
->n_bytes
, a
->ofs
,
2307 &b_value
, b
->field
->n_bytes
, b
->ofs
,
2309 mf_set_flow_value(b
->field
, &b_value
, flow
);
2311 /* Copy original 'b' into 'a'. */
2312 bitwise_copy(&b2_value
, b
->field
->n_bytes
, b
->ofs
,
2313 &a_value
, a
->field
->n_bytes
, a
->ofs
,
2315 mf_set_flow_value(a
->field
, &a_value
, flow
);
2319 /* Checks whether 'sf' is valid for reading a subfield out of 'flow'. Returns
2320 * 0 if so, otherwise an OpenFlow error code (e.g. as returned by
2323 mf_check_src(const struct mf_subfield
*sf
, const struct match
*match
)
2325 return mf_check__(sf
, match
, "source");
2328 /* Checks whether 'sf' is valid for writing a subfield into 'flow'. Returns 0
2329 * if so, otherwise an OpenFlow error code (e.g. as returned by
2332 mf_check_dst(const struct mf_subfield
*sf
, const struct match
*match
)
2334 int error
= mf_check__(sf
, match
, "destination");
2335 if (!error
&& !sf
->field
->writable
) {
2336 VLOG_WARN_RL(&rl
, "destination field %s is not writable",
2338 return OFPERR_OFPBAC_BAD_SET_ARGUMENT
;
2343 /* Copies the value and wildcard bit pattern for 'mf' from 'match' into the
2344 * 'value' and 'mask', respectively. */
2346 mf_get(const struct mf_field
*mf
, const struct match
*match
,
2347 union mf_value
*value
, union mf_value
*mask
)
2349 mf_get_value(mf
, &match
->flow
, value
);
2350 mf_get_mask(mf
, &match
->wc
, mask
);
2354 mf_from_integer_string(const struct mf_field
*mf
, const char *s
,
2355 uint8_t *valuep
, uint8_t *maskp
)
2358 const char *err_str
= "";
2361 err
= parse_int_string(s
, valuep
, mf
->n_bytes
, &tail
);
2362 if (err
|| (*tail
!= '\0' && *tail
!= '/')) {
2368 err
= parse_int_string(tail
+ 1, maskp
, mf
->n_bytes
, &tail
);
2369 if (err
|| *tail
!= '\0') {
2374 memset(maskp
, 0xff, mf
->n_bytes
);
2380 if (err
== ERANGE
) {
2381 return xasprintf("%s: %s too large for %u-byte field %s",
2382 s
, err_str
, mf
->n_bytes
, mf
->name
);
2384 return xasprintf("%s: bad syntax for %s %s", s
, mf
->name
, err_str
);
2389 mf_from_ethernet_string(const struct mf_field
*mf
, const char *s
,
2390 struct eth_addr
*mac
, struct eth_addr
*mask
)
2394 ovs_assert(mf
->n_bytes
== ETH_ADDR_LEN
);
2397 if (ovs_scan(s
, ETH_ADDR_SCAN_FMT
"%n", ETH_ADDR_SCAN_ARGS(*mac
), &n
)
2398 && n
== strlen(s
)) {
2399 *mask
= eth_addr_exact
;
2404 if (ovs_scan(s
, ETH_ADDR_SCAN_FMT
"/"ETH_ADDR_SCAN_FMT
"%n",
2405 ETH_ADDR_SCAN_ARGS(*mac
), ETH_ADDR_SCAN_ARGS(*mask
), &n
)
2406 && n
== strlen(s
)) {
2410 return xasprintf("%s: invalid Ethernet address", s
);
2414 mf_from_ipv4_string(const struct mf_field
*mf
, const char *s
,
2415 ovs_be32
*ip
, ovs_be32
*mask
)
2417 ovs_assert(mf
->n_bytes
== sizeof *ip
);
2418 return ip_parse_masked(s
, ip
, mask
);
2422 mf_from_ipv6_string(const struct mf_field
*mf
, const char *s
,
2423 struct in6_addr
*ipv6
, struct in6_addr
*mask
)
2425 ovs_assert(mf
->n_bytes
== sizeof *ipv6
);
2426 return ipv6_parse_masked(s
, ipv6
, mask
);
2430 mf_from_ofp_port_string(const struct mf_field
*mf
, const char *s
,
2431 const struct ofputil_port_map
*port_map
,
2432 ovs_be16
*valuep
, ovs_be16
*maskp
)
2436 ovs_assert(mf
->n_bytes
== sizeof(ovs_be16
));
2438 if (ofputil_port_from_string(s
, port_map
, &port
)) {
2439 *valuep
= htons(ofp_to_u16(port
));
2440 *maskp
= OVS_BE16_MAX
;
2443 return xasprintf("%s: invalid or unknown port for %s", s
, mf
->name
);
2447 mf_from_ofp_port_string32(const struct mf_field
*mf
, const char *s
,
2448 const struct ofputil_port_map
*port_map
,
2449 ovs_be32
*valuep
, ovs_be32
*maskp
)
2453 ovs_assert(mf
->n_bytes
== sizeof(ovs_be32
));
2454 if (ofputil_port_from_string(s
, port_map
, &port
)) {
2455 *valuep
= ofputil_port_to_ofp11(port
);
2456 *maskp
= OVS_BE32_MAX
;
2459 return xasprintf("%s: port value out of range for %s", s
, mf
->name
);
2462 struct frag_handling
{
2468 static const struct frag_handling all_frags
[] = {
2469 #define A FLOW_NW_FRAG_ANY
2470 #define L FLOW_NW_FRAG_LATER
2471 /* name mask value */
2474 { "first", A
|L
, A
},
2475 { "later", A
|L
, A
|L
},
2480 { "not_later", L
, 0 },
2487 mf_from_frag_string(const char *s
, uint8_t *valuep
, uint8_t *maskp
)
2489 const struct frag_handling
*h
;
2491 for (h
= all_frags
; h
< &all_frags
[ARRAY_SIZE(all_frags
)]; h
++) {
2492 if (!strcasecmp(s
, h
->name
)) {
2493 /* We force the upper bits of the mask on to make mf_parse_value()
2494 * happy (otherwise it will never think it's an exact match.) */
2495 *maskp
= h
->mask
| ~FLOW_NW_FRAG_MASK
;
2501 return xasprintf("%s: unknown fragment type (valid types are \"no\", "
2502 "\"yes\", \"first\", \"later\", \"not_first\"", s
);
2506 parse_mf_flags(const char *s
, const char *(*bit_to_string
)(uint32_t),
2507 const char *field_name
, ovs_be16
*flagsp
, ovs_be16 allowed
,
2512 uint32_t flags
, mask
;
2514 err
= parse_flags(s
, bit_to_string
, '\0', field_name
, &err_str
,
2515 &flags
, ntohs(allowed
), maskp
? &mask
: NULL
);
2520 *flagsp
= htons(flags
);
2522 *maskp
= htons(mask
);
2529 mf_from_tcp_flags_string(const char *s
, ovs_be16
*flagsp
, ovs_be16
*maskp
)
2531 return parse_mf_flags(s
, packet_tcp_flag_to_string
, "TCP", flagsp
,
2532 TCP_FLAGS_BE16(OVS_BE16_MAX
), maskp
);
2536 mf_from_tun_flags_string(const char *s
, ovs_be16
*flagsp
, ovs_be16
*maskp
)
2538 return parse_mf_flags(s
, flow_tun_flag_to_string
, "tunnel", flagsp
,
2539 htons(FLOW_TNL_PUB_F_MASK
), maskp
);
2543 mf_from_ct_state_string(const char *s
, ovs_be32
*flagsp
, ovs_be32
*maskp
)
2547 uint32_t flags
, mask
;
2549 err
= parse_flags(s
, ct_state_to_string
, '\0', "ct_state", &err_str
,
2550 &flags
, CS_SUPPORTED_MASK
, maskp
? &mask
: NULL
);
2555 *flagsp
= htonl(flags
);
2557 *maskp
= htonl(mask
);
2563 /* Parses 's', a string value for field 'mf', into 'value' and 'mask'. Returns
2564 * NULL if successful, otherwise a malloc()'d string describing the error. */
2566 mf_parse(const struct mf_field
*mf
, const char *s
,
2567 const struct ofputil_port_map
*port_map
,
2568 union mf_value
*value
, union mf_value
*mask
)
2572 if (!strcmp(s
, "*")) {
2573 memset(value
, 0, mf
->n_bytes
);
2574 memset(mask
, 0, mf
->n_bytes
);
2578 switch (mf
->string
) {
2580 case MFS_HEXADECIMAL
:
2581 error
= mf_from_integer_string(mf
, s
,
2582 (uint8_t *) value
, (uint8_t *) mask
);
2586 ovs_assert(mf
->n_bytes
== sizeof(ovs_be32
));
2587 error
= mf_from_ct_state_string(s
, &value
->be32
, &mask
->be32
);
2591 error
= mf_from_ethernet_string(mf
, s
, &value
->mac
, &mask
->mac
);
2595 error
= mf_from_ipv4_string(mf
, s
, &value
->be32
, &mask
->be32
);
2599 error
= mf_from_ipv6_string(mf
, s
, &value
->ipv6
, &mask
->ipv6
);
2603 error
= mf_from_ofp_port_string(mf
, s
, port_map
,
2604 &value
->be16
, &mask
->be16
);
2607 case MFS_OFP_PORT_OXM
:
2608 error
= mf_from_ofp_port_string32(mf
, s
, port_map
,
2609 &value
->be32
, &mask
->be32
);
2613 error
= mf_from_frag_string(s
, &value
->u8
, &mask
->u8
);
2617 ovs_assert(mf
->n_bytes
== sizeof(ovs_be16
));
2618 error
= mf_from_tun_flags_string(s
, &value
->be16
, &mask
->be16
);
2622 ovs_assert(mf
->n_bytes
== sizeof(ovs_be16
));
2623 error
= mf_from_tcp_flags_string(s
, &value
->be16
, &mask
->be16
);
2630 if (!error
&& !mf_is_mask_valid(mf
, mask
)) {
2631 error
= xasprintf("%s: invalid mask for field %s", s
, mf
->name
);
2636 /* Parses 's', a string value for field 'mf', into 'value'. Returns NULL if
2637 * successful, otherwise a malloc()'d string describing the error. */
2639 mf_parse_value(const struct mf_field
*mf
, const char *s
,
2640 const struct ofputil_port_map
*port_map
, union mf_value
*value
)
2642 union mf_value mask
;
2645 error
= mf_parse(mf
, s
, port_map
, value
, &mask
);
2650 if (!is_all_ones((const uint8_t *) &mask
, mf
->n_bytes
)) {
2651 return xasprintf("%s: wildcards not allowed here", s
);
2657 mf_format_integer_string(const struct mf_field
*mf
, const uint8_t *valuep
,
2658 const uint8_t *maskp
, struct ds
*s
)
2660 if (mf
->string
== MFS_HEXADECIMAL
) {
2661 ds_put_hex(s
, valuep
, mf
->n_bytes
);
2663 unsigned long long int integer
= 0;
2666 ovs_assert(mf
->n_bytes
<= 8);
2667 for (i
= 0; i
< mf
->n_bytes
; i
++) {
2668 integer
= (integer
<< 8) | valuep
[i
];
2670 ds_put_format(s
, "%lld", integer
);
2674 /* I guess we could write the mask in decimal for MFS_DECIMAL but I'm
2675 * not sure that that a bit-mask written in decimal is ever easier to
2676 * understand than the same bit-mask written in hexadecimal. */
2677 ds_put_char(s
, '/');
2678 ds_put_hex(s
, maskp
, mf
->n_bytes
);
2683 mf_format_frag_string(uint8_t value
, uint8_t mask
, struct ds
*s
)
2685 const struct frag_handling
*h
;
2687 mask
&= FLOW_NW_FRAG_MASK
;
2690 for (h
= all_frags
; h
< &all_frags
[ARRAY_SIZE(all_frags
)]; h
++) {
2691 if (value
== h
->value
&& mask
== h
->mask
) {
2692 ds_put_cstr(s
, h
->name
);
2696 ds_put_cstr(s
, "<error>");
2700 mf_format_tnl_flags_string(ovs_be16 value
, ovs_be16 mask
, struct ds
*s
)
2702 format_flags_masked(s
, NULL
, flow_tun_flag_to_string
, ntohs(value
),
2703 ntohs(mask
) & FLOW_TNL_PUB_F_MASK
, FLOW_TNL_PUB_F_MASK
);
2707 mf_format_tcp_flags_string(ovs_be16 value
, ovs_be16 mask
, struct ds
*s
)
2709 format_flags_masked(s
, NULL
, packet_tcp_flag_to_string
, ntohs(value
),
2710 TCP_FLAGS(mask
), TCP_FLAGS(OVS_BE16_MAX
));
2714 mf_format_ct_state_string(ovs_be32 value
, ovs_be32 mask
, struct ds
*s
)
2716 format_flags_masked(s
, NULL
, ct_state_to_string
, ntohl(value
),
2717 ntohl(mask
), UINT16_MAX
);
2720 /* Appends to 's' a string representation of field 'mf' whose value is in
2721 * 'value' and 'mask'. 'mask' may be NULL to indicate an exact match. */
2723 mf_format(const struct mf_field
*mf
,
2724 const union mf_value
*value
, const union mf_value
*mask
,
2725 const struct ofputil_port_map
*port_map
,
2729 if (is_all_zeros(mask
, mf
->n_bytes
)) {
2730 ds_put_cstr(s
, "ANY");
2732 } else if (is_all_ones(mask
, mf
->n_bytes
)) {
2737 switch (mf
->string
) {
2738 case MFS_OFP_PORT_OXM
:
2741 ofputil_port_from_ofp11(value
->be32
, &port
);
2742 ofputil_format_port(port
, port_map
, s
);
2748 ofputil_format_port(u16_to_ofp(ntohs(value
->be16
)), port_map
, s
);
2753 case MFS_HEXADECIMAL
:
2754 mf_format_integer_string(mf
, (uint8_t *) value
, (uint8_t *) mask
, s
);
2758 mf_format_ct_state_string(value
->be32
,
2759 mask
? mask
->be32
: OVS_BE32_MAX
, s
);
2763 eth_format_masked(value
->mac
, mask
? &mask
->mac
: NULL
, s
);
2767 ip_format_masked(value
->be32
, mask
? mask
->be32
: OVS_BE32_MAX
, s
);
2771 ipv6_format_masked(&value
->ipv6
, mask
? &mask
->ipv6
: NULL
, s
);
2775 mf_format_frag_string(value
->u8
, mask
? mask
->u8
: UINT8_MAX
, s
);
2779 mf_format_tnl_flags_string(value
->be16
,
2780 mask
? mask
->be16
: OVS_BE16_MAX
, s
);
2784 mf_format_tcp_flags_string(value
->be16
,
2785 mask
? mask
->be16
: OVS_BE16_MAX
, s
);
2793 /* Makes subfield 'sf' within 'flow' exactly match the 'sf->n_bits'
2794 * least-significant bits in 'x'.
2797 mf_write_subfield_flow(const struct mf_subfield
*sf
,
2798 const union mf_subvalue
*x
, struct flow
*flow
)
2800 const struct mf_field
*field
= sf
->field
;
2801 union mf_value value
;
2803 mf_get_value(field
, flow
, &value
);
2804 bitwise_copy(x
, sizeof *x
, 0, &value
, field
->n_bytes
,
2805 sf
->ofs
, sf
->n_bits
);
2806 mf_set_flow_value(field
, &value
, flow
);
2809 /* Makes subfield 'sf' within 'match' exactly match the 'sf->n_bits'
2810 * least-significant bits in 'x'.
2813 mf_write_subfield(const struct mf_subfield
*sf
, const union mf_subvalue
*x
,
2814 struct match
*match
)
2816 const struct mf_field
*field
= sf
->field
;
2817 union mf_value value
, mask
;
2819 mf_get(field
, match
, &value
, &mask
);
2820 bitwise_copy(x
, sizeof *x
, 0, &value
, field
->n_bytes
, sf
->ofs
, sf
->n_bits
);
2821 bitwise_one ( &mask
, field
->n_bytes
, sf
->ofs
, sf
->n_bits
);
2822 mf_set(field
, &value
, &mask
, match
, NULL
);
2826 mf_write_subfield_value(const struct mf_subfield
*sf
, const void *src
,
2827 struct match
*match
)
2829 const struct mf_field
*field
= sf
->field
;
2830 union mf_value value
, mask
;
2831 unsigned int size
= DIV_ROUND_UP(sf
->n_bits
, 8);
2833 mf_get(field
, match
, &value
, &mask
);
2834 bitwise_copy(src
, size
, 0, &value
, field
->n_bytes
, sf
->ofs
, sf
->n_bits
);
2835 bitwise_one ( &mask
, field
->n_bytes
, sf
->ofs
, sf
->n_bits
);
2836 mf_set(field
, &value
, &mask
, match
, NULL
);
2839 /* 'v' and 'm' correspond to values of 'field'. This function copies them into
2840 * 'match' in the correspond positions. */
2842 mf_mask_subfield(const struct mf_field
*field
,
2843 const union mf_subvalue
*v
,
2844 const union mf_subvalue
*m
,
2845 struct match
*match
)
2847 union mf_value value
, mask
;
2849 mf_get(field
, match
, &value
, &mask
);
2850 bitwise_copy(v
, sizeof *v
, 0, &value
, field
->n_bytes
, 0, field
->n_bits
);
2851 bitwise_copy(m
, sizeof *m
, 0, &mask
, field
->n_bytes
, 0, field
->n_bits
);
2852 mf_set(field
, &value
, &mask
, match
, NULL
);
2855 /* Initializes 'x' to the value of 'sf' within 'flow'. 'sf' must be valid for
2856 * reading 'flow', e.g. as checked by mf_check_src(). */
2858 mf_read_subfield(const struct mf_subfield
*sf
, const struct flow
*flow
,
2859 union mf_subvalue
*x
)
2861 union mf_value value
;
2863 mf_get_value(sf
->field
, flow
, &value
);
2865 memset(x
, 0, sizeof *x
);
2866 bitwise_copy(&value
, sf
->field
->n_bytes
, sf
->ofs
,
2871 /* Returns the value of 'sf' within 'flow'. 'sf' must be valid for reading
2872 * 'flow', e.g. as checked by mf_check_src() and sf->n_bits must be 64 or
2875 mf_get_subfield(const struct mf_subfield
*sf
, const struct flow
*flow
)
2877 union mf_value value
;
2879 mf_get_value(sf
->field
, flow
, &value
);
2880 return bitwise_get(&value
, sf
->field
->n_bytes
, sf
->ofs
, sf
->n_bits
);
2884 mf_format_subvalue(const union mf_subvalue
*subvalue
, struct ds
*s
)
2886 ds_put_hex(s
, subvalue
->u8
, sizeof subvalue
->u8
);
2890 field_array_set(enum mf_field_id id
, const union mf_value
*value
,
2891 struct field_array
*fa
)
2893 size_t i
, offset
= 0;
2895 ovs_assert(id
< MFF_N_IDS
);
2897 /* Find the spot for 'id'. */
2898 BITMAP_FOR_EACH_1 (i
, id
, fa
->used
.bm
) {
2899 offset
+= mf_from_id(i
)->n_bytes
;
2902 size_t value_size
= mf_from_id(id
)->n_bytes
;
2904 /* make room if necessary. */
2905 if (!bitmap_is_set(fa
->used
.bm
, id
)) {
2906 fa
->values
= xrealloc(fa
->values
, fa
->values_size
+ value_size
);
2907 /* Move remainder forward, if any. */
2908 if (offset
< fa
->values_size
) {
2909 memmove(fa
->values
+ offset
+ value_size
, fa
->values
+ offset
,
2910 fa
->values_size
- offset
);
2912 fa
->values_size
+= value_size
;
2914 bitmap_set1(fa
->used
.bm
, id
);
2916 memcpy(fa
->values
+ offset
, value
, value_size
);
2919 /* A wrapper for variable length mf_fields that is maintained by
2920 * struct vl_mff_map.*/
2921 struct vl_mf_field
{
2923 struct ovs_refcount ref_cnt
;
2924 struct cmap_node cmap_node
; /* In ofproto->vl_mff_map->cmap. */
2927 static inline uint32_t
2928 mf_field_hash(uint32_t key
)
2930 return hash_int(key
, 0);
2934 vmf_delete(struct vl_mf_field
*vmf
)
2936 if (ovs_refcount_unref(&vmf
->ref_cnt
) == 1) {
2937 /* Postpone as this function is typically called immediately
2938 * after removing from cmap. */
2939 ovsrcu_postpone(free
, vmf
);
2942 "Attempted to delete VMF %s but refcount is nonzero!",
2948 mf_vl_mff_map_clear(struct vl_mff_map
*vl_mff_map
, bool force
)
2949 OVS_REQUIRES(vl_mff_map
->mutex
)
2951 struct vl_mf_field
*vmf
;
2954 CMAP_FOR_EACH (vmf
, cmap_node
, &vl_mff_map
->cmap
) {
2955 if (ovs_refcount_read(&vmf
->ref_cnt
) != 1) {
2956 return OFPERR_NXTTMFC_INVALID_TLV_DEL
;
2961 CMAP_FOR_EACH (vmf
, cmap_node
, &vl_mff_map
->cmap
) {
2962 cmap_remove(&vl_mff_map
->cmap
, &vmf
->cmap_node
,
2963 mf_field_hash(vmf
->mf
.id
));
2970 static struct vl_mf_field
*
2971 mf_get_vl_mff__(uint32_t id
, const struct vl_mff_map
*vl_mff_map
)
2973 struct vl_mf_field
*vmf
;
2975 CMAP_FOR_EACH_WITH_HASH (vmf
, cmap_node
, mf_field_hash(id
),
2976 &vl_mff_map
->cmap
) {
2977 if (vmf
->mf
.id
== id
) {
2985 /* If 'mff' is a variable length field, looks up 'vl_mff_map', returns a
2986 * pointer to the variable length meta-flow field corresponding to 'mff'.
2987 * Returns NULL if no mapping is existed for 'mff'. */
2988 const struct mf_field
*
2989 mf_get_vl_mff(const struct mf_field
*mff
,
2990 const struct vl_mff_map
*vl_mff_map
)
2992 if (mff
&& mff
->variable_len
&& vl_mff_map
) {
2993 return &mf_get_vl_mff__(mff
->id
, vl_mff_map
)->mf
;
3000 mf_vl_mff_map_del(struct vl_mff_map
*vl_mff_map
,
3001 const struct ofputil_tlv_table_mod
*ttm
, bool force
)
3002 OVS_REQUIRES(vl_mff_map
->mutex
)
3004 struct ofputil_tlv_map
*tlv_map
;
3005 struct vl_mf_field
*vmf
;
3009 LIST_FOR_EACH (tlv_map
, list_node
, &ttm
->mappings
) {
3010 idx
= MFF_TUN_METADATA0
+ tlv_map
->index
;
3011 if (idx
>= MFF_TUN_METADATA0
+ TUN_METADATA_NUM_OPTS
) {
3012 return OFPERR_NXTTMFC_BAD_FIELD_IDX
;
3015 vmf
= mf_get_vl_mff__(idx
, vl_mff_map
);
3016 if (vmf
&& ovs_refcount_read(&vmf
->ref_cnt
) != 1) {
3017 return OFPERR_NXTTMFC_INVALID_TLV_DEL
;
3022 LIST_FOR_EACH (tlv_map
, list_node
, &ttm
->mappings
) {
3023 idx
= MFF_TUN_METADATA0
+ tlv_map
->index
;
3024 if (idx
>= MFF_TUN_METADATA0
+ TUN_METADATA_NUM_OPTS
) {
3025 return OFPERR_NXTTMFC_BAD_FIELD_IDX
;
3028 vmf
= mf_get_vl_mff__(idx
, vl_mff_map
);
3030 cmap_remove(&vl_mff_map
->cmap
, &vmf
->cmap_node
,
3031 mf_field_hash(idx
));
3040 mf_vl_mff_map_add(struct vl_mff_map
*vl_mff_map
,
3041 const struct ofputil_tlv_table_mod
*ttm
)
3042 OVS_REQUIRES(vl_mff_map
->mutex
)
3044 struct ofputil_tlv_map
*tlv_map
;
3045 struct vl_mf_field
*vmf
;
3048 LIST_FOR_EACH (tlv_map
, list_node
, &ttm
->mappings
) {
3049 idx
= MFF_TUN_METADATA0
+ tlv_map
->index
;
3050 if (idx
>= MFF_TUN_METADATA0
+ TUN_METADATA_NUM_OPTS
) {
3051 return OFPERR_NXTTMFC_BAD_FIELD_IDX
;
3054 vmf
= xmalloc(sizeof *vmf
);
3055 vmf
->mf
= mf_fields
[idx
];
3056 vmf
->mf
.n_bytes
= tlv_map
->option_len
;
3057 vmf
->mf
.n_bits
= tlv_map
->option_len
* 8;
3058 vmf
->mf
.mapped
= true;
3059 ovs_refcount_init(&vmf
->ref_cnt
);
3061 cmap_insert(&vl_mff_map
->cmap
, &vmf
->cmap_node
,
3062 mf_field_hash(idx
));
3068 /* Updates the tun_metadata mf_field in 'vl_mff_map' according to 'ttm'.
3069 * This function must be invoked after tun_metadata_table_mod().
3070 * Returns OFPERR_NXTTMFC_BAD_FIELD_IDX, if the index for the vl_mf_field is
3072 * Returns OFPERR_NXTTMFC_INVALID_TLV_DEL, if 'ttm' tries to delete an
3073 * vl_mf_field that is still used by any active flow.*/
3075 mf_vl_mff_map_mod_from_tun_metadata(struct vl_mff_map
*vl_mff_map
,
3076 const struct ofputil_tlv_table_mod
*ttm
)
3077 OVS_REQUIRES(vl_mff_map
->mutex
)
3079 switch (ttm
->command
) {
3081 return mf_vl_mff_map_add(vl_mff_map
, ttm
);
3084 return mf_vl_mff_map_del(vl_mff_map
, ttm
, false);
3087 return mf_vl_mff_map_clear(vl_mff_map
, false);
3096 /* Returns true if a variable length meta-flow field 'mff' is not mapped in
3097 * the 'vl_mff_map'. */
3099 mf_vl_mff_invalid(const struct mf_field
*mff
, const struct vl_mff_map
*map
)
3101 return map
&& mff
&& mff
->variable_len
&& !mff
->mapped
;
3105 mf_vl_mff_set_tlv_bitmap(const struct mf_field
*mff
, uint64_t *tlv_bitmap
)
3107 if (mff
&& mff
->mapped
) {
3108 ovs_assert(mf_is_tun_metadata(mff
));
3109 ULLONG_SET1(*tlv_bitmap
, mff
->id
- MFF_TUN_METADATA0
);
3114 mf_vl_mff_ref_cnt_mod(const struct vl_mff_map
*map
, uint64_t tlv_bitmap
,
3117 struct vl_mf_field
*vmf
;
3121 ULLONG_FOR_EACH_1 (i
, tlv_bitmap
) {
3122 vmf
= mf_get_vl_mff__(i
+ MFF_TUN_METADATA0
, map
);
3125 ovs_refcount_ref(&vmf
->ref_cnt
);
3127 ovs_refcount_unref(&vmf
->ref_cnt
);
3130 VLOG_WARN("Invalid TLV index %d.", i
);
3137 mf_vl_mff_ref(const struct vl_mff_map
*map
, uint64_t tlv_bitmap
)
3139 mf_vl_mff_ref_cnt_mod(map
, tlv_bitmap
, true);
3143 mf_vl_mff_unref(const struct vl_mff_map
*map
, uint64_t tlv_bitmap
)
3145 mf_vl_mff_ref_cnt_mod(map
, tlv_bitmap
, false);
3149 mf_vl_mff_nx_pull_header(struct ofpbuf
*b
, const struct vl_mff_map
*vl_mff_map
,
3150 const struct mf_field
**field
, bool *masked
,
3151 uint64_t *tlv_bitmap
)
3153 enum ofperr error
= nx_pull_header(b
, vl_mff_map
, field
, masked
);
3158 mf_vl_mff_set_tlv_bitmap(*field
, tlv_bitmap
);
3163 mf_vl_mff_nx_pull_entry(struct ofpbuf
*b
, const struct vl_mff_map
*vl_mff_map
,
3164 const struct mf_field
**field
, union mf_value
*value
,
3165 union mf_value
*mask
, uint64_t *tlv_bitmap
)
3167 enum ofperr error
= nx_pull_entry(b
, vl_mff_map
, field
, value
, mask
);
3172 mf_vl_mff_set_tlv_bitmap(*field
, tlv_bitmap
);
3177 mf_vl_mff_mf_from_nxm_header(uint32_t header
,
3178 const struct vl_mff_map
*vl_mff_map
,
3179 const struct mf_field
**field
,
3180 uint64_t *tlv_bitmap
)
3182 *field
= mf_from_nxm_header(header
, vl_mff_map
);
3183 if (mf_vl_mff_invalid(*field
, vl_mff_map
)) {
3184 return OFPERR_NXFMFC_INVALID_TLV_FIELD
;
3187 mf_vl_mff_set_tlv_bitmap(*field
, tlv_bitmap
);