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"
43 #include "openvswitch/nsh.h"
45 VLOG_DEFINE_THIS_MODULE(meta_flow
);
47 #define FLOW_U32OFS(FIELD) \
48 offsetof(struct flow, FIELD) % 4 ? -1 : offsetof(struct flow, FIELD) / 4
50 #define MF_FIELD_SIZES(MEMBER) \
51 sizeof ((union mf_value *)0)->MEMBER, \
52 8 * sizeof ((union mf_value *)0)->MEMBER
54 extern const struct mf_field mf_fields
[MFF_N_IDS
]; /* Silence a warning. */
56 const struct mf_field mf_fields
[MFF_N_IDS
] = {
57 #include "meta-flow.inc"
60 /* Maps from an mf_field's 'name' or 'extra_name' to the mf_field. */
61 static struct shash mf_by_name
;
63 /* Rate limit for parse errors. These always indicate a bug in an OpenFlow
64 * controller and so there's not much point in showing a lot of them. */
65 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
67 #define MF_VALUE_EXACT_8 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
68 #define MF_VALUE_EXACT_16 MF_VALUE_EXACT_8, MF_VALUE_EXACT_8
69 #define MF_VALUE_EXACT_32 MF_VALUE_EXACT_16, MF_VALUE_EXACT_16
70 #define MF_VALUE_EXACT_64 MF_VALUE_EXACT_32, MF_VALUE_EXACT_32
71 #define MF_VALUE_EXACT_128 MF_VALUE_EXACT_64, MF_VALUE_EXACT_64
72 #define MF_VALUE_EXACT_INITIALIZER { .tun_metadata = { MF_VALUE_EXACT_128 } }
74 const union mf_value exact_match_mask
= MF_VALUE_EXACT_INITIALIZER
;
76 static void nxm_init(void);
78 /* Returns the field with the given 'name', or a null pointer if no field has
80 const struct mf_field
*
81 mf_from_name(const char *name
)
84 return shash_find_data(&mf_by_name
, name
);
87 /* Returns the field with the given 'name' (which is 'len' bytes long), or a
88 * null pointer if no field has that name. */
89 const struct mf_field
*
90 mf_from_name_len(const char *name
, size_t len
)
94 struct shash_node
*node
= shash_find_len(&mf_by_name
, name
, len
);
95 return node
? node
->data
: NULL
;
103 shash_init(&mf_by_name
);
104 for (i
= 0; i
< MFF_N_IDS
; i
++) {
105 const struct mf_field
*mf
= &mf_fields
[i
];
107 ovs_assert(mf
->id
== i
); /* Fields must be in the enum order. */
109 shash_add_once(&mf_by_name
, mf
->name
, mf
);
110 if (mf
->extra_name
) {
111 shash_add_once(&mf_by_name
, mf
->extra_name
, mf
);
119 static pthread_once_t once
= PTHREAD_ONCE_INIT
;
120 pthread_once(&once
, nxm_do_init
);
123 /* Consider the two value/mask pairs 'a_value/a_mask' and 'b_value/b_mask' as
124 * restrictions on a field's value. Then, this function initializes
125 * 'dst_value/dst_mask' such that it combines the restrictions of both pairs.
126 * This is not always possible, i.e. if one pair insists on a value of 0 in
127 * some bit and the other pair insists on a value of 1 in that bit. This
128 * function returns false in a case where the combined restriction is
129 * impossible (in which case 'dst_value/dst_mask' is not fully initialized),
132 * (As usually true for value/mask pairs in OVS, any 1-bit in a value must have
133 * a corresponding 1-bit in its mask.) */
135 mf_subvalue_intersect(const union mf_subvalue
*a_value
,
136 const union mf_subvalue
*a_mask
,
137 const union mf_subvalue
*b_value
,
138 const union mf_subvalue
*b_mask
,
139 union mf_subvalue
*dst_value
,
140 union mf_subvalue
*dst_mask
)
142 for (int i
= 0; i
< ARRAY_SIZE(a_value
->be64
); i
++) {
143 ovs_be64 av
= a_value
->be64
[i
];
144 ovs_be64 am
= a_mask
->be64
[i
];
145 ovs_be64 bv
= b_value
->be64
[i
];
146 ovs_be64 bm
= b_mask
->be64
[i
];
147 ovs_be64
*dv
= &dst_value
->be64
[i
];
148 ovs_be64
*dm
= &dst_mask
->be64
[i
];
150 if ((av
^ bv
) & (am
& bm
)) {
159 /* Returns the "number of bits" in 'v', e.g. 1 if only the lowest-order bit is
160 * set, 2 if the second-lowest-order bit is set, and so on. */
162 mf_subvalue_width(const union mf_subvalue
*v
)
164 return 1 + bitwise_rscan(v
, sizeof *v
, true, sizeof *v
* 8 - 1, -1);
167 /* For positive 'n', shifts the bits in 'value' 'n' bits to the left, and for
168 * negative 'n', shifts the bits '-n' bits to the right. */
170 mf_subvalue_shift(union mf_subvalue
*value
, int n
)
173 union mf_subvalue tmp
;
174 memset(&tmp
, 0, sizeof tmp
);
176 if (n
> 0 && n
< 8 * sizeof tmp
) {
177 bitwise_copy(value
, sizeof *value
, 0,
180 } else if (n
< 0 && n
> -8 * sizeof tmp
) {
181 bitwise_copy(value
, sizeof *value
, -n
,
189 /* Appends a formatted representation of 'sv' to 's'. */
191 mf_subvalue_format(const union mf_subvalue
*sv
, struct ds
*s
)
193 ds_put_hex(s
, sv
, sizeof *sv
);
196 /* Returns true if 'wc' wildcards all the bits in field 'mf', false if 'wc'
197 * specifies at least one bit in the field.
199 * The caller is responsible for ensuring that 'wc' corresponds to a flow that
200 * meets 'mf''s prerequisites. */
202 mf_is_all_wild(const struct mf_field
*mf
, const struct flow_wildcards
*wc
)
206 return !wc
->masks
.dp_hash
;
208 return !wc
->masks
.recirc_id
;
209 case MFF_PACKET_TYPE
:
210 return !wc
->masks
.packet_type
;
212 return !wc
->masks
.conj_id
;
214 return !wc
->masks
.tunnel
.ip_src
;
216 return !wc
->masks
.tunnel
.ip_dst
;
217 case MFF_TUN_IPV6_SRC
:
218 return ipv6_mask_is_any(&wc
->masks
.tunnel
.ipv6_src
);
219 case MFF_TUN_IPV6_DST
:
220 return ipv6_mask_is_any(&wc
->masks
.tunnel
.ipv6_dst
);
222 return !wc
->masks
.tunnel
.tun_id
;
224 return !wc
->masks
.tunnel
.ip_tos
;
226 return !wc
->masks
.tunnel
.ip_ttl
;
228 return !(wc
->masks
.tunnel
.flags
& FLOW_TNL_PUB_F_MASK
);
230 return !wc
->masks
.tunnel
.gbp_id
;
231 case MFF_TUN_GBP_FLAGS
:
232 return !wc
->masks
.tunnel
.gbp_flags
;
233 CASE_MFF_TUN_METADATA
:
234 return !ULLONG_GET(wc
->masks
.tunnel
.metadata
.present
.map
,
235 mf
->id
- MFF_TUN_METADATA0
);
237 return !wc
->masks
.metadata
;
239 case MFF_IN_PORT_OXM
:
240 return !wc
->masks
.in_port
.ofp_port
;
241 case MFF_SKB_PRIORITY
:
242 return !wc
->masks
.skb_priority
;
244 return !wc
->masks
.pkt_mark
;
246 return !wc
->masks
.ct_state
;
248 return !wc
->masks
.ct_zone
;
250 return !wc
->masks
.ct_mark
;
252 return ovs_u128_is_zero(wc
->masks
.ct_label
);
253 case MFF_CT_NW_PROTO
:
254 return !wc
->masks
.ct_nw_proto
;
256 return !wc
->masks
.ct_nw_src
;
258 return !wc
->masks
.ct_nw_dst
;
260 return !wc
->masks
.ct_tp_src
;
262 return !wc
->masks
.ct_tp_dst
;
263 case MFF_CT_IPV6_SRC
:
264 return ipv6_mask_is_any(&wc
->masks
.ct_ipv6_src
);
265 case MFF_CT_IPV6_DST
:
266 return ipv6_mask_is_any(&wc
->masks
.ct_ipv6_dst
);
268 return !wc
->masks
.regs
[mf
->id
- MFF_REG0
];
270 return !flow_get_xreg(&wc
->masks
, mf
->id
- MFF_XREG0
);
272 ovs_u128 value
= flow_get_xxreg(&wc
->masks
, mf
->id
- MFF_XXREG0
);
273 return ovs_u128_is_zero(value
);
275 case MFF_ACTSET_OUTPUT
:
276 return !wc
->masks
.actset_output
;
279 return eth_addr_is_zero(wc
->masks
.dl_src
);
281 return eth_addr_is_zero(wc
->masks
.dl_dst
);
283 return !wc
->masks
.dl_type
;
287 return eth_addr_is_zero(wc
->masks
.arp_sha
);
291 return eth_addr_is_zero(wc
->masks
.arp_tha
);
294 return !wc
->masks
.vlans
[0].tci
;
296 return !(wc
->masks
.vlans
[0].tci
& htons(VLAN_VID_MASK
));
298 return !(wc
->masks
.vlans
[0].tci
& htons(VLAN_VID_MASK
| VLAN_CFI
));
299 case MFF_DL_VLAN_PCP
:
301 return !(wc
->masks
.vlans
[0].tci
& htons(VLAN_PCP_MASK
));
304 return !(wc
->masks
.mpls_lse
[0] & htonl(MPLS_LABEL_MASK
));
306 return !(wc
->masks
.mpls_lse
[0] & htonl(MPLS_TC_MASK
));
308 return !(wc
->masks
.mpls_lse
[0] & htonl(MPLS_BOS_MASK
));
310 return !(wc
->masks
.mpls_lse
[0] & htonl(MPLS_TTL_MASK
));
313 return !wc
->masks
.nw_src
;
315 return !wc
->masks
.nw_dst
;
318 return ipv6_mask_is_any(&wc
->masks
.ipv6_src
);
320 return ipv6_mask_is_any(&wc
->masks
.ipv6_dst
);
323 return !wc
->masks
.ipv6_label
;
326 return !wc
->masks
.nw_proto
;
328 case MFF_IP_DSCP_SHIFTED
:
329 return !(wc
->masks
.nw_tos
& IP_DSCP_MASK
);
331 return !(wc
->masks
.nw_tos
& IP_ECN_MASK
);
333 return !wc
->masks
.nw_ttl
;
336 return ipv6_mask_is_any(&wc
->masks
.nd_target
);
339 return !(wc
->masks
.nw_frag
& FLOW_NW_FRAG_MASK
);
342 return !wc
->masks
.nw_proto
;
344 return !wc
->masks
.nw_src
;
346 return !wc
->masks
.nw_dst
;
351 case MFF_ICMPV4_TYPE
:
352 case MFF_ICMPV6_TYPE
:
353 return !wc
->masks
.tp_src
;
357 case MFF_ICMPV4_CODE
:
358 case MFF_ICMPV6_CODE
:
359 return !wc
->masks
.tp_dst
;
361 return !wc
->masks
.tcp_flags
;
364 return !wc
->masks
.nsh
.flags
;
366 return !wc
->masks
.nsh
.ttl
;
368 return !wc
->masks
.nsh
.mdtype
;
370 return !wc
->masks
.nsh
.np
;
372 return !(wc
->masks
.nsh
.path_hdr
& htonl(NSH_SPI_MASK
));
374 return !(wc
->masks
.nsh
.path_hdr
& htonl(NSH_SI_MASK
));
379 return !wc
->masks
.nsh
.context
[mf
->id
- MFF_NSH_C1
];
387 /* Initializes 'mask' with the wildcard bit pattern for field 'mf' within 'wc'.
388 * Each bit in 'mask' will be set to 1 if the bit is significant for matching
389 * purposes, or to 0 if it is wildcarded.
391 * The caller is responsible for ensuring that 'wc' corresponds to a flow that
392 * meets 'mf''s prerequisites. */
394 mf_get_mask(const struct mf_field
*mf
, const struct flow_wildcards
*wc
,
395 union mf_value
*mask
)
397 mf_get_value(mf
, &wc
->masks
, mask
);
400 /* Tests whether 'mask' is a valid wildcard bit pattern for 'mf'. Returns true
401 * if the mask is valid, false otherwise. */
403 mf_is_mask_valid(const struct mf_field
*mf
, const union mf_value
*mask
)
405 switch (mf
->maskable
) {
407 return (is_all_zeros(mask
, mf
->n_bytes
) ||
408 is_all_ones(mask
, mf
->n_bytes
));
417 /* Returns true if 'flow' meets the prerequisites for 'mf', false otherwise.
418 * If a non-NULL 'mask' is passed, zero-valued matches can also be verified.
419 * Sets inspected bits in 'wc', if non-NULL. */
421 mf_are_prereqs_ok__(const struct mf_field
*mf
, const struct flow
*flow
,
422 const struct flow_wildcards
*mask
,
423 struct flow_wildcards
*wc
)
425 ovs_be16 dl_type
= get_dl_type(flow
);
427 switch (mf
->prereqs
) {
431 return is_ethernet(flow
, wc
);
433 return (dl_type
== htons(ETH_TYPE_ARP
) ||
434 dl_type
== htons(ETH_TYPE_RARP
));
436 return dl_type
== htons(ETH_TYPE_IP
);
438 return dl_type
== htons(ETH_TYPE_IPV6
);
440 return is_vlan(flow
, wc
);
442 return eth_type_mpls(dl_type
);
444 return is_ip_any(flow
);
446 return dl_type
== htons(ETH_TYPE_NSH
);
448 return is_ct_valid(flow
, mask
, wc
);
450 /* Matching !FRAG_LATER is not enforced (mask is not checked). */
451 return is_tcp(flow
, wc
) && !(flow
->nw_frag
& FLOW_NW_FRAG_LATER
);
453 return is_udp(flow
, wc
) && !(flow
->nw_frag
& FLOW_NW_FRAG_LATER
);
455 return is_sctp(flow
, wc
) && !(flow
->nw_frag
& FLOW_NW_FRAG_LATER
);
457 return is_icmpv4(flow
, wc
);
459 return is_icmpv6(flow
, wc
);
461 return is_nd(flow
, wc
);
463 return is_nd(flow
, wc
) && flow
->tp_src
== htons(ND_NEIGHBOR_SOLICIT
);
465 return is_nd(flow
, wc
) && flow
->tp_src
== htons(ND_NEIGHBOR_ADVERT
);
471 /* Returns true if 'flow' meets the prerequisites for 'mf', false otherwise.
472 * Sets inspected bits in 'wc', if non-NULL. */
474 mf_are_prereqs_ok(const struct mf_field
*mf
, const struct flow
*flow
,
475 struct flow_wildcards
*wc
)
477 return mf_are_prereqs_ok__(mf
, flow
, NULL
, wc
);
480 /* Returns true if 'match' meets the prerequisites for 'mf', false otherwise.
483 mf_are_match_prereqs_ok(const struct mf_field
*mf
, const struct match
*match
)
485 return mf_are_prereqs_ok__(mf
, &match
->flow
, &match
->wc
, NULL
);
488 /* Returns true if 'value' may be a valid value *as part of a masked match*,
491 * A value is not rejected just because it is not valid for the field in
492 * question, but only if it doesn't make sense to test the bits in question at
493 * all. For example, the MFF_VLAN_TCI field will never have a nonzero value
494 * without the VLAN_CFI bit being set, but we can't reject those values because
495 * it is still legitimate to test just for those bits (see the documentation
496 * for NXM_OF_VLAN_TCI in meta-flow.h). On the other hand, there is never a
497 * reason to set the low bit of MFF_IP_DSCP to 1, so we reject that. */
499 mf_is_value_valid(const struct mf_field
*mf
, const union mf_value
*value
)
504 case MFF_PACKET_TYPE
:
509 case MFF_TUN_IPV6_SRC
:
510 case MFF_TUN_IPV6_DST
:
514 case MFF_TUN_GBP_FLAGS
:
515 CASE_MFF_TUN_METADATA
:
518 case MFF_SKB_PRIORITY
:
523 case MFF_CT_NW_PROTO
:
526 case MFF_CT_IPV6_SRC
:
527 case MFF_CT_IPV6_DST
:
554 case MFF_ICMPV4_TYPE
:
555 case MFF_ICMPV4_CODE
:
556 case MFF_ICMPV6_TYPE
:
557 case MFF_ICMPV6_CODE
:
563 case MFF_IN_PORT_OXM
:
564 case MFF_ACTSET_OUTPUT
: {
566 return !ofputil_port_from_ofp11(value
->be32
, &port
);
570 return !(value
->u8
& ~IP_DSCP_MASK
);
571 case MFF_IP_DSCP_SHIFTED
:
572 return !(value
->u8
& (~IP_DSCP_MASK
>> 2));
574 return !(value
->u8
& ~IP_ECN_MASK
);
576 return !(value
->u8
& ~FLOW_NW_FRAG_MASK
);
578 return !(value
->be16
& ~htons(0x0fff));
581 return !(value
->be16
& htons(0xff00));
584 return !(value
->be16
& htons(VLAN_CFI
| VLAN_PCP_MASK
));
586 return !(value
->be16
& htons(VLAN_PCP_MASK
));
588 case MFF_DL_VLAN_PCP
:
590 return !(value
->u8
& ~(VLAN_PCP_MASK
>> VLAN_PCP_SHIFT
));
593 return !(value
->be32
& ~htonl(IPV6_LABEL_MASK
));
596 return !(value
->be32
& ~htonl(MPLS_LABEL_MASK
>> MPLS_LABEL_SHIFT
));
599 return !(value
->u8
& ~(MPLS_TC_MASK
>> MPLS_TC_SHIFT
));
602 return !(value
->u8
& ~(MPLS_BOS_MASK
>> MPLS_BOS_SHIFT
));
605 return !(value
->be16
& ~htons(FLOW_TNL_PUB_F_MASK
));
608 return !(value
->be32
& ~htonl(CS_SUPPORTED_MASK
));
613 return (value
->u8
<= 63);
615 return (value
->u8
== 1 || value
->u8
== 2);
619 return !(value
->be32
& htonl(0xFF000000));
633 /* Copies the value of field 'mf' from 'flow' into 'value'. The caller is
634 * responsible for ensuring that 'flow' meets 'mf''s prerequisites. */
636 mf_get_value(const struct mf_field
*mf
, const struct flow
*flow
,
637 union mf_value
*value
)
641 value
->be32
= htonl(flow
->dp_hash
);
644 value
->be32
= htonl(flow
->recirc_id
);
646 case MFF_PACKET_TYPE
:
647 value
->be32
= flow
->packet_type
;
650 value
->be32
= htonl(flow
->conj_id
);
653 value
->be64
= flow
->tunnel
.tun_id
;
656 value
->be32
= flow
->tunnel
.ip_src
;
659 value
->be32
= flow
->tunnel
.ip_dst
;
661 case MFF_TUN_IPV6_SRC
:
662 value
->ipv6
= flow
->tunnel
.ipv6_src
;
664 case MFF_TUN_IPV6_DST
:
665 value
->ipv6
= flow
->tunnel
.ipv6_dst
;
668 value
->be16
= htons(flow
->tunnel
.flags
& FLOW_TNL_PUB_F_MASK
);
671 value
->be16
= flow
->tunnel
.gbp_id
;
673 case MFF_TUN_GBP_FLAGS
:
674 value
->u8
= flow
->tunnel
.gbp_flags
;
677 value
->u8
= flow
->tunnel
.ip_ttl
;
680 value
->u8
= flow
->tunnel
.ip_tos
;
682 CASE_MFF_TUN_METADATA
:
683 tun_metadata_read(&flow
->tunnel
, mf
, value
);
687 value
->be64
= flow
->metadata
;
691 value
->be16
= htons(ofp_to_u16(flow
->in_port
.ofp_port
));
693 case MFF_IN_PORT_OXM
:
694 value
->be32
= ofputil_port_to_ofp11(flow
->in_port
.ofp_port
);
696 case MFF_ACTSET_OUTPUT
:
697 value
->be32
= ofputil_port_to_ofp11(flow
->actset_output
);
700 case MFF_SKB_PRIORITY
:
701 value
->be32
= htonl(flow
->skb_priority
);
705 value
->be32
= htonl(flow
->pkt_mark
);
709 value
->be32
= htonl(flow
->ct_state
);
713 value
->be16
= htons(flow
->ct_zone
);
717 value
->be32
= htonl(flow
->ct_mark
);
721 value
->be128
= hton128(flow
->ct_label
);
724 case MFF_CT_NW_PROTO
:
725 value
->u8
= flow
->ct_nw_proto
;
729 value
->be32
= flow
->ct_nw_src
;
733 value
->be32
= flow
->ct_nw_dst
;
736 case MFF_CT_IPV6_SRC
:
737 value
->ipv6
= flow
->ct_ipv6_src
;
740 case MFF_CT_IPV6_DST
:
741 value
->ipv6
= flow
->ct_ipv6_dst
;
745 value
->be16
= flow
->ct_tp_src
;
749 value
->be16
= flow
->ct_tp_dst
;
753 value
->be32
= htonl(flow
->regs
[mf
->id
- MFF_REG0
]);
757 value
->be64
= htonll(flow_get_xreg(flow
, mf
->id
- MFF_XREG0
));
761 value
->be128
= hton128(flow_get_xxreg(flow
, mf
->id
- MFF_XXREG0
));
765 value
->mac
= flow
->dl_src
;
769 value
->mac
= flow
->dl_dst
;
773 value
->be16
= flow
->dl_type
;
777 value
->be16
= flow
->vlans
[0].tci
;
781 value
->be16
= flow
->vlans
[0].tci
& htons(VLAN_VID_MASK
);
784 value
->be16
= flow
->vlans
[0].tci
& htons(VLAN_VID_MASK
| VLAN_CFI
);
787 case MFF_DL_VLAN_PCP
:
789 value
->u8
= vlan_tci_to_pcp(flow
->vlans
[0].tci
);
793 value
->be32
= htonl(mpls_lse_to_label(flow
->mpls_lse
[0]));
797 value
->u8
= mpls_lse_to_tc(flow
->mpls_lse
[0]);
801 value
->u8
= mpls_lse_to_bos(flow
->mpls_lse
[0]);
805 value
->u8
= mpls_lse_to_ttl(flow
->mpls_lse
[0]);
809 value
->be32
= flow
->nw_src
;
813 value
->be32
= flow
->nw_dst
;
817 value
->ipv6
= flow
->ipv6_src
;
821 value
->ipv6
= flow
->ipv6_dst
;
825 value
->be32
= flow
->ipv6_label
;
829 value
->u8
= flow
->nw_proto
;
833 value
->u8
= flow
->nw_tos
& IP_DSCP_MASK
;
836 case MFF_IP_DSCP_SHIFTED
:
837 value
->u8
= flow
->nw_tos
>> 2;
841 value
->u8
= flow
->nw_tos
& IP_ECN_MASK
;
845 value
->u8
= flow
->nw_ttl
;
849 value
->u8
= flow
->nw_frag
;
853 value
->be16
= htons(flow
->nw_proto
);
857 value
->be32
= flow
->nw_src
;
861 value
->be32
= flow
->nw_dst
;
866 value
->mac
= flow
->arp_sha
;
871 value
->mac
= flow
->arp_tha
;
877 value
->be16
= flow
->tp_src
;
883 value
->be16
= flow
->tp_dst
;
887 value
->be16
= flow
->tcp_flags
;
890 case MFF_ICMPV4_TYPE
:
891 case MFF_ICMPV6_TYPE
:
892 value
->u8
= ntohs(flow
->tp_src
);
895 case MFF_ICMPV4_CODE
:
896 case MFF_ICMPV6_CODE
:
897 value
->u8
= ntohs(flow
->tp_dst
);
901 value
->ipv6
= flow
->nd_target
;
905 value
->u8
= flow
->nsh
.flags
;
908 value
->u8
= flow
->nsh
.ttl
;
911 value
->u8
= flow
->nsh
.mdtype
;
914 value
->u8
= flow
->nsh
.np
;
917 value
->be32
= nsh_path_hdr_to_spi(flow
->nsh
.path_hdr
);
918 if (value
->be32
== htonl(NSH_SPI_MASK
>> NSH_SPI_SHIFT
)) {
919 value
->be32
= OVS_BE32_MAX
;
923 value
->u8
= nsh_path_hdr_to_si(flow
->nsh
.path_hdr
);
929 value
->be32
= flow
->nsh
.context
[mf
->id
- MFF_NSH_C1
];
938 /* Makes 'match' match field 'mf' exactly, with the value matched taken from
939 * 'value'. The caller is responsible for ensuring that 'match' meets 'mf''s
942 * If non-NULL, 'err_str' returns a malloc'ed string describing any errors
943 * with the request or NULL if there is no error. The caller is reponsible
944 * for freeing the string. */
946 mf_set_value(const struct mf_field
*mf
,
947 const union mf_value
*value
, struct match
*match
, char **err_str
)
955 match_set_dp_hash(match
, ntohl(value
->be32
));
958 match_set_recirc_id(match
, ntohl(value
->be32
));
960 case MFF_PACKET_TYPE
:
961 match_set_packet_type(match
, value
->be32
);
964 match_set_conj_id(match
, ntohl(value
->be32
));
967 match_set_tun_id(match
, value
->be64
);
970 match_set_tun_src(match
, value
->be32
);
973 match_set_tun_dst(match
, value
->be32
);
975 case MFF_TUN_IPV6_SRC
:
976 match_set_tun_ipv6_src(match
, &value
->ipv6
);
978 case MFF_TUN_IPV6_DST
:
979 match_set_tun_ipv6_dst(match
, &value
->ipv6
);
982 match_set_tun_flags(match
, ntohs(value
->be16
));
985 match_set_tun_gbp_id(match
, value
->be16
);
987 case MFF_TUN_GBP_FLAGS
:
988 match_set_tun_gbp_flags(match
, value
->u8
);
991 match_set_tun_tos(match
, value
->u8
);
994 match_set_tun_ttl(match
, value
->u8
);
996 CASE_MFF_TUN_METADATA
:
997 tun_metadata_set_match(mf
, value
, NULL
, match
, err_str
);
1001 match_set_metadata(match
, value
->be64
);
1005 match_set_in_port(match
, u16_to_ofp(ntohs(value
->be16
)));
1008 case MFF_IN_PORT_OXM
: {
1010 ofputil_port_from_ofp11(value
->be32
, &port
);
1011 match_set_in_port(match
, port
);
1014 case MFF_ACTSET_OUTPUT
: {
1016 ofputil_port_from_ofp11(value
->be32
, &port
);
1017 match_set_actset_output(match
, port
);
1021 case MFF_SKB_PRIORITY
:
1022 match_set_skb_priority(match
, ntohl(value
->be32
));
1026 match_set_pkt_mark(match
, ntohl(value
->be32
));
1030 match_set_ct_state(match
, ntohl(value
->be32
));
1034 match_set_ct_zone(match
, ntohs(value
->be16
));
1038 match_set_ct_mark(match
, ntohl(value
->be32
));
1042 match_set_ct_label(match
, ntoh128(value
->be128
));
1045 case MFF_CT_NW_PROTO
:
1046 match_set_ct_nw_proto(match
, value
->u8
);
1050 match_set_ct_nw_src(match
, value
->be32
);
1054 match_set_ct_nw_dst(match
, value
->be32
);
1057 case MFF_CT_IPV6_SRC
:
1058 match_set_ct_ipv6_src(match
, &value
->ipv6
);
1061 case MFF_CT_IPV6_DST
:
1062 match_set_ct_ipv6_dst(match
, &value
->ipv6
);
1066 match_set_ct_tp_src(match
, value
->be16
);
1070 match_set_ct_tp_dst(match
, value
->be16
);
1074 match_set_reg(match
, mf
->id
- MFF_REG0
, ntohl(value
->be32
));
1078 match_set_xreg(match
, mf
->id
- MFF_XREG0
, ntohll(value
->be64
));
1082 match_set_xxreg(match
, mf
->id
- MFF_XXREG0
, ntoh128(value
->be128
));
1086 match_set_dl_src(match
, value
->mac
);
1090 match_set_dl_dst(match
, value
->mac
);
1094 match_set_dl_type(match
, value
->be16
);
1098 match_set_dl_tci(match
, value
->be16
);
1102 match_set_dl_vlan(match
, value
->be16
);
1105 match_set_vlan_vid(match
, value
->be16
);
1108 case MFF_DL_VLAN_PCP
:
1110 match_set_dl_vlan_pcp(match
, value
->u8
);
1113 case MFF_MPLS_LABEL
:
1114 match_set_mpls_label(match
, 0, value
->be32
);
1118 match_set_mpls_tc(match
, 0, value
->u8
);
1122 match_set_mpls_bos(match
, 0, value
->u8
);
1126 match_set_mpls_ttl(match
, 0, value
->u8
);
1130 match_set_nw_src(match
, value
->be32
);
1134 match_set_nw_dst(match
, value
->be32
);
1138 match_set_ipv6_src(match
, &value
->ipv6
);
1142 match_set_ipv6_dst(match
, &value
->ipv6
);
1145 case MFF_IPV6_LABEL
:
1146 match_set_ipv6_label(match
, value
->be32
);
1150 match_set_nw_proto(match
, value
->u8
);
1154 match_set_nw_dscp(match
, value
->u8
);
1157 case MFF_IP_DSCP_SHIFTED
:
1158 match_set_nw_dscp(match
, value
->u8
<< 2);
1162 match_set_nw_ecn(match
, value
->u8
);
1166 match_set_nw_ttl(match
, value
->u8
);
1170 match_set_nw_frag(match
, value
->u8
);
1174 match_set_nw_proto(match
, ntohs(value
->be16
));
1178 match_set_nw_src(match
, value
->be32
);
1182 match_set_nw_dst(match
, value
->be32
);
1187 match_set_arp_sha(match
, value
->mac
);
1192 match_set_arp_tha(match
, value
->mac
);
1198 match_set_tp_src(match
, value
->be16
);
1204 match_set_tp_dst(match
, value
->be16
);
1208 match_set_tcp_flags(match
, value
->be16
);
1211 case MFF_ICMPV4_TYPE
:
1212 case MFF_ICMPV6_TYPE
:
1213 match_set_icmp_type(match
, value
->u8
);
1216 case MFF_ICMPV4_CODE
:
1217 case MFF_ICMPV6_CODE
:
1218 match_set_icmp_code(match
, value
->u8
);
1222 match_set_nd_target(match
, &value
->ipv6
);
1226 MATCH_SET_FIELD_UINT8(match
, nsh
.flags
, value
->u8
);
1229 MATCH_SET_FIELD_UINT8(match
, nsh
.ttl
, value
->u8
);
1231 case MFF_NSH_MDTYPE
:
1232 MATCH_SET_FIELD_UINT8(match
, nsh
.mdtype
, value
->u8
);
1235 MATCH_SET_FIELD_UINT8(match
, nsh
.np
, value
->u8
);
1238 match
->wc
.masks
.nsh
.path_hdr
|= htonl(NSH_SPI_MASK
);
1239 nsh_path_hdr_set_spi(&match
->flow
.nsh
.path_hdr
, value
->be32
);
1242 match
->wc
.masks
.nsh
.path_hdr
|= htonl(NSH_SI_MASK
);
1243 nsh_path_hdr_set_si(&match
->flow
.nsh
.path_hdr
, value
->u8
);
1249 MATCH_SET_FIELD_BE32(match
, nsh
.context
[mf
->id
- MFF_NSH_C1
],
1259 /* Unwildcard the bits in 'mask' of the 'wc' member field described by 'mf'.
1260 * The caller is responsible for ensuring that 'wc' meets 'mf''s
1263 mf_mask_field_masked(const struct mf_field
*mf
, const union mf_value
*mask
,
1264 struct flow_wildcards
*wc
)
1266 union mf_value temp_mask
;
1267 /* For MFF_DL_VLAN, we cannot send a all 1's to flow_set_dl_vlan() as that
1268 * will be considered as OFP10_VLAN_NONE. So make sure the mask only has
1269 * valid bits in this case. */
1270 if (mf
->id
== MFF_DL_VLAN
) {
1271 temp_mask
.be16
= htons(VLAN_VID_MASK
) & mask
->be16
;
1275 union mf_value mask_value
;
1277 mf_get_value(mf
, &wc
->masks
, &mask_value
);
1278 for (size_t i
= 0; i
< mf
->n_bytes
; i
++) {
1279 mask_value
.b
[i
] |= mask
->b
[i
];
1281 mf_set_flow_value(mf
, &mask_value
, &wc
->masks
);
1284 /* Unwildcard 'wc' member field described by 'mf'. The caller is
1285 * responsible for ensuring that 'mask' meets 'mf''s prerequisites. */
1287 mf_mask_field(const struct mf_field
*mf
, struct flow_wildcards
*wc
)
1289 mf_mask_field_masked(mf
, &exact_match_mask
, wc
);
1293 field_len(const struct mf_field
*mf
, const union mf_value
*value_
)
1295 const uint8_t *value
= &value_
->u8
;
1298 if (!mf
->variable_len
) {
1306 for (i
= 0; i
< mf
->n_bytes
; i
++) {
1307 if (value
[i
] != 0) {
1312 return mf
->n_bytes
- i
;
1315 /* Returns the effective length of the field. For fixed length fields,
1316 * this is just the defined length. For variable length fields, it is
1317 * the minimum size encoding that retains the same meaning (i.e.
1318 * discarding leading zeros).
1320 * 'is_masked' returns (if non-NULL) whether the original contained
1321 * a mask. Otherwise, a mask that is the same length as the value
1322 * might be misinterpreted as an exact match. */
1324 mf_field_len(const struct mf_field
*mf
, const union mf_value
*value
,
1325 const union mf_value
*mask
, bool *is_masked_
)
1328 bool is_masked
= mask
&& !is_all_ones(mask
, mf
->n_bytes
);
1330 len
= field_len(mf
, value
);
1332 mask_len
= field_len(mf
, mask
);
1333 len
= MAX(len
, mask_len
);
1337 *is_masked_
= is_masked
;
1343 /* Sets 'flow' member field described by 'mf' to 'value'. The caller is
1344 * responsible for ensuring that 'flow' meets 'mf''s prerequisites.*/
1346 mf_set_flow_value(const struct mf_field
*mf
,
1347 const union mf_value
*value
, struct flow
*flow
)
1351 flow
->dp_hash
= ntohl(value
->be32
);
1354 flow
->recirc_id
= ntohl(value
->be32
);
1356 case MFF_PACKET_TYPE
:
1357 flow
->packet_type
= value
->be32
;
1360 flow
->conj_id
= ntohl(value
->be32
);
1363 flow
->tunnel
.tun_id
= value
->be64
;
1366 flow
->tunnel
.ip_src
= value
->be32
;
1369 flow
->tunnel
.ip_dst
= value
->be32
;
1371 case MFF_TUN_IPV6_SRC
:
1372 flow
->tunnel
.ipv6_src
= value
->ipv6
;
1374 case MFF_TUN_IPV6_DST
:
1375 flow
->tunnel
.ipv6_dst
= value
->ipv6
;
1378 flow
->tunnel
.flags
= (flow
->tunnel
.flags
& ~FLOW_TNL_PUB_F_MASK
) |
1381 case MFF_TUN_GBP_ID
:
1382 flow
->tunnel
.gbp_id
= value
->be16
;
1384 case MFF_TUN_GBP_FLAGS
:
1385 flow
->tunnel
.gbp_flags
= value
->u8
;
1388 flow
->tunnel
.ip_tos
= value
->u8
;
1391 flow
->tunnel
.ip_ttl
= value
->u8
;
1393 CASE_MFF_TUN_METADATA
:
1394 tun_metadata_write(&flow
->tunnel
, mf
, value
);
1397 flow
->metadata
= value
->be64
;
1401 flow
->in_port
.ofp_port
= u16_to_ofp(ntohs(value
->be16
));
1403 case MFF_IN_PORT_OXM
:
1404 ofputil_port_from_ofp11(value
->be32
, &flow
->in_port
.ofp_port
);
1406 case MFF_ACTSET_OUTPUT
:
1407 ofputil_port_from_ofp11(value
->be32
, &flow
->actset_output
);
1410 case MFF_SKB_PRIORITY
:
1411 flow
->skb_priority
= ntohl(value
->be32
);
1415 flow
->pkt_mark
= ntohl(value
->be32
);
1419 flow
->ct_state
= ntohl(value
->be32
);
1423 flow
->ct_zone
= ntohs(value
->be16
);
1427 flow
->ct_mark
= ntohl(value
->be32
);
1431 flow
->ct_label
= ntoh128(value
->be128
);
1434 case MFF_CT_NW_PROTO
:
1435 flow
->ct_nw_proto
= value
->u8
;
1439 flow
->ct_nw_src
= value
->be32
;
1443 flow
->ct_nw_dst
= value
->be32
;
1446 case MFF_CT_IPV6_SRC
:
1447 flow
->ct_ipv6_src
= value
->ipv6
;
1450 case MFF_CT_IPV6_DST
:
1451 flow
->ct_ipv6_dst
= value
->ipv6
;
1455 flow
->ct_tp_src
= value
->be16
;
1459 flow
->ct_tp_dst
= value
->be16
;
1463 flow
->regs
[mf
->id
- MFF_REG0
] = ntohl(value
->be32
);
1467 flow_set_xreg(flow
, mf
->id
- MFF_XREG0
, ntohll(value
->be64
));
1471 flow_set_xxreg(flow
, mf
->id
- MFF_XXREG0
, ntoh128(value
->be128
));
1475 flow
->dl_src
= value
->mac
;
1479 flow
->dl_dst
= value
->mac
;
1483 flow
->dl_type
= value
->be16
;
1487 flow
->vlans
[0].tci
= value
->be16
;
1488 flow_fix_vlan_tpid(flow
);
1492 flow_set_dl_vlan(flow
, value
->be16
);
1493 flow_fix_vlan_tpid(flow
);
1497 flow_set_vlan_vid(flow
, value
->be16
);
1498 flow_fix_vlan_tpid(flow
);
1501 case MFF_DL_VLAN_PCP
:
1503 flow_set_vlan_pcp(flow
, value
->u8
);
1504 flow_fix_vlan_tpid(flow
);
1507 case MFF_MPLS_LABEL
:
1508 flow_set_mpls_label(flow
, 0, value
->be32
);
1512 flow_set_mpls_tc(flow
, 0, value
->u8
);
1516 flow_set_mpls_bos(flow
, 0, value
->u8
);
1520 flow_set_mpls_ttl(flow
, 0, value
->u8
);
1524 flow
->nw_src
= value
->be32
;
1528 flow
->nw_dst
= value
->be32
;
1532 flow
->ipv6_src
= value
->ipv6
;
1536 flow
->ipv6_dst
= value
->ipv6
;
1539 case MFF_IPV6_LABEL
:
1540 flow
->ipv6_label
= value
->be32
& htonl(IPV6_LABEL_MASK
);
1544 flow
->nw_proto
= value
->u8
;
1548 flow
->nw_tos
&= ~IP_DSCP_MASK
;
1549 flow
->nw_tos
|= value
->u8
& IP_DSCP_MASK
;
1552 case MFF_IP_DSCP_SHIFTED
:
1553 flow
->nw_tos
&= ~IP_DSCP_MASK
;
1554 flow
->nw_tos
|= value
->u8
<< 2;
1558 flow
->nw_tos
&= ~IP_ECN_MASK
;
1559 flow
->nw_tos
|= value
->u8
& IP_ECN_MASK
;
1563 flow
->nw_ttl
= value
->u8
;
1567 flow
->nw_frag
= value
->u8
& FLOW_NW_FRAG_MASK
;
1571 flow
->nw_proto
= ntohs(value
->be16
);
1575 flow
->nw_src
= value
->be32
;
1579 flow
->nw_dst
= value
->be32
;
1584 flow
->arp_sha
= value
->mac
;
1589 flow
->arp_tha
= value
->mac
;
1595 flow
->tp_src
= value
->be16
;
1601 flow
->tp_dst
= value
->be16
;
1605 flow
->tcp_flags
= value
->be16
;
1608 case MFF_ICMPV4_TYPE
:
1609 case MFF_ICMPV6_TYPE
:
1610 flow
->tp_src
= htons(value
->u8
);
1613 case MFF_ICMPV4_CODE
:
1614 case MFF_ICMPV6_CODE
:
1615 flow
->tp_dst
= htons(value
->u8
);
1619 flow
->nd_target
= value
->ipv6
;
1623 flow
->nsh
.flags
= value
->u8
;
1626 flow
->nsh
.ttl
= value
->u8
;
1628 case MFF_NSH_MDTYPE
:
1629 flow
->nsh
.mdtype
= value
->u8
;
1632 flow
->nsh
.np
= value
->u8
;
1635 nsh_path_hdr_set_spi(&flow
->nsh
.path_hdr
, value
->be32
);
1638 nsh_path_hdr_set_si(&flow
->nsh
.path_hdr
, value
->u8
);
1644 flow
->nsh
.context
[mf
->id
- MFF_NSH_C1
] = value
->be32
;
1653 /* Consider each of 'src', 'mask', and 'dst' as if they were arrays of 8*n
1654 * bits. Then, for each 0 <= i < 8 * n such that mask[i] == 1, sets dst[i] =
1657 apply_mask(const uint8_t *src
, const uint8_t *mask
, uint8_t *dst
, size_t n
)
1661 for (i
= 0; i
< n
; i
++) {
1662 dst
[i
] = (src
[i
] & mask
[i
]) | (dst
[i
] & ~mask
[i
]);
1666 /* Sets 'flow' member field described by 'field' to 'value', except that bits
1667 * for which 'mask' has a 0-bit keep their existing values. The caller is
1668 * responsible for ensuring that 'flow' meets 'field''s prerequisites.*/
1670 mf_set_flow_value_masked(const struct mf_field
*field
,
1671 const union mf_value
*value
,
1672 const union mf_value
*mask
,
1677 mf_get_value(field
, flow
, &tmp
);
1678 apply_mask((const uint8_t *) value
, (const uint8_t *) mask
,
1679 (uint8_t *) &tmp
, field
->n_bytes
);
1680 mf_set_flow_value(field
, &tmp
, flow
);
1684 mf_is_tun_metadata(const struct mf_field
*mf
)
1686 return mf
->id
>= MFF_TUN_METADATA0
&&
1687 mf
->id
< MFF_TUN_METADATA0
+ TUN_METADATA_NUM_OPTS
;
1691 mf_is_pipeline_field(const struct mf_field
*mf
)
1697 case MFF_TUN_IPV6_SRC
:
1698 case MFF_TUN_IPV6_DST
:
1700 case MFF_TUN_GBP_ID
:
1701 case MFF_TUN_GBP_FLAGS
:
1702 CASE_MFF_TUN_METADATA
:
1705 case MFF_IN_PORT_OXM
:
1709 case MFF_PACKET_TYPE
:
1717 case MFF_ACTSET_OUTPUT
:
1718 case MFF_SKB_PRIORITY
:
1724 case MFF_CT_NW_PROTO
:
1727 case MFF_CT_IPV6_SRC
:
1728 case MFF_CT_IPV6_DST
:
1737 case MFF_DL_VLAN_PCP
:
1739 case MFF_MPLS_LABEL
:
1747 case MFF_IPV6_LABEL
:
1750 case MFF_IP_DSCP_SHIFTED
:
1766 case MFF_ICMPV4_TYPE
:
1767 case MFF_ICMPV4_CODE
:
1768 case MFF_ICMPV6_TYPE
:
1769 case MFF_ICMPV6_CODE
:
1775 case MFF_NSH_MDTYPE
:
1791 /* Returns true if 'mf' has previously been set in 'flow', false if
1792 * it contains a non-default value.
1794 * The caller is responsible for ensuring that 'flow' meets 'mf''s
1797 mf_is_set(const struct mf_field
*mf
, const struct flow
*flow
)
1799 if (!mf_is_tun_metadata(mf
)) {
1800 union mf_value value
;
1802 mf_get_value(mf
, flow
, &value
);
1803 return !is_all_zeros(&value
, mf
->n_bytes
);
1805 return ULLONG_GET(flow
->tunnel
.metadata
.present
.map
,
1806 mf
->id
- MFF_TUN_METADATA0
);
1810 /* Makes 'match' wildcard field 'mf'.
1812 * The caller is responsible for ensuring that 'match' meets 'mf''s
1815 * If non-NULL, 'err_str' returns a malloc'ed string describing any errors
1816 * with the request or NULL if there is no error. The caller is reponsible
1817 * for freeing the string. */
1819 mf_set_wild(const struct mf_field
*mf
, struct match
*match
, char **err_str
)
1827 match
->flow
.dp_hash
= 0;
1828 match
->wc
.masks
.dp_hash
= 0;
1831 match
->flow
.recirc_id
= 0;
1832 match
->wc
.masks
.recirc_id
= 0;
1834 case MFF_PACKET_TYPE
:
1835 match
->flow
.packet_type
= 0;
1836 match
->wc
.masks
.packet_type
= 0;
1839 match
->flow
.conj_id
= 0;
1840 match
->wc
.masks
.conj_id
= 0;
1843 match_set_tun_id_masked(match
, htonll(0), htonll(0));
1846 match_set_tun_src_masked(match
, htonl(0), htonl(0));
1849 match_set_tun_dst_masked(match
, htonl(0), htonl(0));
1851 case MFF_TUN_IPV6_SRC
:
1852 memset(&match
->wc
.masks
.tunnel
.ipv6_src
, 0,
1853 sizeof match
->wc
.masks
.tunnel
.ipv6_src
);
1854 memset(&match
->flow
.tunnel
.ipv6_src
, 0,
1855 sizeof match
->flow
.tunnel
.ipv6_src
);
1857 case MFF_TUN_IPV6_DST
:
1858 memset(&match
->wc
.masks
.tunnel
.ipv6_dst
, 0,
1859 sizeof match
->wc
.masks
.tunnel
.ipv6_dst
);
1860 memset(&match
->flow
.tunnel
.ipv6_dst
, 0,
1861 sizeof match
->flow
.tunnel
.ipv6_dst
);
1864 match_set_tun_flags_masked(match
, 0, 0);
1866 case MFF_TUN_GBP_ID
:
1867 match_set_tun_gbp_id_masked(match
, 0, 0);
1869 case MFF_TUN_GBP_FLAGS
:
1870 match_set_tun_gbp_flags_masked(match
, 0, 0);
1873 match_set_tun_tos_masked(match
, 0, 0);
1876 match_set_tun_ttl_masked(match
, 0, 0);
1878 CASE_MFF_TUN_METADATA
:
1879 tun_metadata_set_match(mf
, NULL
, NULL
, match
, err_str
);
1883 match_set_metadata_masked(match
, htonll(0), htonll(0));
1887 case MFF_IN_PORT_OXM
:
1888 match
->flow
.in_port
.ofp_port
= 0;
1889 match
->wc
.masks
.in_port
.ofp_port
= 0;
1891 case MFF_ACTSET_OUTPUT
:
1892 match
->flow
.actset_output
= 0;
1893 match
->wc
.masks
.actset_output
= 0;
1896 case MFF_SKB_PRIORITY
:
1897 match
->flow
.skb_priority
= 0;
1898 match
->wc
.masks
.skb_priority
= 0;
1902 match
->flow
.pkt_mark
= 0;
1903 match
->wc
.masks
.pkt_mark
= 0;
1907 match
->flow
.ct_state
= 0;
1908 match
->wc
.masks
.ct_state
= 0;
1912 match
->flow
.ct_zone
= 0;
1913 match
->wc
.masks
.ct_zone
= 0;
1917 match
->flow
.ct_mark
= 0;
1918 match
->wc
.masks
.ct_mark
= 0;
1922 memset(&match
->flow
.ct_label
, 0, sizeof(match
->flow
.ct_label
));
1923 memset(&match
->wc
.masks
.ct_label
, 0, sizeof(match
->wc
.masks
.ct_label
));
1926 case MFF_CT_NW_PROTO
:
1927 match
->flow
.ct_nw_proto
= 0;
1928 match
->wc
.masks
.ct_nw_proto
= 0;
1932 match
->flow
.ct_nw_src
= 0;
1933 match
->wc
.masks
.ct_nw_src
= 0;
1937 match
->flow
.ct_nw_dst
= 0;
1938 match
->wc
.masks
.ct_nw_dst
= 0;
1941 case MFF_CT_IPV6_SRC
:
1942 memset(&match
->flow
.ct_ipv6_src
, 0, sizeof(match
->flow
.ct_ipv6_src
));
1943 WC_UNMASK_FIELD(&match
->wc
, ct_ipv6_src
);
1946 case MFF_CT_IPV6_DST
:
1947 memset(&match
->flow
.ct_ipv6_dst
, 0, sizeof(match
->flow
.ct_ipv6_dst
));
1948 WC_UNMASK_FIELD(&match
->wc
, ct_ipv6_dst
);
1952 match
->flow
.ct_tp_src
= 0;
1953 match
->wc
.masks
.ct_tp_src
= 0;
1957 match
->flow
.ct_tp_dst
= 0;
1958 match
->wc
.masks
.ct_tp_dst
= 0;
1962 match_set_reg_masked(match
, mf
->id
- MFF_REG0
, 0, 0);
1966 match_set_xreg_masked(match
, mf
->id
- MFF_XREG0
, 0, 0);
1970 match_set_xxreg_masked(match
, mf
->id
- MFF_XXREG0
, OVS_U128_ZERO
,
1976 match
->flow
.dl_src
= eth_addr_zero
;
1977 match
->wc
.masks
.dl_src
= eth_addr_zero
;
1981 match
->flow
.dl_dst
= eth_addr_zero
;
1982 match
->wc
.masks
.dl_dst
= eth_addr_zero
;
1986 match
->flow
.dl_type
= htons(0);
1987 match
->wc
.masks
.dl_type
= htons(0);
1991 match_set_dl_tci_masked(match
, htons(0), htons(0));
1996 match_set_any_vid(match
);
1999 case MFF_DL_VLAN_PCP
:
2001 match_set_any_pcp(match
);
2004 case MFF_MPLS_LABEL
:
2005 match_set_any_mpls_label(match
, 0);
2009 match_set_any_mpls_tc(match
, 0);
2013 match_set_any_mpls_bos(match
, 0);
2017 match_set_any_mpls_ttl(match
, 0);
2022 match_set_nw_src_masked(match
, htonl(0), htonl(0));
2027 match_set_nw_dst_masked(match
, htonl(0), htonl(0));
2031 memset(&match
->wc
.masks
.ipv6_src
, 0, sizeof match
->wc
.masks
.ipv6_src
);
2032 memset(&match
->flow
.ipv6_src
, 0, sizeof match
->flow
.ipv6_src
);
2036 memset(&match
->wc
.masks
.ipv6_dst
, 0, sizeof match
->wc
.masks
.ipv6_dst
);
2037 memset(&match
->flow
.ipv6_dst
, 0, sizeof match
->flow
.ipv6_dst
);
2040 case MFF_IPV6_LABEL
:
2041 match
->wc
.masks
.ipv6_label
= htonl(0);
2042 match
->flow
.ipv6_label
= htonl(0);
2046 match
->wc
.masks
.nw_proto
= 0;
2047 match
->flow
.nw_proto
= 0;
2051 case MFF_IP_DSCP_SHIFTED
:
2052 match
->wc
.masks
.nw_tos
&= ~IP_DSCP_MASK
;
2053 match
->flow
.nw_tos
&= ~IP_DSCP_MASK
;
2057 match
->wc
.masks
.nw_tos
&= ~IP_ECN_MASK
;
2058 match
->flow
.nw_tos
&= ~IP_ECN_MASK
;
2062 match
->wc
.masks
.nw_ttl
= 0;
2063 match
->flow
.nw_ttl
= 0;
2067 match
->wc
.masks
.nw_frag
&= ~FLOW_NW_FRAG_MASK
;
2068 match
->flow
.nw_frag
&= ~FLOW_NW_FRAG_MASK
;
2072 match
->wc
.masks
.nw_proto
= 0;
2073 match
->flow
.nw_proto
= 0;
2078 match
->flow
.arp_sha
= eth_addr_zero
;
2079 match
->wc
.masks
.arp_sha
= eth_addr_zero
;
2084 match
->flow
.arp_tha
= eth_addr_zero
;
2085 match
->wc
.masks
.arp_tha
= eth_addr_zero
;
2091 case MFF_ICMPV4_TYPE
:
2092 case MFF_ICMPV6_TYPE
:
2093 match
->wc
.masks
.tp_src
= htons(0);
2094 match
->flow
.tp_src
= htons(0);
2100 case MFF_ICMPV4_CODE
:
2101 case MFF_ICMPV6_CODE
:
2102 match
->wc
.masks
.tp_dst
= htons(0);
2103 match
->flow
.tp_dst
= htons(0);
2107 match
->wc
.masks
.tcp_flags
= htons(0);
2108 match
->flow
.tcp_flags
= htons(0);
2112 memset(&match
->wc
.masks
.nd_target
, 0,
2113 sizeof match
->wc
.masks
.nd_target
);
2114 memset(&match
->flow
.nd_target
, 0, sizeof match
->flow
.nd_target
);
2118 MATCH_SET_FIELD_MASKED(match
, nsh
.flags
, 0, 0);
2121 MATCH_SET_FIELD_MASKED(match
, nsh
.ttl
, 0, 0);
2123 case MFF_NSH_MDTYPE
:
2124 MATCH_SET_FIELD_MASKED(match
, nsh
.mdtype
, 0, 0);
2127 MATCH_SET_FIELD_MASKED(match
, nsh
.np
, 0, 0);
2130 match
->wc
.masks
.nsh
.path_hdr
&= ~htonl(NSH_SPI_MASK
);
2131 nsh_path_hdr_set_spi(&match
->flow
.nsh
.path_hdr
, htonl(0));
2134 match
->wc
.masks
.nsh
.path_hdr
&= ~htonl(NSH_SI_MASK
);
2135 nsh_path_hdr_set_si(&match
->flow
.nsh
.path_hdr
, 0);
2141 MATCH_SET_FIELD_MASKED(match
, nsh
.context
[mf
->id
- MFF_NSH_C1
],
2142 htonl(0), htonl(0));
2151 /* Makes 'match' match field 'mf' with the specified 'value' and 'mask'.
2152 * 'value' specifies a value to match and 'mask' specifies a wildcard pattern,
2153 * with a 1-bit indicating that the corresponding value bit must match and a
2154 * 0-bit indicating a don't-care.
2156 * If 'mask' is NULL or points to all-1-bits, then this call is equivalent to
2157 * mf_set_value(mf, value, match). If 'mask' points to all-0-bits, then this
2158 * call is equivalent to mf_set_wild(mf, match).
2160 * 'mask' must be a valid mask for 'mf' (see mf_is_mask_valid()). The caller
2161 * is responsible for ensuring that 'match' meets 'mf''s prerequisites.
2163 * If non-NULL, 'err_str' returns a malloc'ed string describing any errors
2164 * with the request or NULL if there is no error. The caller is reponsible
2165 * for freeing the string.
2167 * Return a set of enum ofputil_protocol bits (as an uint32_t to avoid circular
2168 * dependency on enum ofputil_protocol definition) indicating which OpenFlow
2169 * protocol versions can support this functionality. */
2171 mf_set(const struct mf_field
*mf
,
2172 const union mf_value
*value
, const union mf_value
*mask
,
2173 struct match
*match
, char **err_str
)
2175 if (!mask
|| is_all_ones(mask
, mf
->n_bytes
)) {
2176 mf_set_value(mf
, value
, match
, err_str
);
2177 return mf
->usable_protocols_exact
;
2178 } else if (is_all_zeros(mask
, mf
->n_bytes
) && !mf_is_tun_metadata(mf
)) {
2179 /* Tunnel metadata matches on the existence of the field itself, so
2180 * it still needs to be encoded even if the value is wildcarded. */
2181 mf_set_wild(mf
, match
, err_str
);
2182 return OFPUTIL_P_ANY
;
2191 case MFF_CT_NW_PROTO
:
2194 case MFF_CT_IPV6_SRC
:
2195 case MFF_CT_IPV6_DST
:
2199 case MFF_PACKET_TYPE
:
2202 case MFF_IN_PORT_OXM
:
2203 case MFF_ACTSET_OUTPUT
:
2204 case MFF_SKB_PRIORITY
:
2207 case MFF_DL_VLAN_PCP
:
2209 case MFF_MPLS_LABEL
:
2216 case MFF_IP_DSCP_SHIFTED
:
2219 case MFF_ICMPV4_TYPE
:
2220 case MFF_ICMPV4_CODE
:
2221 case MFF_ICMPV6_TYPE
:
2222 case MFF_ICMPV6_CODE
:
2223 return OFPUTIL_P_NONE
;
2226 match_set_dp_hash_masked(match
, ntohl(value
->be32
), ntohl(mask
->be32
));
2229 match_set_tun_id_masked(match
, value
->be64
, mask
->be64
);
2232 match_set_tun_src_masked(match
, value
->be32
, mask
->be32
);
2235 match_set_tun_dst_masked(match
, value
->be32
, mask
->be32
);
2237 case MFF_TUN_IPV6_SRC
:
2238 match_set_tun_ipv6_src_masked(match
, &value
->ipv6
, &mask
->ipv6
);
2240 case MFF_TUN_IPV6_DST
:
2241 match_set_tun_ipv6_dst_masked(match
, &value
->ipv6
, &mask
->ipv6
);
2244 match_set_tun_flags_masked(match
, ntohs(value
->be16
), ntohs(mask
->be16
));
2246 case MFF_TUN_GBP_ID
:
2247 match_set_tun_gbp_id_masked(match
, value
->be16
, mask
->be16
);
2249 case MFF_TUN_GBP_FLAGS
:
2250 match_set_tun_gbp_flags_masked(match
, value
->u8
, mask
->u8
);
2253 match_set_tun_ttl_masked(match
, value
->u8
, mask
->u8
);
2256 match_set_tun_tos_masked(match
, value
->u8
, mask
->u8
);
2258 CASE_MFF_TUN_METADATA
:
2259 tun_metadata_set_match(mf
, value
, mask
, match
, err_str
);
2263 match_set_metadata_masked(match
, value
->be64
, mask
->be64
);
2267 match_set_reg_masked(match
, mf
->id
- MFF_REG0
,
2268 ntohl(value
->be32
), ntohl(mask
->be32
));
2272 match_set_xreg_masked(match
, mf
->id
- MFF_XREG0
,
2273 ntohll(value
->be64
), ntohll(mask
->be64
));
2277 match_set_xxreg_masked(match
, mf
->id
- MFF_XXREG0
,
2278 ntoh128(value
->be128
), ntoh128(mask
->be128
));
2283 match_set_pkt_mark_masked(match
, ntohl(value
->be32
),
2288 match_set_ct_state_masked(match
, ntohl(value
->be32
), ntohl(mask
->be32
));
2292 match_set_ct_mark_masked(match
, ntohl(value
->be32
), ntohl(mask
->be32
));
2296 match_set_ct_label_masked(match
, ntoh128(value
->be128
),
2297 ntoh128(mask
->be128
));
2301 match_set_dl_dst_masked(match
, value
->mac
, mask
->mac
);
2305 match_set_dl_src_masked(match
, value
->mac
, mask
->mac
);
2310 match_set_arp_sha_masked(match
, value
->mac
, mask
->mac
);
2315 match_set_arp_tha_masked(match
, value
->mac
, mask
->mac
);
2319 match_set_dl_tci_masked(match
, value
->be16
, mask
->be16
);
2323 match_set_vlan_vid_masked(match
, value
->be16
, mask
->be16
);
2327 match_set_nw_src_masked(match
, value
->be32
, mask
->be32
);
2331 match_set_nw_dst_masked(match
, value
->be32
, mask
->be32
);
2335 match_set_ipv6_src_masked(match
, &value
->ipv6
, &mask
->ipv6
);
2339 match_set_ipv6_dst_masked(match
, &value
->ipv6
, &mask
->ipv6
);
2342 case MFF_IPV6_LABEL
:
2343 if ((mask
->be32
& htonl(IPV6_LABEL_MASK
)) == htonl(IPV6_LABEL_MASK
)) {
2344 mf_set_value(mf
, value
, match
, err_str
);
2346 match_set_ipv6_label_masked(match
, value
->be32
, mask
->be32
);
2351 match_set_nd_target_masked(match
, &value
->ipv6
, &mask
->ipv6
);
2355 match_set_nw_frag_masked(match
, value
->u8
, mask
->u8
);
2359 match_set_nw_src_masked(match
, value
->be32
, mask
->be32
);
2363 match_set_nw_dst_masked(match
, value
->be32
, mask
->be32
);
2369 match_set_tp_src_masked(match
, value
->be16
, mask
->be16
);
2375 match_set_tp_dst_masked(match
, value
->be16
, mask
->be16
);
2379 match_set_tcp_flags_masked(match
, value
->be16
, mask
->be16
);
2383 MATCH_SET_FIELD_MASKED(match
, nsh
.flags
, value
->u8
, mask
->u8
);
2386 MATCH_SET_FIELD_MASKED(match
, nsh
.ttl
, value
->u8
, mask
->u8
);
2388 case MFF_NSH_MDTYPE
:
2389 MATCH_SET_FIELD_MASKED(match
, nsh
.mdtype
, value
->u8
, mask
->u8
);
2392 MATCH_SET_FIELD_MASKED(match
, nsh
.np
, value
->u8
, mask
->u8
);
2395 match
->wc
.masks
.nsh
.path_hdr
|= mask
->be32
;
2396 nsh_path_hdr_set_spi(&match
->flow
.nsh
.path_hdr
,
2397 value
->be32
& mask
->be32
);
2400 match
->wc
.masks
.nsh
.path_hdr
|= htonl(mask
->u8
);
2401 nsh_path_hdr_set_si(&match
->flow
.nsh
.path_hdr
,
2402 value
->u8
& mask
->u8
);
2408 MATCH_SET_FIELD_MASKED(match
, nsh
.context
[mf
->id
- MFF_NSH_C1
],
2409 value
->be32
, mask
->be32
);
2417 return ((mf
->usable_protocols_bitwise
== mf
->usable_protocols_cidr
2418 || ip_is_cidr(mask
->be32
))
2419 ? mf
->usable_protocols_cidr
2420 : mf
->usable_protocols_bitwise
);
2424 mf_check__(const struct mf_subfield
*sf
, const struct match
*match
,
2428 VLOG_WARN_RL(&rl
, "unknown %s field", type
);
2429 return OFPERR_OFPBAC_BAD_SET_TYPE
;
2430 } else if (!sf
->n_bits
) {
2431 VLOG_WARN_RL(&rl
, "zero bit %s field %s", type
, sf
->field
->name
);
2432 return OFPERR_OFPBAC_BAD_SET_LEN
;
2433 } else if (sf
->ofs
>= sf
->field
->n_bits
) {
2434 VLOG_WARN_RL(&rl
, "bit offset %d exceeds %d-bit width of %s field %s",
2435 sf
->ofs
, sf
->field
->n_bits
, type
, sf
->field
->name
);
2436 return OFPERR_OFPBAC_BAD_SET_LEN
;
2437 } else if (sf
->ofs
+ sf
->n_bits
> sf
->field
->n_bits
) {
2438 VLOG_WARN_RL(&rl
, "bit offset %d and width %d exceeds %d-bit width "
2439 "of %s field %s", sf
->ofs
, sf
->n_bits
,
2440 sf
->field
->n_bits
, type
, sf
->field
->name
);
2441 return OFPERR_OFPBAC_BAD_SET_LEN
;
2442 } else if (match
&& !mf_are_match_prereqs_ok(sf
->field
, match
)) {
2443 VLOG_WARN_RL(&rl
, "%s field %s lacks correct prerequisites",
2444 type
, sf
->field
->name
);
2445 return OFPERR_OFPBAC_MATCH_INCONSISTENT
;
2451 /* Sets all the bits in 'sf' to 1 within 'wc', if 'wc' is nonnull. */
2453 unwildcard_subfield(const struct mf_subfield
*sf
, struct flow_wildcards
*wc
)
2456 union mf_value mask
;
2458 memset(&mask
, 0, sizeof mask
);
2459 bitwise_one(&mask
, sf
->field
->n_bytes
, sf
->ofs
, sf
->n_bits
);
2460 mf_mask_field_masked(sf
->field
, &mask
, wc
);
2464 /* Copies 'src' into 'dst' within 'flow', and sets all the bits in 'src' and
2465 * 'dst' to 1s in 'wc', if 'wc' is nonnull.
2467 * 'src' and 'dst' may overlap. */
2469 mf_subfield_copy(const struct mf_subfield
*src
,
2470 const struct mf_subfield
*dst
,
2471 struct flow
*flow
, struct flow_wildcards
*wc
)
2473 ovs_assert(src
->n_bits
== dst
->n_bits
);
2474 if (mf_are_prereqs_ok(dst
->field
, flow
, wc
)
2475 && mf_are_prereqs_ok(src
->field
, flow
, wc
)) {
2476 unwildcard_subfield(src
, wc
);
2477 unwildcard_subfield(dst
, wc
);
2479 union mf_value src_value
;
2480 union mf_value dst_value
;
2481 mf_get_value(dst
->field
, flow
, &dst_value
);
2482 mf_get_value(src
->field
, flow
, &src_value
);
2483 bitwise_copy(&src_value
, src
->field
->n_bytes
, src
->ofs
,
2484 &dst_value
, dst
->field
->n_bytes
, dst
->ofs
,
2486 mf_set_flow_value(dst
->field
, &dst_value
, flow
);
2490 /* Swaps the bits in 'src' and 'dst' within 'flow', and sets all the bits in
2491 * 'src' and 'dst' to 1s in 'wc', if 'wc' is nonnull.
2493 * 'src' and 'dst' may overlap. */
2495 mf_subfield_swap(const struct mf_subfield
*a
,
2496 const struct mf_subfield
*b
,
2497 struct flow
*flow
, struct flow_wildcards
*wc
)
2499 ovs_assert(a
->n_bits
== b
->n_bits
);
2500 if (mf_are_prereqs_ok(a
->field
, flow
, wc
)
2501 && mf_are_prereqs_ok(b
->field
, flow
, wc
)) {
2502 unwildcard_subfield(a
, wc
);
2503 unwildcard_subfield(b
, wc
);
2505 union mf_value a_value
;
2506 union mf_value b_value
;
2507 mf_get_value(a
->field
, flow
, &a_value
);
2508 mf_get_value(b
->field
, flow
, &b_value
);
2509 union mf_value b2_value
= b_value
;
2511 /* Copy 'a' into 'b'. */
2512 bitwise_copy(&a_value
, a
->field
->n_bytes
, a
->ofs
,
2513 &b_value
, b
->field
->n_bytes
, b
->ofs
,
2515 mf_set_flow_value(b
->field
, &b_value
, flow
);
2517 /* Copy original 'b' into 'a'. */
2518 bitwise_copy(&b2_value
, b
->field
->n_bytes
, b
->ofs
,
2519 &a_value
, a
->field
->n_bytes
, a
->ofs
,
2521 mf_set_flow_value(a
->field
, &a_value
, flow
);
2525 /* Checks whether 'sf' is valid for reading a subfield out of 'flow'. Returns
2526 * 0 if so, otherwise an OpenFlow error code (e.g. as returned by
2529 mf_check_src(const struct mf_subfield
*sf
, const struct match
*match
)
2531 return mf_check__(sf
, match
, "source");
2534 /* Checks whether 'sf' is valid for writing a subfield into 'flow'. Returns 0
2535 * if so, otherwise an OpenFlow error code (e.g. as returned by
2538 mf_check_dst(const struct mf_subfield
*sf
, const struct match
*match
)
2540 int error
= mf_check__(sf
, match
, "destination");
2541 if (!error
&& !sf
->field
->writable
) {
2542 VLOG_WARN_RL(&rl
, "destination field %s is not writable",
2544 return OFPERR_OFPBAC_BAD_SET_ARGUMENT
;
2549 /* Copies the value and wildcard bit pattern for 'mf' from 'match' into the
2550 * 'value' and 'mask', respectively. */
2552 mf_get(const struct mf_field
*mf
, const struct match
*match
,
2553 union mf_value
*value
, union mf_value
*mask
)
2555 mf_get_value(mf
, &match
->flow
, value
);
2556 mf_get_mask(mf
, &match
->wc
, mask
);
2560 mf_from_integer_string(const struct mf_field
*mf
, const char *s
,
2561 uint8_t *valuep
, uint8_t *maskp
)
2564 const char *err_str
= "";
2567 err
= parse_int_string(s
, valuep
, mf
->n_bytes
, &tail
);
2568 if (err
|| (*tail
!= '\0' && *tail
!= '/')) {
2574 err
= parse_int_string(tail
+ 1, maskp
, mf
->n_bytes
, &tail
);
2575 if (err
|| *tail
!= '\0') {
2580 memset(maskp
, 0xff, mf
->n_bytes
);
2586 if (err
== ERANGE
) {
2587 return xasprintf("%s: %s too large for %u-byte field %s",
2588 s
, err_str
, mf
->n_bytes
, mf
->name
);
2590 return xasprintf("%s: bad syntax for %s %s", s
, mf
->name
, err_str
);
2595 mf_from_packet_type_string(const char *s
, ovs_be32
*packet_type
)
2598 const char *err_str
= "";
2602 err_str
= "missing '('";
2606 err
= parse_int_string(s
, (uint8_t *)packet_type
, 2, &tail
);
2612 err_str
= "missing ','";
2616 err
= parse_int_string(s
, ((uint8_t *)packet_type
) + 2, 2, &tail
);
2618 err_str
= "ns_type";
2622 err_str
= "missing ')'";
2629 return xasprintf("%s: bad syntax for packet type %s", s
, err_str
);
2633 mf_from_ethernet_string(const struct mf_field
*mf
, const char *s
,
2634 struct eth_addr
*mac
, struct eth_addr
*mask
)
2638 ovs_assert(mf
->n_bytes
== ETH_ADDR_LEN
);
2641 if (ovs_scan(s
, ETH_ADDR_SCAN_FMT
"%n", ETH_ADDR_SCAN_ARGS(*mac
), &n
)
2642 && n
== strlen(s
)) {
2643 *mask
= eth_addr_exact
;
2648 if (ovs_scan(s
, ETH_ADDR_SCAN_FMT
"/"ETH_ADDR_SCAN_FMT
"%n",
2649 ETH_ADDR_SCAN_ARGS(*mac
), ETH_ADDR_SCAN_ARGS(*mask
), &n
)
2650 && n
== strlen(s
)) {
2654 return xasprintf("%s: invalid Ethernet address", s
);
2658 mf_from_ipv4_string(const struct mf_field
*mf
, const char *s
,
2659 ovs_be32
*ip
, ovs_be32
*mask
)
2661 ovs_assert(mf
->n_bytes
== sizeof *ip
);
2662 return ip_parse_masked(s
, ip
, mask
);
2666 mf_from_ipv6_string(const struct mf_field
*mf
, const char *s
,
2667 struct in6_addr
*ipv6
, struct in6_addr
*mask
)
2669 ovs_assert(mf
->n_bytes
== sizeof *ipv6
);
2670 return ipv6_parse_masked(s
, ipv6
, mask
);
2674 mf_from_ofp_port_string(const struct mf_field
*mf
, const char *s
,
2675 const struct ofputil_port_map
*port_map
,
2676 ovs_be16
*valuep
, ovs_be16
*maskp
)
2680 ovs_assert(mf
->n_bytes
== sizeof(ovs_be16
));
2682 if (ofputil_port_from_string(s
, port_map
, &port
)) {
2683 *valuep
= htons(ofp_to_u16(port
));
2684 *maskp
= OVS_BE16_MAX
;
2687 return xasprintf("%s: invalid or unknown port for %s", s
, mf
->name
);
2691 mf_from_ofp_port_string32(const struct mf_field
*mf
, const char *s
,
2692 const struct ofputil_port_map
*port_map
,
2693 ovs_be32
*valuep
, ovs_be32
*maskp
)
2697 ovs_assert(mf
->n_bytes
== sizeof(ovs_be32
));
2698 if (ofputil_port_from_string(s
, port_map
, &port
)) {
2699 *valuep
= ofputil_port_to_ofp11(port
);
2700 *maskp
= OVS_BE32_MAX
;
2703 return xasprintf("%s: port value out of range for %s", s
, mf
->name
);
2706 struct frag_handling
{
2712 static const struct frag_handling all_frags
[] = {
2713 #define A FLOW_NW_FRAG_ANY
2714 #define L FLOW_NW_FRAG_LATER
2715 /* name mask value */
2718 { "first", A
|L
, A
},
2719 { "later", A
|L
, A
|L
},
2724 { "not_later", L
, 0 },
2731 mf_from_frag_string(const char *s
, uint8_t *valuep
, uint8_t *maskp
)
2733 const struct frag_handling
*h
;
2735 for (h
= all_frags
; h
< &all_frags
[ARRAY_SIZE(all_frags
)]; h
++) {
2736 if (!strcasecmp(s
, h
->name
)) {
2737 /* We force the upper bits of the mask on to make mf_parse_value()
2738 * happy (otherwise it will never think it's an exact match.) */
2739 *maskp
= h
->mask
| ~FLOW_NW_FRAG_MASK
;
2745 return xasprintf("%s: unknown fragment type (valid types are \"no\", "
2746 "\"yes\", \"first\", \"later\", \"not_first\"", s
);
2750 parse_mf_flags(const char *s
, const char *(*bit_to_string
)(uint32_t),
2751 const char *field_name
, ovs_be16
*flagsp
, ovs_be16 allowed
,
2756 uint32_t flags
, mask
;
2758 err
= parse_flags(s
, bit_to_string
, '\0', field_name
, &err_str
,
2759 &flags
, ntohs(allowed
), maskp
? &mask
: NULL
);
2764 *flagsp
= htons(flags
);
2766 *maskp
= htons(mask
);
2773 mf_from_tcp_flags_string(const char *s
, ovs_be16
*flagsp
, ovs_be16
*maskp
)
2775 return parse_mf_flags(s
, packet_tcp_flag_to_string
, "TCP", flagsp
,
2776 TCP_FLAGS_BE16(OVS_BE16_MAX
), maskp
);
2780 mf_from_tun_flags_string(const char *s
, ovs_be16
*flagsp
, ovs_be16
*maskp
)
2782 return parse_mf_flags(s
, flow_tun_flag_to_string
, "tunnel", flagsp
,
2783 htons(FLOW_TNL_PUB_F_MASK
), maskp
);
2787 mf_from_ct_state_string(const char *s
, ovs_be32
*flagsp
, ovs_be32
*maskp
)
2791 uint32_t flags
, mask
;
2793 err
= parse_flags(s
, ct_state_to_string
, '\0', "ct_state", &err_str
,
2794 &flags
, CS_SUPPORTED_MASK
, maskp
? &mask
: NULL
);
2799 *flagsp
= htonl(flags
);
2801 *maskp
= htonl(mask
);
2807 /* Parses 's', a string value for field 'mf', into 'value' and 'mask'. Returns
2808 * NULL if successful, otherwise a malloc()'d string describing the error. */
2810 mf_parse(const struct mf_field
*mf
, const char *s
,
2811 const struct ofputil_port_map
*port_map
,
2812 union mf_value
*value
, union mf_value
*mask
)
2816 if (!strcmp(s
, "*")) {
2817 memset(value
, 0, mf
->n_bytes
);
2818 memset(mask
, 0, mf
->n_bytes
);
2822 switch (mf
->string
) {
2824 case MFS_HEXADECIMAL
:
2825 error
= mf_from_integer_string(mf
, s
,
2826 (uint8_t *) value
, (uint8_t *) mask
);
2830 ovs_assert(mf
->n_bytes
== sizeof(ovs_be32
));
2831 error
= mf_from_ct_state_string(s
, &value
->be32
, &mask
->be32
);
2835 error
= mf_from_ethernet_string(mf
, s
, &value
->mac
, &mask
->mac
);
2839 error
= mf_from_ipv4_string(mf
, s
, &value
->be32
, &mask
->be32
);
2843 error
= mf_from_ipv6_string(mf
, s
, &value
->ipv6
, &mask
->ipv6
);
2847 error
= mf_from_ofp_port_string(mf
, s
, port_map
,
2848 &value
->be16
, &mask
->be16
);
2851 case MFS_OFP_PORT_OXM
:
2852 error
= mf_from_ofp_port_string32(mf
, s
, port_map
,
2853 &value
->be32
, &mask
->be32
);
2857 error
= mf_from_frag_string(s
, &value
->u8
, &mask
->u8
);
2861 ovs_assert(mf
->n_bytes
== sizeof(ovs_be16
));
2862 error
= mf_from_tun_flags_string(s
, &value
->be16
, &mask
->be16
);
2866 ovs_assert(mf
->n_bytes
== sizeof(ovs_be16
));
2867 error
= mf_from_tcp_flags_string(s
, &value
->be16
, &mask
->be16
);
2870 case MFS_PACKET_TYPE
:
2871 ovs_assert(mf
->n_bytes
== sizeof(ovs_be32
));
2872 error
= mf_from_packet_type_string(s
, &value
->be32
);
2873 mask
->be32
= OVS_BE32_MAX
;
2880 if (!error
&& !mf_is_mask_valid(mf
, mask
)) {
2881 error
= xasprintf("%s: invalid mask for field %s", s
, mf
->name
);
2886 /* Parses 's', a string value for field 'mf', into 'value'. Returns NULL if
2887 * successful, otherwise a malloc()'d string describing the error. */
2889 mf_parse_value(const struct mf_field
*mf
, const char *s
,
2890 const struct ofputil_port_map
*port_map
, union mf_value
*value
)
2892 union mf_value mask
;
2895 error
= mf_parse(mf
, s
, port_map
, value
, &mask
);
2900 if (!is_all_ones((const uint8_t *) &mask
, mf
->n_bytes
)) {
2901 return xasprintf("%s: wildcards not allowed here", s
);
2907 mf_format_integer_string(const struct mf_field
*mf
, const uint8_t *valuep
,
2908 const uint8_t *maskp
, struct ds
*s
)
2910 if (mf
->string
== MFS_HEXADECIMAL
) {
2911 ds_put_hex(s
, valuep
, mf
->n_bytes
);
2913 unsigned long long int integer
= 0;
2916 ovs_assert(mf
->n_bytes
<= 8);
2917 for (i
= 0; i
< mf
->n_bytes
; i
++) {
2918 integer
= (integer
<< 8) | valuep
[i
];
2920 ds_put_format(s
, "%lld", integer
);
2924 /* I guess we could write the mask in decimal for MFS_DECIMAL but I'm
2925 * not sure that that a bit-mask written in decimal is ever easier to
2926 * understand than the same bit-mask written in hexadecimal. */
2927 ds_put_char(s
, '/');
2928 ds_put_hex(s
, maskp
, mf
->n_bytes
);
2933 mf_format_frag_string(uint8_t value
, uint8_t mask
, struct ds
*s
)
2935 const struct frag_handling
*h
;
2937 mask
&= FLOW_NW_FRAG_MASK
;
2940 for (h
= all_frags
; h
< &all_frags
[ARRAY_SIZE(all_frags
)]; h
++) {
2941 if (value
== h
->value
&& mask
== h
->mask
) {
2942 ds_put_cstr(s
, h
->name
);
2946 ds_put_cstr(s
, "<error>");
2950 mf_format_tnl_flags_string(ovs_be16 value
, ovs_be16 mask
, struct ds
*s
)
2952 format_flags_masked(s
, NULL
, flow_tun_flag_to_string
, ntohs(value
),
2953 ntohs(mask
) & FLOW_TNL_PUB_F_MASK
, FLOW_TNL_PUB_F_MASK
);
2957 mf_format_tcp_flags_string(ovs_be16 value
, ovs_be16 mask
, struct ds
*s
)
2959 format_flags_masked(s
, NULL
, packet_tcp_flag_to_string
, ntohs(value
),
2960 TCP_FLAGS(mask
), TCP_FLAGS(OVS_BE16_MAX
));
2964 mf_format_ct_state_string(ovs_be32 value
, ovs_be32 mask
, struct ds
*s
)
2966 format_flags_masked(s
, NULL
, ct_state_to_string
, ntohl(value
),
2967 ntohl(mask
), UINT16_MAX
);
2971 mf_format_packet_type_string(ovs_be32 value
, ovs_be32 mask
, struct ds
*s
)
2973 format_packet_type_masked(s
, value
, mask
);
2976 /* Appends to 's' a string representation of field 'mf' whose value is in
2977 * 'value' and 'mask'. 'mask' may be NULL to indicate an exact match. */
2979 mf_format(const struct mf_field
*mf
,
2980 const union mf_value
*value
, const union mf_value
*mask
,
2981 const struct ofputil_port_map
*port_map
,
2985 if (is_all_zeros(mask
, mf
->n_bytes
)) {
2986 ds_put_cstr(s
, "ANY");
2988 } else if (is_all_ones(mask
, mf
->n_bytes
)) {
2993 switch (mf
->string
) {
2994 case MFS_OFP_PORT_OXM
:
2997 ofputil_port_from_ofp11(value
->be32
, &port
);
2998 ofputil_format_port(port
, port_map
, s
);
3004 ofputil_format_port(u16_to_ofp(ntohs(value
->be16
)), port_map
, s
);
3009 case MFS_HEXADECIMAL
:
3010 mf_format_integer_string(mf
, (uint8_t *) value
, (uint8_t *) mask
, s
);
3014 mf_format_ct_state_string(value
->be32
,
3015 mask
? mask
->be32
: OVS_BE32_MAX
, s
);
3019 eth_format_masked(value
->mac
, mask
? &mask
->mac
: NULL
, s
);
3023 ip_format_masked(value
->be32
, mask
? mask
->be32
: OVS_BE32_MAX
, s
);
3027 ipv6_format_masked(&value
->ipv6
, mask
? &mask
->ipv6
: NULL
, s
);
3031 mf_format_frag_string(value
->u8
, mask
? mask
->u8
: UINT8_MAX
, s
);
3035 mf_format_tnl_flags_string(value
->be16
,
3036 mask
? mask
->be16
: OVS_BE16_MAX
, s
);
3040 mf_format_tcp_flags_string(value
->be16
,
3041 mask
? mask
->be16
: OVS_BE16_MAX
, s
);
3044 case MFS_PACKET_TYPE
:
3045 mf_format_packet_type_string(value
->be32
,
3046 mask
? mask
->be32
: OVS_BE32_MAX
, s
);
3054 /* Makes subfield 'sf' within 'flow' exactly match the 'sf->n_bits'
3055 * least-significant bits in 'x'.
3058 mf_write_subfield_flow(const struct mf_subfield
*sf
,
3059 const union mf_subvalue
*x
, struct flow
*flow
)
3061 const struct mf_field
*field
= sf
->field
;
3062 union mf_value value
;
3064 mf_get_value(field
, flow
, &value
);
3065 bitwise_copy(x
, sizeof *x
, 0, &value
, field
->n_bytes
,
3066 sf
->ofs
, sf
->n_bits
);
3067 mf_set_flow_value(field
, &value
, flow
);
3070 /* Makes subfield 'sf' within 'match' exactly match the 'sf->n_bits'
3071 * least-significant bits in 'x'.
3074 mf_write_subfield(const struct mf_subfield
*sf
, const union mf_subvalue
*x
,
3075 struct match
*match
)
3077 const struct mf_field
*field
= sf
->field
;
3078 union mf_value value
, mask
;
3080 mf_get(field
, match
, &value
, &mask
);
3081 bitwise_copy(x
, sizeof *x
, 0, &value
, field
->n_bytes
, sf
->ofs
, sf
->n_bits
);
3082 bitwise_one ( &mask
, field
->n_bytes
, sf
->ofs
, sf
->n_bits
);
3083 mf_set(field
, &value
, &mask
, match
, NULL
);
3087 mf_write_subfield_value(const struct mf_subfield
*sf
, const void *src
,
3088 struct match
*match
)
3090 const struct mf_field
*field
= sf
->field
;
3091 union mf_value value
, mask
;
3092 unsigned int size
= DIV_ROUND_UP(sf
->n_bits
, 8);
3094 mf_get(field
, match
, &value
, &mask
);
3095 bitwise_copy(src
, size
, 0, &value
, field
->n_bytes
, sf
->ofs
, sf
->n_bits
);
3096 bitwise_one ( &mask
, field
->n_bytes
, sf
->ofs
, sf
->n_bits
);
3097 mf_set(field
, &value
, &mask
, match
, NULL
);
3100 /* 'v' and 'm' correspond to values of 'field'. This function copies them into
3101 * 'match' in the correspond positions. */
3103 mf_mask_subfield(const struct mf_field
*field
,
3104 const union mf_subvalue
*v
,
3105 const union mf_subvalue
*m
,
3106 struct match
*match
)
3108 union mf_value value
, mask
;
3110 mf_get(field
, match
, &value
, &mask
);
3111 bitwise_copy(v
, sizeof *v
, 0, &value
, field
->n_bytes
, 0, field
->n_bits
);
3112 bitwise_copy(m
, sizeof *m
, 0, &mask
, field
->n_bytes
, 0, field
->n_bits
);
3113 mf_set(field
, &value
, &mask
, match
, NULL
);
3116 /* Initializes 'x' to the value of 'sf' within 'flow'. 'sf' must be valid for
3117 * reading 'flow', e.g. as checked by mf_check_src(). */
3119 mf_read_subfield(const struct mf_subfield
*sf
, const struct flow
*flow
,
3120 union mf_subvalue
*x
)
3122 union mf_value value
;
3124 mf_get_value(sf
->field
, flow
, &value
);
3126 memset(x
, 0, sizeof *x
);
3127 bitwise_copy(&value
, sf
->field
->n_bytes
, sf
->ofs
,
3132 /* Returns the value of 'sf' within 'flow'. 'sf' must be valid for reading
3133 * 'flow', e.g. as checked by mf_check_src() and sf->n_bits must be 64 or
3136 mf_get_subfield(const struct mf_subfield
*sf
, const struct flow
*flow
)
3138 union mf_value value
;
3140 mf_get_value(sf
->field
, flow
, &value
);
3141 return bitwise_get(&value
, sf
->field
->n_bytes
, sf
->ofs
, sf
->n_bits
);
3145 mf_format_subvalue(const union mf_subvalue
*subvalue
, struct ds
*s
)
3147 ds_put_hex(s
, subvalue
->u8
, sizeof subvalue
->u8
);
3151 field_array_set(enum mf_field_id id
, const union mf_value
*value
,
3152 struct field_array
*fa
)
3154 size_t i
, offset
= 0;
3156 ovs_assert(id
< MFF_N_IDS
);
3158 /* Find the spot for 'id'. */
3159 BITMAP_FOR_EACH_1 (i
, id
, fa
->used
.bm
) {
3160 offset
+= mf_from_id(i
)->n_bytes
;
3163 size_t value_size
= mf_from_id(id
)->n_bytes
;
3165 /* make room if necessary. */
3166 if (!bitmap_is_set(fa
->used
.bm
, id
)) {
3167 fa
->values
= xrealloc(fa
->values
, fa
->values_size
+ value_size
);
3168 /* Move remainder forward, if any. */
3169 if (offset
< fa
->values_size
) {
3170 memmove(fa
->values
+ offset
+ value_size
, fa
->values
+ offset
,
3171 fa
->values_size
- offset
);
3173 fa
->values_size
+= value_size
;
3175 bitmap_set1(fa
->used
.bm
, id
);
3177 memcpy(fa
->values
+ offset
, value
, value_size
);
3180 /* A wrapper for variable length mf_fields that is maintained by
3181 * struct vl_mff_map.*/
3182 struct vl_mf_field
{
3184 struct ovs_refcount ref_cnt
;
3185 struct cmap_node cmap_node
; /* In ofproto->vl_mff_map->cmap. */
3188 static inline uint32_t
3189 mf_field_hash(uint32_t key
)
3191 return hash_int(key
, 0);
3195 vmf_delete(struct vl_mf_field
*vmf
)
3197 if (ovs_refcount_unref(&vmf
->ref_cnt
) == 1) {
3198 /* Postpone as this function is typically called immediately
3199 * after removing from cmap. */
3200 ovsrcu_postpone(free
, vmf
);
3203 "Attempted to delete VMF %s but refcount is nonzero!",
3209 mf_vl_mff_map_clear(struct vl_mff_map
*vl_mff_map
, bool force
)
3210 OVS_REQUIRES(vl_mff_map
->mutex
)
3212 struct vl_mf_field
*vmf
;
3215 CMAP_FOR_EACH (vmf
, cmap_node
, &vl_mff_map
->cmap
) {
3216 if (ovs_refcount_read(&vmf
->ref_cnt
) != 1) {
3217 return OFPERR_NXTTMFC_INVALID_TLV_DEL
;
3222 CMAP_FOR_EACH (vmf
, cmap_node
, &vl_mff_map
->cmap
) {
3223 cmap_remove(&vl_mff_map
->cmap
, &vmf
->cmap_node
,
3224 mf_field_hash(vmf
->mf
.id
));
3231 static struct vl_mf_field
*
3232 mf_get_vl_mff__(uint32_t id
, const struct vl_mff_map
*vl_mff_map
)
3234 struct vl_mf_field
*vmf
;
3236 CMAP_FOR_EACH_WITH_HASH (vmf
, cmap_node
, mf_field_hash(id
),
3237 &vl_mff_map
->cmap
) {
3238 if (vmf
->mf
.id
== id
) {
3246 /* If 'mff' is a variable length field, looks up 'vl_mff_map', returns a
3247 * pointer to the variable length meta-flow field corresponding to 'mff'.
3248 * Returns NULL if no mapping is existed for 'mff'. */
3249 const struct mf_field
*
3250 mf_get_vl_mff(const struct mf_field
*mff
,
3251 const struct vl_mff_map
*vl_mff_map
)
3253 if (mff
&& mff
->variable_len
&& vl_mff_map
) {
3254 return &mf_get_vl_mff__(mff
->id
, vl_mff_map
)->mf
;
3261 mf_vl_mff_map_del(struct vl_mff_map
*vl_mff_map
,
3262 const struct ofputil_tlv_table_mod
*ttm
, bool force
)
3263 OVS_REQUIRES(vl_mff_map
->mutex
)
3265 struct ofputil_tlv_map
*tlv_map
;
3266 struct vl_mf_field
*vmf
;
3270 LIST_FOR_EACH (tlv_map
, list_node
, &ttm
->mappings
) {
3271 idx
= MFF_TUN_METADATA0
+ tlv_map
->index
;
3272 if (idx
>= MFF_TUN_METADATA0
+ TUN_METADATA_NUM_OPTS
) {
3273 return OFPERR_NXTTMFC_BAD_FIELD_IDX
;
3276 vmf
= mf_get_vl_mff__(idx
, vl_mff_map
);
3277 if (vmf
&& ovs_refcount_read(&vmf
->ref_cnt
) != 1) {
3278 return OFPERR_NXTTMFC_INVALID_TLV_DEL
;
3283 LIST_FOR_EACH (tlv_map
, list_node
, &ttm
->mappings
) {
3284 idx
= MFF_TUN_METADATA0
+ tlv_map
->index
;
3285 if (idx
>= MFF_TUN_METADATA0
+ TUN_METADATA_NUM_OPTS
) {
3286 return OFPERR_NXTTMFC_BAD_FIELD_IDX
;
3289 vmf
= mf_get_vl_mff__(idx
, vl_mff_map
);
3291 cmap_remove(&vl_mff_map
->cmap
, &vmf
->cmap_node
,
3292 mf_field_hash(idx
));
3301 mf_vl_mff_map_add(struct vl_mff_map
*vl_mff_map
,
3302 const struct ofputil_tlv_table_mod
*ttm
)
3303 OVS_REQUIRES(vl_mff_map
->mutex
)
3305 struct ofputil_tlv_map
*tlv_map
;
3306 struct vl_mf_field
*vmf
;
3309 LIST_FOR_EACH (tlv_map
, list_node
, &ttm
->mappings
) {
3310 idx
= MFF_TUN_METADATA0
+ tlv_map
->index
;
3311 if (idx
>= MFF_TUN_METADATA0
+ TUN_METADATA_NUM_OPTS
) {
3312 return OFPERR_NXTTMFC_BAD_FIELD_IDX
;
3315 vmf
= xmalloc(sizeof *vmf
);
3316 vmf
->mf
= mf_fields
[idx
];
3317 vmf
->mf
.n_bytes
= tlv_map
->option_len
;
3318 vmf
->mf
.n_bits
= tlv_map
->option_len
* 8;
3319 vmf
->mf
.mapped
= true;
3320 ovs_refcount_init(&vmf
->ref_cnt
);
3322 cmap_insert(&vl_mff_map
->cmap
, &vmf
->cmap_node
,
3323 mf_field_hash(idx
));
3329 /* Updates the tun_metadata mf_field in 'vl_mff_map' according to 'ttm'.
3330 * This function must be invoked after tun_metadata_table_mod().
3331 * Returns OFPERR_NXTTMFC_BAD_FIELD_IDX, if the index for the vl_mf_field is
3333 * Returns OFPERR_NXTTMFC_INVALID_TLV_DEL, if 'ttm' tries to delete an
3334 * vl_mf_field that is still used by any active flow.*/
3336 mf_vl_mff_map_mod_from_tun_metadata(struct vl_mff_map
*vl_mff_map
,
3337 const struct ofputil_tlv_table_mod
*ttm
)
3338 OVS_REQUIRES(vl_mff_map
->mutex
)
3340 switch (ttm
->command
) {
3342 return mf_vl_mff_map_add(vl_mff_map
, ttm
);
3345 return mf_vl_mff_map_del(vl_mff_map
, ttm
, false);
3348 return mf_vl_mff_map_clear(vl_mff_map
, false);
3357 /* Returns true if a variable length meta-flow field 'mff' is not mapped in
3358 * the 'vl_mff_map'. */
3360 mf_vl_mff_invalid(const struct mf_field
*mff
, const struct vl_mff_map
*map
)
3362 return map
&& mff
&& mff
->variable_len
&& !mff
->mapped
;
3366 mf_vl_mff_set_tlv_bitmap(const struct mf_field
*mff
, uint64_t *tlv_bitmap
)
3368 if (mff
&& mff
->mapped
) {
3369 ovs_assert(mf_is_tun_metadata(mff
));
3370 ULLONG_SET1(*tlv_bitmap
, mff
->id
- MFF_TUN_METADATA0
);
3375 mf_vl_mff_ref_cnt_mod(const struct vl_mff_map
*map
, uint64_t tlv_bitmap
,
3378 struct vl_mf_field
*vmf
;
3382 ULLONG_FOR_EACH_1 (i
, tlv_bitmap
) {
3383 vmf
= mf_get_vl_mff__(i
+ MFF_TUN_METADATA0
, map
);
3386 ovs_refcount_ref(&vmf
->ref_cnt
);
3388 ovs_refcount_unref(&vmf
->ref_cnt
);
3391 VLOG_WARN("Invalid TLV index %d.", i
);
3398 mf_vl_mff_ref(const struct vl_mff_map
*map
, uint64_t tlv_bitmap
)
3400 mf_vl_mff_ref_cnt_mod(map
, tlv_bitmap
, true);
3404 mf_vl_mff_unref(const struct vl_mff_map
*map
, uint64_t tlv_bitmap
)
3406 mf_vl_mff_ref_cnt_mod(map
, tlv_bitmap
, false);
3410 mf_vl_mff_nx_pull_header(struct ofpbuf
*b
, const struct vl_mff_map
*vl_mff_map
,
3411 const struct mf_field
**field
, bool *masked
,
3412 uint64_t *tlv_bitmap
)
3414 enum ofperr error
= nx_pull_header(b
, vl_mff_map
, field
, masked
);
3419 mf_vl_mff_set_tlv_bitmap(*field
, tlv_bitmap
);
3424 mf_vl_mff_nx_pull_entry(struct ofpbuf
*b
, const struct vl_mff_map
*vl_mff_map
,
3425 const struct mf_field
**field
, union mf_value
*value
,
3426 union mf_value
*mask
, uint64_t *tlv_bitmap
)
3428 enum ofperr error
= nx_pull_entry(b
, vl_mff_map
, field
, value
, mask
);
3433 mf_vl_mff_set_tlv_bitmap(*field
, tlv_bitmap
);
3438 mf_vl_mff_mf_from_nxm_header(uint32_t header
,
3439 const struct vl_mff_map
*vl_mff_map
,
3440 const struct mf_field
**field
,
3441 uint64_t *tlv_bitmap
)
3443 *field
= mf_from_nxm_header(header
, vl_mff_map
);
3444 if (mf_vl_mff_invalid(*field
, vl_mff_map
)) {
3445 return OFPERR_NXFMFC_INVALID_TLV_FIELD
;
3448 mf_vl_mff_set_tlv_bitmap(*field
, tlv_bitmap
);