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
;
208 case MFF_PACKET_TYPE
:
209 return !wc
->masks
.packet_type
;
211 return !wc
->masks
.conj_id
;
213 return !wc
->masks
.tunnel
.ip_src
;
215 return !wc
->masks
.tunnel
.ip_dst
;
216 case MFF_TUN_IPV6_SRC
:
217 return ipv6_mask_is_any(&wc
->masks
.tunnel
.ipv6_src
);
218 case MFF_TUN_IPV6_DST
:
219 return ipv6_mask_is_any(&wc
->masks
.tunnel
.ipv6_dst
);
221 return !wc
->masks
.tunnel
.tun_id
;
223 return !wc
->masks
.tunnel
.ip_tos
;
225 return !wc
->masks
.tunnel
.ip_ttl
;
227 return !(wc
->masks
.tunnel
.flags
& FLOW_TNL_PUB_F_MASK
);
229 return !wc
->masks
.tunnel
.gbp_id
;
230 case MFF_TUN_GBP_FLAGS
:
231 return !wc
->masks
.tunnel
.gbp_flags
;
232 CASE_MFF_TUN_METADATA
:
233 return !ULLONG_GET(wc
->masks
.tunnel
.metadata
.present
.map
,
234 mf
->id
- MFF_TUN_METADATA0
);
236 return !wc
->masks
.metadata
;
238 case MFF_IN_PORT_OXM
:
239 return !wc
->masks
.in_port
.ofp_port
;
240 case MFF_SKB_PRIORITY
:
241 return !wc
->masks
.skb_priority
;
243 return !wc
->masks
.pkt_mark
;
245 return !wc
->masks
.ct_state
;
247 return !wc
->masks
.ct_zone
;
249 return !wc
->masks
.ct_mark
;
251 return ovs_u128_is_zero(wc
->masks
.ct_label
);
252 case MFF_CT_NW_PROTO
:
253 return !wc
->masks
.ct_nw_proto
;
255 return !wc
->masks
.ct_nw_src
;
257 return !wc
->masks
.ct_nw_dst
;
259 return !wc
->masks
.ct_tp_src
;
261 return !wc
->masks
.ct_tp_dst
;
262 case MFF_CT_IPV6_SRC
:
263 return ipv6_mask_is_any(&wc
->masks
.ct_ipv6_src
);
264 case MFF_CT_IPV6_DST
:
265 return ipv6_mask_is_any(&wc
->masks
.ct_ipv6_dst
);
267 return !wc
->masks
.regs
[mf
->id
- MFF_REG0
];
269 return !flow_get_xreg(&wc
->masks
, mf
->id
- MFF_XREG0
);
271 ovs_u128 value
= flow_get_xxreg(&wc
->masks
, mf
->id
- MFF_XXREG0
);
272 return ovs_u128_is_zero(value
);
274 case MFF_ACTSET_OUTPUT
:
275 return !wc
->masks
.actset_output
;
278 return eth_addr_is_zero(wc
->masks
.dl_src
);
280 return eth_addr_is_zero(wc
->masks
.dl_dst
);
282 return !wc
->masks
.dl_type
;
286 return eth_addr_is_zero(wc
->masks
.arp_sha
);
290 return eth_addr_is_zero(wc
->masks
.arp_tha
);
293 return !wc
->masks
.vlans
[0].tci
;
295 return !(wc
->masks
.vlans
[0].tci
& htons(VLAN_VID_MASK
));
297 return !(wc
->masks
.vlans
[0].tci
& htons(VLAN_VID_MASK
| VLAN_CFI
));
298 case MFF_DL_VLAN_PCP
:
300 return !(wc
->masks
.vlans
[0].tci
& htons(VLAN_PCP_MASK
));
303 return !(wc
->masks
.mpls_lse
[0] & htonl(MPLS_LABEL_MASK
));
305 return !(wc
->masks
.mpls_lse
[0] & htonl(MPLS_TC_MASK
));
307 return !(wc
->masks
.mpls_lse
[0] & htonl(MPLS_BOS_MASK
));
309 return !(wc
->masks
.mpls_lse
[0] & htonl(MPLS_TTL_MASK
));
312 return !wc
->masks
.nw_src
;
314 return !wc
->masks
.nw_dst
;
317 return ipv6_mask_is_any(&wc
->masks
.ipv6_src
);
319 return ipv6_mask_is_any(&wc
->masks
.ipv6_dst
);
322 return !wc
->masks
.ipv6_label
;
325 return !wc
->masks
.nw_proto
;
327 case MFF_IP_DSCP_SHIFTED
:
328 return !(wc
->masks
.nw_tos
& IP_DSCP_MASK
);
330 return !(wc
->masks
.nw_tos
& IP_ECN_MASK
);
332 return !wc
->masks
.nw_ttl
;
335 return ipv6_mask_is_any(&wc
->masks
.nd_target
);
338 return !(wc
->masks
.nw_frag
& FLOW_NW_FRAG_MASK
);
341 return !wc
->masks
.nw_proto
;
343 return !wc
->masks
.nw_src
;
345 return !wc
->masks
.nw_dst
;
350 case MFF_ICMPV4_TYPE
:
351 case MFF_ICMPV6_TYPE
:
352 return !wc
->masks
.tp_src
;
356 case MFF_ICMPV4_CODE
:
357 case MFF_ICMPV6_CODE
:
358 return !wc
->masks
.tp_dst
;
360 return !wc
->masks
.tcp_flags
;
363 return !wc
->masks
.nsh
.flags
;
365 return !wc
->masks
.nsh
.mdtype
;
367 return !wc
->masks
.nsh
.np
;
369 return !wc
->masks
.nsh
.spi
;
371 return !wc
->masks
.nsh
.si
;
376 return !wc
->masks
.nsh
.c
[mf
->id
- MFF_NSH_C1
];
384 /* Initializes 'mask' with the wildcard bit pattern for field 'mf' within 'wc'.
385 * Each bit in 'mask' will be set to 1 if the bit is significant for matching
386 * purposes, or to 0 if it is wildcarded.
388 * The caller is responsible for ensuring that 'wc' corresponds to a flow that
389 * meets 'mf''s prerequisites. */
391 mf_get_mask(const struct mf_field
*mf
, const struct flow_wildcards
*wc
,
392 union mf_value
*mask
)
394 mf_get_value(mf
, &wc
->masks
, mask
);
397 /* Tests whether 'mask' is a valid wildcard bit pattern for 'mf'. Returns true
398 * if the mask is valid, false otherwise. */
400 mf_is_mask_valid(const struct mf_field
*mf
, const union mf_value
*mask
)
402 switch (mf
->maskable
) {
404 return (is_all_zeros(mask
, mf
->n_bytes
) ||
405 is_all_ones(mask
, mf
->n_bytes
));
414 /* Returns true if 'flow' meets the prerequisites for 'mf', false otherwise.
415 * If a non-NULL 'mask' is passed, zero-valued matches can also be verified.
416 * Sets inspected bits in 'wc', if non-NULL. */
418 mf_are_prereqs_ok__(const struct mf_field
*mf
, const struct flow
*flow
,
419 const struct flow_wildcards
*mask
,
420 struct flow_wildcards
*wc
)
422 ovs_be16 dl_type
= get_dl_type(flow
);
424 switch (mf
->prereqs
) {
428 return is_ethernet(flow
, wc
);
430 return (dl_type
== htons(ETH_TYPE_ARP
) ||
431 dl_type
== htons(ETH_TYPE_RARP
));
433 return dl_type
== htons(ETH_TYPE_IP
);
435 return dl_type
== htons(ETH_TYPE_IPV6
);
437 return is_vlan(flow
, wc
);
439 return eth_type_mpls(dl_type
);
441 return is_ip_any(flow
);
443 return dl_type
== htons(ETH_TYPE_NSH
);
445 return is_ct_valid(flow
, mask
, wc
);
447 /* Matching !FRAG_LATER is not enforced (mask is not checked). */
448 return is_tcp(flow
, wc
) && !(flow
->nw_frag
& FLOW_NW_FRAG_LATER
);
450 return is_udp(flow
, wc
) && !(flow
->nw_frag
& FLOW_NW_FRAG_LATER
);
452 return is_sctp(flow
, wc
) && !(flow
->nw_frag
& FLOW_NW_FRAG_LATER
);
454 return is_icmpv4(flow
, wc
);
456 return is_icmpv6(flow
, wc
);
458 return is_nd(flow
, wc
);
460 return is_nd(flow
, wc
) && flow
->tp_src
== htons(ND_NEIGHBOR_SOLICIT
);
462 return is_nd(flow
, wc
) && flow
->tp_src
== htons(ND_NEIGHBOR_ADVERT
);
468 /* Returns true if 'flow' meets the prerequisites for 'mf', false otherwise.
469 * Sets inspected bits in 'wc', if non-NULL. */
471 mf_are_prereqs_ok(const struct mf_field
*mf
, const struct flow
*flow
,
472 struct flow_wildcards
*wc
)
474 return mf_are_prereqs_ok__(mf
, flow
, NULL
, wc
);
477 /* Returns true if 'match' meets the prerequisites for 'mf', false otherwise.
480 mf_are_match_prereqs_ok(const struct mf_field
*mf
, const struct match
*match
)
482 return mf_are_prereqs_ok__(mf
, &match
->flow
, &match
->wc
, NULL
);
485 /* Returns true if 'value' may be a valid value *as part of a masked match*,
488 * A value is not rejected just because it is not valid for the field in
489 * question, but only if it doesn't make sense to test the bits in question at
490 * all. For example, the MFF_VLAN_TCI field will never have a nonzero value
491 * without the VLAN_CFI bit being set, but we can't reject those values because
492 * it is still legitimate to test just for those bits (see the documentation
493 * for NXM_OF_VLAN_TCI in meta-flow.h). On the other hand, there is never a
494 * reason to set the low bit of MFF_IP_DSCP to 1, so we reject that. */
496 mf_is_value_valid(const struct mf_field
*mf
, const union mf_value
*value
)
501 case MFF_PACKET_TYPE
:
506 case MFF_TUN_IPV6_SRC
:
507 case MFF_TUN_IPV6_DST
:
511 case MFF_TUN_GBP_FLAGS
:
512 CASE_MFF_TUN_METADATA
:
515 case MFF_SKB_PRIORITY
:
520 case MFF_CT_NW_PROTO
:
523 case MFF_CT_IPV6_SRC
:
524 case MFF_CT_IPV6_DST
:
551 case MFF_ICMPV4_TYPE
:
552 case MFF_ICMPV4_CODE
:
553 case MFF_ICMPV6_TYPE
:
554 case MFF_ICMPV6_CODE
:
560 case MFF_IN_PORT_OXM
:
561 case MFF_ACTSET_OUTPUT
: {
563 return !ofputil_port_from_ofp11(value
->be32
, &port
);
567 return !(value
->u8
& ~IP_DSCP_MASK
);
568 case MFF_IP_DSCP_SHIFTED
:
569 return !(value
->u8
& (~IP_DSCP_MASK
>> 2));
571 return !(value
->u8
& ~IP_ECN_MASK
);
573 return !(value
->u8
& ~FLOW_NW_FRAG_MASK
);
575 return !(value
->be16
& ~htons(0x0fff));
578 return !(value
->be16
& htons(0xff00));
581 return !(value
->be16
& htons(VLAN_CFI
| VLAN_PCP_MASK
));
583 return !(value
->be16
& htons(VLAN_PCP_MASK
));
585 case MFF_DL_VLAN_PCP
:
587 return !(value
->u8
& ~(VLAN_PCP_MASK
>> VLAN_PCP_SHIFT
));
590 return !(value
->be32
& ~htonl(IPV6_LABEL_MASK
));
593 return !(value
->be32
& ~htonl(MPLS_LABEL_MASK
>> MPLS_LABEL_SHIFT
));
596 return !(value
->u8
& ~(MPLS_TC_MASK
>> MPLS_TC_SHIFT
));
599 return !(value
->u8
& ~(MPLS_BOS_MASK
>> MPLS_BOS_SHIFT
));
602 return !(value
->be16
& ~htons(FLOW_TNL_PUB_F_MASK
));
605 return !(value
->be32
& ~htonl(CS_SUPPORTED_MASK
));
610 return (value
->u8
== 1 || value
->u8
== 2);
614 return !(value
->be32
& htonl(0xFF000000));
628 /* Copies the value of field 'mf' from 'flow' into 'value'. The caller is
629 * responsible for ensuring that 'flow' meets 'mf''s prerequisites. */
631 mf_get_value(const struct mf_field
*mf
, const struct flow
*flow
,
632 union mf_value
*value
)
636 value
->be32
= htonl(flow
->dp_hash
);
639 value
->be32
= htonl(flow
->recirc_id
);
641 case MFF_PACKET_TYPE
:
642 value
->be32
= flow
->packet_type
;
645 value
->be32
= htonl(flow
->conj_id
);
648 value
->be64
= flow
->tunnel
.tun_id
;
651 value
->be32
= flow
->tunnel
.ip_src
;
654 value
->be32
= flow
->tunnel
.ip_dst
;
656 case MFF_TUN_IPV6_SRC
:
657 value
->ipv6
= flow
->tunnel
.ipv6_src
;
659 case MFF_TUN_IPV6_DST
:
660 value
->ipv6
= flow
->tunnel
.ipv6_dst
;
663 value
->be16
= htons(flow
->tunnel
.flags
& FLOW_TNL_PUB_F_MASK
);
666 value
->be16
= flow
->tunnel
.gbp_id
;
668 case MFF_TUN_GBP_FLAGS
:
669 value
->u8
= flow
->tunnel
.gbp_flags
;
672 value
->u8
= flow
->tunnel
.ip_ttl
;
675 value
->u8
= flow
->tunnel
.ip_tos
;
677 CASE_MFF_TUN_METADATA
:
678 tun_metadata_read(&flow
->tunnel
, mf
, value
);
682 value
->be64
= flow
->metadata
;
686 value
->be16
= htons(ofp_to_u16(flow
->in_port
.ofp_port
));
688 case MFF_IN_PORT_OXM
:
689 value
->be32
= ofputil_port_to_ofp11(flow
->in_port
.ofp_port
);
691 case MFF_ACTSET_OUTPUT
:
692 value
->be32
= ofputil_port_to_ofp11(flow
->actset_output
);
695 case MFF_SKB_PRIORITY
:
696 value
->be32
= htonl(flow
->skb_priority
);
700 value
->be32
= htonl(flow
->pkt_mark
);
704 value
->be32
= htonl(flow
->ct_state
);
708 value
->be16
= htons(flow
->ct_zone
);
712 value
->be32
= htonl(flow
->ct_mark
);
716 value
->be128
= hton128(flow
->ct_label
);
719 case MFF_CT_NW_PROTO
:
720 value
->u8
= flow
->ct_nw_proto
;
724 value
->be32
= flow
->ct_nw_src
;
728 value
->be32
= flow
->ct_nw_dst
;
731 case MFF_CT_IPV6_SRC
:
732 value
->ipv6
= flow
->ct_ipv6_src
;
735 case MFF_CT_IPV6_DST
:
736 value
->ipv6
= flow
->ct_ipv6_dst
;
740 value
->be16
= flow
->ct_tp_src
;
744 value
->be16
= flow
->ct_tp_dst
;
748 value
->be32
= htonl(flow
->regs
[mf
->id
- MFF_REG0
]);
752 value
->be64
= htonll(flow_get_xreg(flow
, mf
->id
- MFF_XREG0
));
756 value
->be128
= hton128(flow_get_xxreg(flow
, mf
->id
- MFF_XXREG0
));
760 value
->mac
= flow
->dl_src
;
764 value
->mac
= flow
->dl_dst
;
768 value
->be16
= flow
->dl_type
;
772 value
->be16
= flow
->vlans
[0].tci
;
776 value
->be16
= flow
->vlans
[0].tci
& htons(VLAN_VID_MASK
);
779 value
->be16
= flow
->vlans
[0].tci
& htons(VLAN_VID_MASK
| VLAN_CFI
);
782 case MFF_DL_VLAN_PCP
:
784 value
->u8
= vlan_tci_to_pcp(flow
->vlans
[0].tci
);
788 value
->be32
= htonl(mpls_lse_to_label(flow
->mpls_lse
[0]));
792 value
->u8
= mpls_lse_to_tc(flow
->mpls_lse
[0]);
796 value
->u8
= mpls_lse_to_bos(flow
->mpls_lse
[0]);
800 value
->u8
= mpls_lse_to_ttl(flow
->mpls_lse
[0]);
804 value
->be32
= flow
->nw_src
;
808 value
->be32
= flow
->nw_dst
;
812 value
->ipv6
= flow
->ipv6_src
;
816 value
->ipv6
= flow
->ipv6_dst
;
820 value
->be32
= flow
->ipv6_label
;
824 value
->u8
= flow
->nw_proto
;
828 value
->u8
= flow
->nw_tos
& IP_DSCP_MASK
;
831 case MFF_IP_DSCP_SHIFTED
:
832 value
->u8
= flow
->nw_tos
>> 2;
836 value
->u8
= flow
->nw_tos
& IP_ECN_MASK
;
840 value
->u8
= flow
->nw_ttl
;
844 value
->u8
= flow
->nw_frag
;
848 value
->be16
= htons(flow
->nw_proto
);
852 value
->be32
= flow
->nw_src
;
856 value
->be32
= flow
->nw_dst
;
861 value
->mac
= flow
->arp_sha
;
866 value
->mac
= flow
->arp_tha
;
872 value
->be16
= flow
->tp_src
;
878 value
->be16
= flow
->tp_dst
;
882 value
->be16
= flow
->tcp_flags
;
885 case MFF_ICMPV4_TYPE
:
886 case MFF_ICMPV6_TYPE
:
887 value
->u8
= ntohs(flow
->tp_src
);
890 case MFF_ICMPV4_CODE
:
891 case MFF_ICMPV6_CODE
:
892 value
->u8
= ntohs(flow
->tp_dst
);
896 value
->ipv6
= flow
->nd_target
;
900 value
->u8
= flow
->nsh
.flags
;
903 value
->u8
= flow
->nsh
.mdtype
;
906 value
->u8
= flow
->nsh
.np
;
909 value
->be32
= flow
->nsh
.spi
;
912 value
->u8
= flow
->nsh
.si
;
918 value
->be32
= flow
->nsh
.c
[mf
->id
- MFF_NSH_C1
];
927 /* Makes 'match' match field 'mf' exactly, with the value matched taken from
928 * 'value'. The caller is responsible for ensuring that 'match' meets 'mf''s
931 * If non-NULL, 'err_str' returns a malloc'ed string describing any errors
932 * with the request or NULL if there is no error. The caller is reponsible
933 * for freeing the string. */
935 mf_set_value(const struct mf_field
*mf
,
936 const union mf_value
*value
, struct match
*match
, char **err_str
)
944 match_set_dp_hash(match
, ntohl(value
->be32
));
947 match_set_recirc_id(match
, ntohl(value
->be32
));
949 case MFF_PACKET_TYPE
:
950 match_set_packet_type(match
, value
->be32
);
953 match_set_conj_id(match
, ntohl(value
->be32
));
956 match_set_tun_id(match
, value
->be64
);
959 match_set_tun_src(match
, value
->be32
);
962 match_set_tun_dst(match
, value
->be32
);
964 case MFF_TUN_IPV6_SRC
:
965 match_set_tun_ipv6_src(match
, &value
->ipv6
);
967 case MFF_TUN_IPV6_DST
:
968 match_set_tun_ipv6_dst(match
, &value
->ipv6
);
971 match_set_tun_flags(match
, ntohs(value
->be16
));
974 match_set_tun_gbp_id(match
, value
->be16
);
976 case MFF_TUN_GBP_FLAGS
:
977 match_set_tun_gbp_flags(match
, value
->u8
);
980 match_set_tun_tos(match
, value
->u8
);
983 match_set_tun_ttl(match
, value
->u8
);
985 CASE_MFF_TUN_METADATA
:
986 tun_metadata_set_match(mf
, value
, NULL
, match
, err_str
);
990 match_set_metadata(match
, value
->be64
);
994 match_set_in_port(match
, u16_to_ofp(ntohs(value
->be16
)));
997 case MFF_IN_PORT_OXM
: {
999 ofputil_port_from_ofp11(value
->be32
, &port
);
1000 match_set_in_port(match
, port
);
1003 case MFF_ACTSET_OUTPUT
: {
1005 ofputil_port_from_ofp11(value
->be32
, &port
);
1006 match_set_actset_output(match
, port
);
1010 case MFF_SKB_PRIORITY
:
1011 match_set_skb_priority(match
, ntohl(value
->be32
));
1015 match_set_pkt_mark(match
, ntohl(value
->be32
));
1019 match_set_ct_state(match
, ntohl(value
->be32
));
1023 match_set_ct_zone(match
, ntohs(value
->be16
));
1027 match_set_ct_mark(match
, ntohl(value
->be32
));
1031 match_set_ct_label(match
, ntoh128(value
->be128
));
1034 case MFF_CT_NW_PROTO
:
1035 match_set_ct_nw_proto(match
, value
->u8
);
1039 match_set_ct_nw_src(match
, value
->be32
);
1043 match_set_ct_nw_dst(match
, value
->be32
);
1046 case MFF_CT_IPV6_SRC
:
1047 match_set_ct_ipv6_src(match
, &value
->ipv6
);
1050 case MFF_CT_IPV6_DST
:
1051 match_set_ct_ipv6_dst(match
, &value
->ipv6
);
1055 match_set_ct_tp_src(match
, value
->be16
);
1059 match_set_ct_tp_dst(match
, value
->be16
);
1063 match_set_reg(match
, mf
->id
- MFF_REG0
, ntohl(value
->be32
));
1067 match_set_xreg(match
, mf
->id
- MFF_XREG0
, ntohll(value
->be64
));
1071 match_set_xxreg(match
, mf
->id
- MFF_XXREG0
, ntoh128(value
->be128
));
1075 match_set_dl_src(match
, value
->mac
);
1079 match_set_dl_dst(match
, value
->mac
);
1083 match_set_dl_type(match
, value
->be16
);
1087 match_set_dl_tci(match
, value
->be16
);
1091 match_set_dl_vlan(match
, value
->be16
);
1094 match_set_vlan_vid(match
, value
->be16
);
1097 case MFF_DL_VLAN_PCP
:
1099 match_set_dl_vlan_pcp(match
, value
->u8
);
1102 case MFF_MPLS_LABEL
:
1103 match_set_mpls_label(match
, 0, value
->be32
);
1107 match_set_mpls_tc(match
, 0, value
->u8
);
1111 match_set_mpls_bos(match
, 0, value
->u8
);
1115 match_set_mpls_ttl(match
, 0, value
->u8
);
1119 match_set_nw_src(match
, value
->be32
);
1123 match_set_nw_dst(match
, value
->be32
);
1127 match_set_ipv6_src(match
, &value
->ipv6
);
1131 match_set_ipv6_dst(match
, &value
->ipv6
);
1134 case MFF_IPV6_LABEL
:
1135 match_set_ipv6_label(match
, value
->be32
);
1139 match_set_nw_proto(match
, value
->u8
);
1143 match_set_nw_dscp(match
, value
->u8
);
1146 case MFF_IP_DSCP_SHIFTED
:
1147 match_set_nw_dscp(match
, value
->u8
<< 2);
1151 match_set_nw_ecn(match
, value
->u8
);
1155 match_set_nw_ttl(match
, value
->u8
);
1159 match_set_nw_frag(match
, value
->u8
);
1163 match_set_nw_proto(match
, ntohs(value
->be16
));
1167 match_set_nw_src(match
, value
->be32
);
1171 match_set_nw_dst(match
, value
->be32
);
1176 match_set_arp_sha(match
, value
->mac
);
1181 match_set_arp_tha(match
, value
->mac
);
1187 match_set_tp_src(match
, value
->be16
);
1193 match_set_tp_dst(match
, value
->be16
);
1197 match_set_tcp_flags(match
, value
->be16
);
1200 case MFF_ICMPV4_TYPE
:
1201 case MFF_ICMPV6_TYPE
:
1202 match_set_icmp_type(match
, value
->u8
);
1205 case MFF_ICMPV4_CODE
:
1206 case MFF_ICMPV6_CODE
:
1207 match_set_icmp_code(match
, value
->u8
);
1211 match_set_nd_target(match
, &value
->ipv6
);
1215 MATCH_SET_FIELD_UINT8(match
, nsh
.flags
, value
->u8
);
1217 case MFF_NSH_MDTYPE
:
1218 MATCH_SET_FIELD_UINT8(match
, nsh
.mdtype
, value
->u8
);
1221 MATCH_SET_FIELD_UINT8(match
, nsh
.np
, value
->u8
);
1224 MATCH_SET_FIELD_BE32(match
, nsh
.spi
, value
->be32
);
1227 MATCH_SET_FIELD_UINT8(match
, nsh
.si
, value
->u8
);
1233 MATCH_SET_FIELD_BE32(match
, nsh
.c
[mf
->id
- MFF_NSH_C1
], value
->be32
);
1242 /* Unwildcard the bits in 'mask' of the 'wc' member field described by 'mf'.
1243 * The caller is responsible for ensuring that 'wc' meets 'mf''s
1246 mf_mask_field_masked(const struct mf_field
*mf
, const union mf_value
*mask
,
1247 struct flow_wildcards
*wc
)
1249 union mf_value temp_mask
;
1250 /* For MFF_DL_VLAN, we cannot send a all 1's to flow_set_dl_vlan() as that
1251 * will be considered as OFP10_VLAN_NONE. So make sure the mask only has
1252 * valid bits in this case. */
1253 if (mf
->id
== MFF_DL_VLAN
) {
1254 temp_mask
.be16
= htons(VLAN_VID_MASK
) & mask
->be16
;
1258 union mf_value mask_value
;
1260 mf_get_value(mf
, &wc
->masks
, &mask_value
);
1261 for (size_t i
= 0; i
< mf
->n_bytes
; i
++) {
1262 mask_value
.b
[i
] |= mask
->b
[i
];
1264 mf_set_flow_value(mf
, &mask_value
, &wc
->masks
);
1267 /* Unwildcard 'wc' member field described by 'mf'. The caller is
1268 * responsible for ensuring that 'mask' meets 'mf''s prerequisites. */
1270 mf_mask_field(const struct mf_field
*mf
, struct flow_wildcards
*wc
)
1272 mf_mask_field_masked(mf
, &exact_match_mask
, wc
);
1276 field_len(const struct mf_field
*mf
, const union mf_value
*value_
)
1278 const uint8_t *value
= &value_
->u8
;
1281 if (!mf
->variable_len
) {
1289 for (i
= 0; i
< mf
->n_bytes
; i
++) {
1290 if (value
[i
] != 0) {
1295 return mf
->n_bytes
- i
;
1298 /* Returns the effective length of the field. For fixed length fields,
1299 * this is just the defined length. For variable length fields, it is
1300 * the minimum size encoding that retains the same meaning (i.e.
1301 * discarding leading zeros).
1303 * 'is_masked' returns (if non-NULL) whether the original contained
1304 * a mask. Otherwise, a mask that is the same length as the value
1305 * might be misinterpreted as an exact match. */
1307 mf_field_len(const struct mf_field
*mf
, const union mf_value
*value
,
1308 const union mf_value
*mask
, bool *is_masked_
)
1311 bool is_masked
= mask
&& !is_all_ones(mask
, mf
->n_bytes
);
1313 len
= field_len(mf
, value
);
1315 mask_len
= field_len(mf
, mask
);
1316 len
= MAX(len
, mask_len
);
1320 *is_masked_
= is_masked
;
1326 /* Sets 'flow' member field described by 'mf' to 'value'. The caller is
1327 * responsible for ensuring that 'flow' meets 'mf''s prerequisites.*/
1329 mf_set_flow_value(const struct mf_field
*mf
,
1330 const union mf_value
*value
, struct flow
*flow
)
1334 flow
->dp_hash
= ntohl(value
->be32
);
1337 flow
->recirc_id
= ntohl(value
->be32
);
1339 case MFF_PACKET_TYPE
:
1340 flow
->packet_type
= value
->be32
;
1343 flow
->conj_id
= ntohl(value
->be32
);
1346 flow
->tunnel
.tun_id
= value
->be64
;
1349 flow
->tunnel
.ip_src
= value
->be32
;
1352 flow
->tunnel
.ip_dst
= value
->be32
;
1354 case MFF_TUN_IPV6_SRC
:
1355 flow
->tunnel
.ipv6_src
= value
->ipv6
;
1357 case MFF_TUN_IPV6_DST
:
1358 flow
->tunnel
.ipv6_dst
= value
->ipv6
;
1361 flow
->tunnel
.flags
= (flow
->tunnel
.flags
& ~FLOW_TNL_PUB_F_MASK
) |
1364 case MFF_TUN_GBP_ID
:
1365 flow
->tunnel
.gbp_id
= value
->be16
;
1367 case MFF_TUN_GBP_FLAGS
:
1368 flow
->tunnel
.gbp_flags
= value
->u8
;
1371 flow
->tunnel
.ip_tos
= value
->u8
;
1374 flow
->tunnel
.ip_ttl
= value
->u8
;
1376 CASE_MFF_TUN_METADATA
:
1377 tun_metadata_write(&flow
->tunnel
, mf
, value
);
1380 flow
->metadata
= value
->be64
;
1384 flow
->in_port
.ofp_port
= u16_to_ofp(ntohs(value
->be16
));
1386 case MFF_IN_PORT_OXM
:
1387 ofputil_port_from_ofp11(value
->be32
, &flow
->in_port
.ofp_port
);
1389 case MFF_ACTSET_OUTPUT
:
1390 ofputil_port_from_ofp11(value
->be32
, &flow
->actset_output
);
1393 case MFF_SKB_PRIORITY
:
1394 flow
->skb_priority
= ntohl(value
->be32
);
1398 flow
->pkt_mark
= ntohl(value
->be32
);
1402 flow
->ct_state
= ntohl(value
->be32
);
1406 flow
->ct_zone
= ntohs(value
->be16
);
1410 flow
->ct_mark
= ntohl(value
->be32
);
1414 flow
->ct_label
= ntoh128(value
->be128
);
1417 case MFF_CT_NW_PROTO
:
1418 flow
->ct_nw_proto
= value
->u8
;
1422 flow
->ct_nw_src
= value
->be32
;
1426 flow
->ct_nw_dst
= value
->be32
;
1429 case MFF_CT_IPV6_SRC
:
1430 flow
->ct_ipv6_src
= value
->ipv6
;
1433 case MFF_CT_IPV6_DST
:
1434 flow
->ct_ipv6_dst
= value
->ipv6
;
1438 flow
->ct_tp_src
= value
->be16
;
1442 flow
->ct_tp_dst
= value
->be16
;
1446 flow
->regs
[mf
->id
- MFF_REG0
] = ntohl(value
->be32
);
1450 flow_set_xreg(flow
, mf
->id
- MFF_XREG0
, ntohll(value
->be64
));
1454 flow_set_xxreg(flow
, mf
->id
- MFF_XXREG0
, ntoh128(value
->be128
));
1458 flow
->dl_src
= value
->mac
;
1462 flow
->dl_dst
= value
->mac
;
1466 flow
->dl_type
= value
->be16
;
1470 flow
->vlans
[0].tci
= value
->be16
;
1471 flow_fix_vlan_tpid(flow
);
1475 flow_set_dl_vlan(flow
, value
->be16
);
1476 flow_fix_vlan_tpid(flow
);
1480 flow_set_vlan_vid(flow
, value
->be16
);
1481 flow_fix_vlan_tpid(flow
);
1484 case MFF_DL_VLAN_PCP
:
1486 flow_set_vlan_pcp(flow
, value
->u8
);
1487 flow_fix_vlan_tpid(flow
);
1490 case MFF_MPLS_LABEL
:
1491 flow_set_mpls_label(flow
, 0, value
->be32
);
1495 flow_set_mpls_tc(flow
, 0, value
->u8
);
1499 flow_set_mpls_bos(flow
, 0, value
->u8
);
1503 flow_set_mpls_ttl(flow
, 0, value
->u8
);
1507 flow
->nw_src
= value
->be32
;
1511 flow
->nw_dst
= value
->be32
;
1515 flow
->ipv6_src
= value
->ipv6
;
1519 flow
->ipv6_dst
= value
->ipv6
;
1522 case MFF_IPV6_LABEL
:
1523 flow
->ipv6_label
= value
->be32
& htonl(IPV6_LABEL_MASK
);
1527 flow
->nw_proto
= value
->u8
;
1531 flow
->nw_tos
&= ~IP_DSCP_MASK
;
1532 flow
->nw_tos
|= value
->u8
& IP_DSCP_MASK
;
1535 case MFF_IP_DSCP_SHIFTED
:
1536 flow
->nw_tos
&= ~IP_DSCP_MASK
;
1537 flow
->nw_tos
|= value
->u8
<< 2;
1541 flow
->nw_tos
&= ~IP_ECN_MASK
;
1542 flow
->nw_tos
|= value
->u8
& IP_ECN_MASK
;
1546 flow
->nw_ttl
= value
->u8
;
1550 flow
->nw_frag
= value
->u8
& FLOW_NW_FRAG_MASK
;
1554 flow
->nw_proto
= ntohs(value
->be16
);
1558 flow
->nw_src
= value
->be32
;
1562 flow
->nw_dst
= value
->be32
;
1567 flow
->arp_sha
= value
->mac
;
1572 flow
->arp_tha
= value
->mac
;
1578 flow
->tp_src
= value
->be16
;
1584 flow
->tp_dst
= value
->be16
;
1588 flow
->tcp_flags
= value
->be16
;
1591 case MFF_ICMPV4_TYPE
:
1592 case MFF_ICMPV6_TYPE
:
1593 flow
->tp_src
= htons(value
->u8
);
1596 case MFF_ICMPV4_CODE
:
1597 case MFF_ICMPV6_CODE
:
1598 flow
->tp_dst
= htons(value
->u8
);
1602 flow
->nd_target
= value
->ipv6
;
1606 flow
->nsh
.flags
= value
->u8
;
1608 case MFF_NSH_MDTYPE
:
1609 flow
->nsh
.mdtype
= value
->u8
;
1612 flow
->nsh
.np
= value
->u8
;
1615 flow
->nsh
.spi
= value
->be32
;
1618 flow
->nsh
.si
= value
->u8
;
1624 flow
->nsh
.c
[mf
->id
- MFF_NSH_C1
] = value
->be32
;
1633 /* Consider each of 'src', 'mask', and 'dst' as if they were arrays of 8*n
1634 * bits. Then, for each 0 <= i < 8 * n such that mask[i] == 1, sets dst[i] =
1637 apply_mask(const uint8_t *src
, const uint8_t *mask
, uint8_t *dst
, size_t n
)
1641 for (i
= 0; i
< n
; i
++) {
1642 dst
[i
] = (src
[i
] & mask
[i
]) | (dst
[i
] & ~mask
[i
]);
1646 /* Sets 'flow' member field described by 'field' to 'value', except that bits
1647 * for which 'mask' has a 0-bit keep their existing values. The caller is
1648 * responsible for ensuring that 'flow' meets 'field''s prerequisites.*/
1650 mf_set_flow_value_masked(const struct mf_field
*field
,
1651 const union mf_value
*value
,
1652 const union mf_value
*mask
,
1657 mf_get_value(field
, flow
, &tmp
);
1658 apply_mask((const uint8_t *) value
, (const uint8_t *) mask
,
1659 (uint8_t *) &tmp
, field
->n_bytes
);
1660 mf_set_flow_value(field
, &tmp
, flow
);
1664 mf_is_tun_metadata(const struct mf_field
*mf
)
1666 return mf
->id
>= MFF_TUN_METADATA0
&&
1667 mf
->id
< MFF_TUN_METADATA0
+ TUN_METADATA_NUM_OPTS
;
1671 mf_is_pipeline_field(const struct mf_field
*mf
)
1677 case MFF_TUN_IPV6_SRC
:
1678 case MFF_TUN_IPV6_DST
:
1680 case MFF_TUN_GBP_ID
:
1681 case MFF_TUN_GBP_FLAGS
:
1682 CASE_MFF_TUN_METADATA
:
1685 case MFF_IN_PORT_OXM
:
1689 case MFF_PACKET_TYPE
:
1697 case MFF_ACTSET_OUTPUT
:
1698 case MFF_SKB_PRIORITY
:
1704 case MFF_CT_NW_PROTO
:
1707 case MFF_CT_IPV6_SRC
:
1708 case MFF_CT_IPV6_DST
:
1717 case MFF_DL_VLAN_PCP
:
1719 case MFF_MPLS_LABEL
:
1727 case MFF_IPV6_LABEL
:
1730 case MFF_IP_DSCP_SHIFTED
:
1746 case MFF_ICMPV4_TYPE
:
1747 case MFF_ICMPV4_CODE
:
1748 case MFF_ICMPV6_TYPE
:
1749 case MFF_ICMPV6_CODE
:
1754 case MFF_NSH_MDTYPE
:
1770 /* Returns true if 'mf' has previously been set in 'flow', false if
1771 * it contains a non-default value.
1773 * The caller is responsible for ensuring that 'flow' meets 'mf''s
1776 mf_is_set(const struct mf_field
*mf
, const struct flow
*flow
)
1778 if (!mf_is_tun_metadata(mf
)) {
1779 union mf_value value
;
1781 mf_get_value(mf
, flow
, &value
);
1782 return !is_all_zeros(&value
, mf
->n_bytes
);
1784 return ULLONG_GET(flow
->tunnel
.metadata
.present
.map
,
1785 mf
->id
- MFF_TUN_METADATA0
);
1789 /* Makes 'match' wildcard field 'mf'.
1791 * The caller is responsible for ensuring that 'match' meets 'mf''s
1794 * If non-NULL, 'err_str' returns a malloc'ed string describing any errors
1795 * with the request or NULL if there is no error. The caller is reponsible
1796 * for freeing the string. */
1798 mf_set_wild(const struct mf_field
*mf
, struct match
*match
, char **err_str
)
1806 match
->flow
.dp_hash
= 0;
1807 match
->wc
.masks
.dp_hash
= 0;
1810 match
->flow
.recirc_id
= 0;
1811 match
->wc
.masks
.recirc_id
= 0;
1813 case MFF_PACKET_TYPE
:
1814 match
->flow
.packet_type
= 0;
1815 match
->wc
.masks
.packet_type
= 0;
1818 match
->flow
.conj_id
= 0;
1819 match
->wc
.masks
.conj_id
= 0;
1822 match_set_tun_id_masked(match
, htonll(0), htonll(0));
1825 match_set_tun_src_masked(match
, htonl(0), htonl(0));
1828 match_set_tun_dst_masked(match
, htonl(0), htonl(0));
1830 case MFF_TUN_IPV6_SRC
:
1831 memset(&match
->wc
.masks
.tunnel
.ipv6_src
, 0,
1832 sizeof match
->wc
.masks
.tunnel
.ipv6_src
);
1833 memset(&match
->flow
.tunnel
.ipv6_src
, 0,
1834 sizeof match
->flow
.tunnel
.ipv6_src
);
1836 case MFF_TUN_IPV6_DST
:
1837 memset(&match
->wc
.masks
.tunnel
.ipv6_dst
, 0,
1838 sizeof match
->wc
.masks
.tunnel
.ipv6_dst
);
1839 memset(&match
->flow
.tunnel
.ipv6_dst
, 0,
1840 sizeof match
->flow
.tunnel
.ipv6_dst
);
1843 match_set_tun_flags_masked(match
, 0, 0);
1845 case MFF_TUN_GBP_ID
:
1846 match_set_tun_gbp_id_masked(match
, 0, 0);
1848 case MFF_TUN_GBP_FLAGS
:
1849 match_set_tun_gbp_flags_masked(match
, 0, 0);
1852 match_set_tun_tos_masked(match
, 0, 0);
1855 match_set_tun_ttl_masked(match
, 0, 0);
1857 CASE_MFF_TUN_METADATA
:
1858 tun_metadata_set_match(mf
, NULL
, NULL
, match
, err_str
);
1862 match_set_metadata_masked(match
, htonll(0), htonll(0));
1866 case MFF_IN_PORT_OXM
:
1867 match
->flow
.in_port
.ofp_port
= 0;
1868 match
->wc
.masks
.in_port
.ofp_port
= 0;
1870 case MFF_ACTSET_OUTPUT
:
1871 match
->flow
.actset_output
= 0;
1872 match
->wc
.masks
.actset_output
= 0;
1875 case MFF_SKB_PRIORITY
:
1876 match
->flow
.skb_priority
= 0;
1877 match
->wc
.masks
.skb_priority
= 0;
1881 match
->flow
.pkt_mark
= 0;
1882 match
->wc
.masks
.pkt_mark
= 0;
1886 match
->flow
.ct_state
= 0;
1887 match
->wc
.masks
.ct_state
= 0;
1891 match
->flow
.ct_zone
= 0;
1892 match
->wc
.masks
.ct_zone
= 0;
1896 match
->flow
.ct_mark
= 0;
1897 match
->wc
.masks
.ct_mark
= 0;
1901 memset(&match
->flow
.ct_label
, 0, sizeof(match
->flow
.ct_label
));
1902 memset(&match
->wc
.masks
.ct_label
, 0, sizeof(match
->wc
.masks
.ct_label
));
1905 case MFF_CT_NW_PROTO
:
1906 match
->flow
.ct_nw_proto
= 0;
1907 match
->wc
.masks
.ct_nw_proto
= 0;
1911 match
->flow
.ct_nw_src
= 0;
1912 match
->wc
.masks
.ct_nw_src
= 0;
1916 match
->flow
.ct_nw_dst
= 0;
1917 match
->wc
.masks
.ct_nw_dst
= 0;
1920 case MFF_CT_IPV6_SRC
:
1921 memset(&match
->flow
.ct_ipv6_src
, 0, sizeof(match
->flow
.ct_ipv6_src
));
1922 WC_UNMASK_FIELD(&match
->wc
, ct_ipv6_src
);
1925 case MFF_CT_IPV6_DST
:
1926 memset(&match
->flow
.ct_ipv6_dst
, 0, sizeof(match
->flow
.ct_ipv6_dst
));
1927 WC_UNMASK_FIELD(&match
->wc
, ct_ipv6_dst
);
1931 match
->flow
.ct_tp_src
= 0;
1932 match
->wc
.masks
.ct_tp_src
= 0;
1936 match
->flow
.ct_tp_dst
= 0;
1937 match
->wc
.masks
.ct_tp_dst
= 0;
1941 match_set_reg_masked(match
, mf
->id
- MFF_REG0
, 0, 0);
1945 match_set_xreg_masked(match
, mf
->id
- MFF_XREG0
, 0, 0);
1949 match_set_xxreg_masked(match
, mf
->id
- MFF_XXREG0
, OVS_U128_ZERO
,
1955 match
->flow
.dl_src
= eth_addr_zero
;
1956 match
->wc
.masks
.dl_src
= eth_addr_zero
;
1960 match
->flow
.dl_dst
= eth_addr_zero
;
1961 match
->wc
.masks
.dl_dst
= eth_addr_zero
;
1965 match
->flow
.dl_type
= htons(0);
1966 match
->wc
.masks
.dl_type
= htons(0);
1970 match_set_dl_tci_masked(match
, htons(0), htons(0));
1975 match_set_any_vid(match
);
1978 case MFF_DL_VLAN_PCP
:
1980 match_set_any_pcp(match
);
1983 case MFF_MPLS_LABEL
:
1984 match_set_any_mpls_label(match
, 0);
1988 match_set_any_mpls_tc(match
, 0);
1992 match_set_any_mpls_bos(match
, 0);
1996 match_set_any_mpls_ttl(match
, 0);
2001 match_set_nw_src_masked(match
, htonl(0), htonl(0));
2006 match_set_nw_dst_masked(match
, htonl(0), htonl(0));
2010 memset(&match
->wc
.masks
.ipv6_src
, 0, sizeof match
->wc
.masks
.ipv6_src
);
2011 memset(&match
->flow
.ipv6_src
, 0, sizeof match
->flow
.ipv6_src
);
2015 memset(&match
->wc
.masks
.ipv6_dst
, 0, sizeof match
->wc
.masks
.ipv6_dst
);
2016 memset(&match
->flow
.ipv6_dst
, 0, sizeof match
->flow
.ipv6_dst
);
2019 case MFF_IPV6_LABEL
:
2020 match
->wc
.masks
.ipv6_label
= htonl(0);
2021 match
->flow
.ipv6_label
= htonl(0);
2025 match
->wc
.masks
.nw_proto
= 0;
2026 match
->flow
.nw_proto
= 0;
2030 case MFF_IP_DSCP_SHIFTED
:
2031 match
->wc
.masks
.nw_tos
&= ~IP_DSCP_MASK
;
2032 match
->flow
.nw_tos
&= ~IP_DSCP_MASK
;
2036 match
->wc
.masks
.nw_tos
&= ~IP_ECN_MASK
;
2037 match
->flow
.nw_tos
&= ~IP_ECN_MASK
;
2041 match
->wc
.masks
.nw_ttl
= 0;
2042 match
->flow
.nw_ttl
= 0;
2046 match
->wc
.masks
.nw_frag
&= ~FLOW_NW_FRAG_MASK
;
2047 match
->flow
.nw_frag
&= ~FLOW_NW_FRAG_MASK
;
2051 match
->wc
.masks
.nw_proto
= 0;
2052 match
->flow
.nw_proto
= 0;
2057 match
->flow
.arp_sha
= eth_addr_zero
;
2058 match
->wc
.masks
.arp_sha
= eth_addr_zero
;
2063 match
->flow
.arp_tha
= eth_addr_zero
;
2064 match
->wc
.masks
.arp_tha
= eth_addr_zero
;
2070 case MFF_ICMPV4_TYPE
:
2071 case MFF_ICMPV6_TYPE
:
2072 match
->wc
.masks
.tp_src
= htons(0);
2073 match
->flow
.tp_src
= htons(0);
2079 case MFF_ICMPV4_CODE
:
2080 case MFF_ICMPV6_CODE
:
2081 match
->wc
.masks
.tp_dst
= htons(0);
2082 match
->flow
.tp_dst
= htons(0);
2086 match
->wc
.masks
.tcp_flags
= htons(0);
2087 match
->flow
.tcp_flags
= htons(0);
2091 memset(&match
->wc
.masks
.nd_target
, 0,
2092 sizeof match
->wc
.masks
.nd_target
);
2093 memset(&match
->flow
.nd_target
, 0, sizeof match
->flow
.nd_target
);
2097 MATCH_SET_FIELD_MASKED(match
, nsh
.flags
, 0, 0);
2099 case MFF_NSH_MDTYPE
:
2100 MATCH_SET_FIELD_MASKED(match
, nsh
.mdtype
, 0, 0);
2103 MATCH_SET_FIELD_MASKED(match
, nsh
.np
, 0, 0);
2106 MATCH_SET_FIELD_MASKED(match
, nsh
.spi
, htonl(0), htonl(0));
2109 MATCH_SET_FIELD_MASKED(match
, nsh
.si
, 0, 0);
2115 MATCH_SET_FIELD_MASKED(match
, nsh
.c
[mf
->id
- MFF_NSH_C1
],
2116 htonl(0), htonl(0));
2125 /* Makes 'match' match field 'mf' with the specified 'value' and 'mask'.
2126 * 'value' specifies a value to match and 'mask' specifies a wildcard pattern,
2127 * with a 1-bit indicating that the corresponding value bit must match and a
2128 * 0-bit indicating a don't-care.
2130 * If 'mask' is NULL or points to all-1-bits, then this call is equivalent to
2131 * mf_set_value(mf, value, match). If 'mask' points to all-0-bits, then this
2132 * call is equivalent to mf_set_wild(mf, match).
2134 * 'mask' must be a valid mask for 'mf' (see mf_is_mask_valid()). The caller
2135 * is responsible for ensuring that 'match' meets 'mf''s prerequisites.
2137 * If non-NULL, 'err_str' returns a malloc'ed string describing any errors
2138 * with the request or NULL if there is no error. The caller is reponsible
2139 * for freeing the string.
2141 * Return a set of enum ofputil_protocol bits (as an uint32_t to avoid circular
2142 * dependency on enum ofputil_protocol definition) indicating which OpenFlow
2143 * protocol versions can support this functionality. */
2145 mf_set(const struct mf_field
*mf
,
2146 const union mf_value
*value
, const union mf_value
*mask
,
2147 struct match
*match
, char **err_str
)
2149 if (!mask
|| is_all_ones(mask
, mf
->n_bytes
)) {
2150 mf_set_value(mf
, value
, match
, err_str
);
2151 return mf
->usable_protocols_exact
;
2152 } else if (is_all_zeros(mask
, mf
->n_bytes
) && !mf_is_tun_metadata(mf
)) {
2153 /* Tunnel metadata matches on the existence of the field itself, so
2154 * it still needs to be encoded even if the value is wildcarded. */
2155 mf_set_wild(mf
, match
, err_str
);
2156 return OFPUTIL_P_ANY
;
2165 case MFF_CT_NW_PROTO
:
2168 case MFF_CT_IPV6_SRC
:
2169 case MFF_CT_IPV6_DST
:
2173 case MFF_PACKET_TYPE
:
2176 case MFF_IN_PORT_OXM
:
2177 case MFF_ACTSET_OUTPUT
:
2178 case MFF_SKB_PRIORITY
:
2181 case MFF_DL_VLAN_PCP
:
2183 case MFF_MPLS_LABEL
:
2190 case MFF_IP_DSCP_SHIFTED
:
2193 case MFF_ICMPV4_TYPE
:
2194 case MFF_ICMPV4_CODE
:
2195 case MFF_ICMPV6_TYPE
:
2196 case MFF_ICMPV6_CODE
:
2197 return OFPUTIL_P_NONE
;
2200 match_set_dp_hash_masked(match
, ntohl(value
->be32
), ntohl(mask
->be32
));
2203 match_set_tun_id_masked(match
, value
->be64
, mask
->be64
);
2206 match_set_tun_src_masked(match
, value
->be32
, mask
->be32
);
2209 match_set_tun_dst_masked(match
, value
->be32
, mask
->be32
);
2211 case MFF_TUN_IPV6_SRC
:
2212 match_set_tun_ipv6_src_masked(match
, &value
->ipv6
, &mask
->ipv6
);
2214 case MFF_TUN_IPV6_DST
:
2215 match_set_tun_ipv6_dst_masked(match
, &value
->ipv6
, &mask
->ipv6
);
2218 match_set_tun_flags_masked(match
, ntohs(value
->be16
), ntohs(mask
->be16
));
2220 case MFF_TUN_GBP_ID
:
2221 match_set_tun_gbp_id_masked(match
, value
->be16
, mask
->be16
);
2223 case MFF_TUN_GBP_FLAGS
:
2224 match_set_tun_gbp_flags_masked(match
, value
->u8
, mask
->u8
);
2227 match_set_tun_ttl_masked(match
, value
->u8
, mask
->u8
);
2230 match_set_tun_tos_masked(match
, value
->u8
, mask
->u8
);
2232 CASE_MFF_TUN_METADATA
:
2233 tun_metadata_set_match(mf
, value
, mask
, match
, err_str
);
2237 match_set_metadata_masked(match
, value
->be64
, mask
->be64
);
2241 match_set_reg_masked(match
, mf
->id
- MFF_REG0
,
2242 ntohl(value
->be32
), ntohl(mask
->be32
));
2246 match_set_xreg_masked(match
, mf
->id
- MFF_XREG0
,
2247 ntohll(value
->be64
), ntohll(mask
->be64
));
2251 match_set_xxreg_masked(match
, mf
->id
- MFF_XXREG0
,
2252 ntoh128(value
->be128
), ntoh128(mask
->be128
));
2257 match_set_pkt_mark_masked(match
, ntohl(value
->be32
),
2262 match_set_ct_state_masked(match
, ntohl(value
->be32
), ntohl(mask
->be32
));
2266 match_set_ct_mark_masked(match
, ntohl(value
->be32
), ntohl(mask
->be32
));
2270 match_set_ct_label_masked(match
, ntoh128(value
->be128
),
2271 ntoh128(mask
->be128
));
2275 match_set_dl_dst_masked(match
, value
->mac
, mask
->mac
);
2279 match_set_dl_src_masked(match
, value
->mac
, mask
->mac
);
2284 match_set_arp_sha_masked(match
, value
->mac
, mask
->mac
);
2289 match_set_arp_tha_masked(match
, value
->mac
, mask
->mac
);
2293 match_set_dl_tci_masked(match
, value
->be16
, mask
->be16
);
2297 match_set_vlan_vid_masked(match
, value
->be16
, mask
->be16
);
2301 match_set_nw_src_masked(match
, value
->be32
, mask
->be32
);
2305 match_set_nw_dst_masked(match
, value
->be32
, mask
->be32
);
2309 match_set_ipv6_src_masked(match
, &value
->ipv6
, &mask
->ipv6
);
2313 match_set_ipv6_dst_masked(match
, &value
->ipv6
, &mask
->ipv6
);
2316 case MFF_IPV6_LABEL
:
2317 if ((mask
->be32
& htonl(IPV6_LABEL_MASK
)) == htonl(IPV6_LABEL_MASK
)) {
2318 mf_set_value(mf
, value
, match
, err_str
);
2320 match_set_ipv6_label_masked(match
, value
->be32
, mask
->be32
);
2325 match_set_nd_target_masked(match
, &value
->ipv6
, &mask
->ipv6
);
2329 match_set_nw_frag_masked(match
, value
->u8
, mask
->u8
);
2333 match_set_nw_src_masked(match
, value
->be32
, mask
->be32
);
2337 match_set_nw_dst_masked(match
, value
->be32
, mask
->be32
);
2343 match_set_tp_src_masked(match
, value
->be16
, mask
->be16
);
2349 match_set_tp_dst_masked(match
, value
->be16
, mask
->be16
);
2353 match_set_tcp_flags_masked(match
, value
->be16
, mask
->be16
);
2357 MATCH_SET_FIELD_MASKED(match
, nsh
.flags
, value
->u8
, mask
->u8
);
2359 case MFF_NSH_MDTYPE
:
2360 MATCH_SET_FIELD_MASKED(match
, nsh
.mdtype
, value
->u8
, mask
->u8
);
2363 MATCH_SET_FIELD_MASKED(match
, nsh
.np
, value
->u8
, mask
->u8
);
2366 MATCH_SET_FIELD_MASKED(match
, nsh
.spi
, value
->be32
, mask
->be32
);
2369 MATCH_SET_FIELD_MASKED(match
, nsh
.si
, value
->u8
, mask
->u8
);
2375 MATCH_SET_FIELD_MASKED(match
, nsh
.c
[mf
->id
- MFF_NSH_C1
],
2376 value
->be32
, mask
->be32
);
2384 return ((mf
->usable_protocols_bitwise
== mf
->usable_protocols_cidr
2385 || ip_is_cidr(mask
->be32
))
2386 ? mf
->usable_protocols_cidr
2387 : mf
->usable_protocols_bitwise
);
2391 mf_check__(const struct mf_subfield
*sf
, const struct match
*match
,
2395 VLOG_WARN_RL(&rl
, "unknown %s field", type
);
2396 return OFPERR_OFPBAC_BAD_SET_TYPE
;
2397 } else if (!sf
->n_bits
) {
2398 VLOG_WARN_RL(&rl
, "zero bit %s field %s", type
, sf
->field
->name
);
2399 return OFPERR_OFPBAC_BAD_SET_LEN
;
2400 } else if (sf
->ofs
>= sf
->field
->n_bits
) {
2401 VLOG_WARN_RL(&rl
, "bit offset %d exceeds %d-bit width of %s field %s",
2402 sf
->ofs
, sf
->field
->n_bits
, type
, sf
->field
->name
);
2403 return OFPERR_OFPBAC_BAD_SET_LEN
;
2404 } else if (sf
->ofs
+ sf
->n_bits
> sf
->field
->n_bits
) {
2405 VLOG_WARN_RL(&rl
, "bit offset %d and width %d exceeds %d-bit width "
2406 "of %s field %s", sf
->ofs
, sf
->n_bits
,
2407 sf
->field
->n_bits
, type
, sf
->field
->name
);
2408 return OFPERR_OFPBAC_BAD_SET_LEN
;
2409 } else if (match
&& !mf_are_match_prereqs_ok(sf
->field
, match
)) {
2410 VLOG_WARN_RL(&rl
, "%s field %s lacks correct prerequisites",
2411 type
, sf
->field
->name
);
2412 return OFPERR_OFPBAC_MATCH_INCONSISTENT
;
2418 /* Sets all the bits in 'sf' to 1 within 'wc', if 'wc' is nonnull. */
2420 unwildcard_subfield(const struct mf_subfield
*sf
, struct flow_wildcards
*wc
)
2423 union mf_value mask
;
2425 memset(&mask
, 0, sizeof mask
);
2426 bitwise_one(&mask
, sf
->field
->n_bytes
, sf
->ofs
, sf
->n_bits
);
2427 mf_mask_field_masked(sf
->field
, &mask
, wc
);
2431 /* Copies 'src' into 'dst' within 'flow', and sets all the bits in 'src' and
2432 * 'dst' to 1s in 'wc', if 'wc' is nonnull.
2434 * 'src' and 'dst' may overlap. */
2436 mf_subfield_copy(const struct mf_subfield
*src
,
2437 const struct mf_subfield
*dst
,
2438 struct flow
*flow
, struct flow_wildcards
*wc
)
2440 ovs_assert(src
->n_bits
== dst
->n_bits
);
2441 if (mf_are_prereqs_ok(dst
->field
, flow
, wc
)
2442 && mf_are_prereqs_ok(src
->field
, flow
, wc
)) {
2443 unwildcard_subfield(src
, wc
);
2444 unwildcard_subfield(dst
, wc
);
2446 union mf_value src_value
;
2447 union mf_value dst_value
;
2448 mf_get_value(dst
->field
, flow
, &dst_value
);
2449 mf_get_value(src
->field
, flow
, &src_value
);
2450 bitwise_copy(&src_value
, src
->field
->n_bytes
, src
->ofs
,
2451 &dst_value
, dst
->field
->n_bytes
, dst
->ofs
,
2453 mf_set_flow_value(dst
->field
, &dst_value
, flow
);
2457 /* Swaps the bits in 'src' and 'dst' within 'flow', and sets all the bits in
2458 * 'src' and 'dst' to 1s in 'wc', if 'wc' is nonnull.
2460 * 'src' and 'dst' may overlap. */
2462 mf_subfield_swap(const struct mf_subfield
*a
,
2463 const struct mf_subfield
*b
,
2464 struct flow
*flow
, struct flow_wildcards
*wc
)
2466 ovs_assert(a
->n_bits
== b
->n_bits
);
2467 if (mf_are_prereqs_ok(a
->field
, flow
, wc
)
2468 && mf_are_prereqs_ok(b
->field
, flow
, wc
)) {
2469 unwildcard_subfield(a
, wc
);
2470 unwildcard_subfield(b
, wc
);
2472 union mf_value a_value
;
2473 union mf_value b_value
;
2474 mf_get_value(a
->field
, flow
, &a_value
);
2475 mf_get_value(b
->field
, flow
, &b_value
);
2476 union mf_value b2_value
= b_value
;
2478 /* Copy 'a' into 'b'. */
2479 bitwise_copy(&a_value
, a
->field
->n_bytes
, a
->ofs
,
2480 &b_value
, b
->field
->n_bytes
, b
->ofs
,
2482 mf_set_flow_value(b
->field
, &b_value
, flow
);
2484 /* Copy original 'b' into 'a'. */
2485 bitwise_copy(&b2_value
, b
->field
->n_bytes
, b
->ofs
,
2486 &a_value
, a
->field
->n_bytes
, a
->ofs
,
2488 mf_set_flow_value(a
->field
, &a_value
, flow
);
2492 /* Checks whether 'sf' is valid for reading a subfield out of 'flow'. Returns
2493 * 0 if so, otherwise an OpenFlow error code (e.g. as returned by
2496 mf_check_src(const struct mf_subfield
*sf
, const struct match
*match
)
2498 return mf_check__(sf
, match
, "source");
2501 /* Checks whether 'sf' is valid for writing a subfield into 'flow'. Returns 0
2502 * if so, otherwise an OpenFlow error code (e.g. as returned by
2505 mf_check_dst(const struct mf_subfield
*sf
, const struct match
*match
)
2507 int error
= mf_check__(sf
, match
, "destination");
2508 if (!error
&& !sf
->field
->writable
) {
2509 VLOG_WARN_RL(&rl
, "destination field %s is not writable",
2511 return OFPERR_OFPBAC_BAD_SET_ARGUMENT
;
2516 /* Copies the value and wildcard bit pattern for 'mf' from 'match' into the
2517 * 'value' and 'mask', respectively. */
2519 mf_get(const struct mf_field
*mf
, const struct match
*match
,
2520 union mf_value
*value
, union mf_value
*mask
)
2522 mf_get_value(mf
, &match
->flow
, value
);
2523 mf_get_mask(mf
, &match
->wc
, mask
);
2527 mf_from_integer_string(const struct mf_field
*mf
, const char *s
,
2528 uint8_t *valuep
, uint8_t *maskp
)
2531 const char *err_str
= "";
2534 err
= parse_int_string(s
, valuep
, mf
->n_bytes
, &tail
);
2535 if (err
|| (*tail
!= '\0' && *tail
!= '/')) {
2541 err
= parse_int_string(tail
+ 1, maskp
, mf
->n_bytes
, &tail
);
2542 if (err
|| *tail
!= '\0') {
2547 memset(maskp
, 0xff, mf
->n_bytes
);
2553 if (err
== ERANGE
) {
2554 return xasprintf("%s: %s too large for %u-byte field %s",
2555 s
, err_str
, mf
->n_bytes
, mf
->name
);
2557 return xasprintf("%s: bad syntax for %s %s", s
, mf
->name
, err_str
);
2562 mf_from_packet_type_string(const char *s
, ovs_be32
*packet_type
)
2565 const char *err_str
= "";
2569 err_str
= "missing '('";
2573 err
= parse_int_string(s
, (uint8_t *)packet_type
, 2, &tail
);
2579 err_str
= "missing ','";
2583 err
= parse_int_string(s
, ((uint8_t *)packet_type
) + 2, 2, &tail
);
2585 err_str
= "ns_type";
2589 err_str
= "missing ')'";
2596 return xasprintf("%s: bad syntax for packet type %s", s
, err_str
);
2600 mf_from_ethernet_string(const struct mf_field
*mf
, const char *s
,
2601 struct eth_addr
*mac
, struct eth_addr
*mask
)
2605 ovs_assert(mf
->n_bytes
== ETH_ADDR_LEN
);
2608 if (ovs_scan(s
, ETH_ADDR_SCAN_FMT
"%n", ETH_ADDR_SCAN_ARGS(*mac
), &n
)
2609 && n
== strlen(s
)) {
2610 *mask
= eth_addr_exact
;
2615 if (ovs_scan(s
, ETH_ADDR_SCAN_FMT
"/"ETH_ADDR_SCAN_FMT
"%n",
2616 ETH_ADDR_SCAN_ARGS(*mac
), ETH_ADDR_SCAN_ARGS(*mask
), &n
)
2617 && n
== strlen(s
)) {
2621 return xasprintf("%s: invalid Ethernet address", s
);
2625 mf_from_ipv4_string(const struct mf_field
*mf
, const char *s
,
2626 ovs_be32
*ip
, ovs_be32
*mask
)
2628 ovs_assert(mf
->n_bytes
== sizeof *ip
);
2629 return ip_parse_masked(s
, ip
, mask
);
2633 mf_from_ipv6_string(const struct mf_field
*mf
, const char *s
,
2634 struct in6_addr
*ipv6
, struct in6_addr
*mask
)
2636 ovs_assert(mf
->n_bytes
== sizeof *ipv6
);
2637 return ipv6_parse_masked(s
, ipv6
, mask
);
2641 mf_from_ofp_port_string(const struct mf_field
*mf
, const char *s
,
2642 const struct ofputil_port_map
*port_map
,
2643 ovs_be16
*valuep
, ovs_be16
*maskp
)
2647 ovs_assert(mf
->n_bytes
== sizeof(ovs_be16
));
2649 if (ofputil_port_from_string(s
, port_map
, &port
)) {
2650 *valuep
= htons(ofp_to_u16(port
));
2651 *maskp
= OVS_BE16_MAX
;
2654 return xasprintf("%s: invalid or unknown port for %s", s
, mf
->name
);
2658 mf_from_ofp_port_string32(const struct mf_field
*mf
, const char *s
,
2659 const struct ofputil_port_map
*port_map
,
2660 ovs_be32
*valuep
, ovs_be32
*maskp
)
2664 ovs_assert(mf
->n_bytes
== sizeof(ovs_be32
));
2665 if (ofputil_port_from_string(s
, port_map
, &port
)) {
2666 *valuep
= ofputil_port_to_ofp11(port
);
2667 *maskp
= OVS_BE32_MAX
;
2670 return xasprintf("%s: port value out of range for %s", s
, mf
->name
);
2673 struct frag_handling
{
2679 static const struct frag_handling all_frags
[] = {
2680 #define A FLOW_NW_FRAG_ANY
2681 #define L FLOW_NW_FRAG_LATER
2682 /* name mask value */
2685 { "first", A
|L
, A
},
2686 { "later", A
|L
, A
|L
},
2691 { "not_later", L
, 0 },
2698 mf_from_frag_string(const char *s
, uint8_t *valuep
, uint8_t *maskp
)
2700 const struct frag_handling
*h
;
2702 for (h
= all_frags
; h
< &all_frags
[ARRAY_SIZE(all_frags
)]; h
++) {
2703 if (!strcasecmp(s
, h
->name
)) {
2704 /* We force the upper bits of the mask on to make mf_parse_value()
2705 * happy (otherwise it will never think it's an exact match.) */
2706 *maskp
= h
->mask
| ~FLOW_NW_FRAG_MASK
;
2712 return xasprintf("%s: unknown fragment type (valid types are \"no\", "
2713 "\"yes\", \"first\", \"later\", \"not_first\"", s
);
2717 parse_mf_flags(const char *s
, const char *(*bit_to_string
)(uint32_t),
2718 const char *field_name
, ovs_be16
*flagsp
, ovs_be16 allowed
,
2723 uint32_t flags
, mask
;
2725 err
= parse_flags(s
, bit_to_string
, '\0', field_name
, &err_str
,
2726 &flags
, ntohs(allowed
), maskp
? &mask
: NULL
);
2731 *flagsp
= htons(flags
);
2733 *maskp
= htons(mask
);
2740 mf_from_tcp_flags_string(const char *s
, ovs_be16
*flagsp
, ovs_be16
*maskp
)
2742 return parse_mf_flags(s
, packet_tcp_flag_to_string
, "TCP", flagsp
,
2743 TCP_FLAGS_BE16(OVS_BE16_MAX
), maskp
);
2747 mf_from_tun_flags_string(const char *s
, ovs_be16
*flagsp
, ovs_be16
*maskp
)
2749 return parse_mf_flags(s
, flow_tun_flag_to_string
, "tunnel", flagsp
,
2750 htons(FLOW_TNL_PUB_F_MASK
), maskp
);
2754 mf_from_ct_state_string(const char *s
, ovs_be32
*flagsp
, ovs_be32
*maskp
)
2758 uint32_t flags
, mask
;
2760 err
= parse_flags(s
, ct_state_to_string
, '\0', "ct_state", &err_str
,
2761 &flags
, CS_SUPPORTED_MASK
, maskp
? &mask
: NULL
);
2766 *flagsp
= htonl(flags
);
2768 *maskp
= htonl(mask
);
2774 /* Parses 's', a string value for field 'mf', into 'value' and 'mask'. Returns
2775 * NULL if successful, otherwise a malloc()'d string describing the error. */
2777 mf_parse(const struct mf_field
*mf
, const char *s
,
2778 const struct ofputil_port_map
*port_map
,
2779 union mf_value
*value
, union mf_value
*mask
)
2783 if (!strcmp(s
, "*")) {
2784 memset(value
, 0, mf
->n_bytes
);
2785 memset(mask
, 0, mf
->n_bytes
);
2789 switch (mf
->string
) {
2791 case MFS_HEXADECIMAL
:
2792 error
= mf_from_integer_string(mf
, s
,
2793 (uint8_t *) value
, (uint8_t *) mask
);
2797 ovs_assert(mf
->n_bytes
== sizeof(ovs_be32
));
2798 error
= mf_from_ct_state_string(s
, &value
->be32
, &mask
->be32
);
2802 error
= mf_from_ethernet_string(mf
, s
, &value
->mac
, &mask
->mac
);
2806 error
= mf_from_ipv4_string(mf
, s
, &value
->be32
, &mask
->be32
);
2810 error
= mf_from_ipv6_string(mf
, s
, &value
->ipv6
, &mask
->ipv6
);
2814 error
= mf_from_ofp_port_string(mf
, s
, port_map
,
2815 &value
->be16
, &mask
->be16
);
2818 case MFS_OFP_PORT_OXM
:
2819 error
= mf_from_ofp_port_string32(mf
, s
, port_map
,
2820 &value
->be32
, &mask
->be32
);
2824 error
= mf_from_frag_string(s
, &value
->u8
, &mask
->u8
);
2828 ovs_assert(mf
->n_bytes
== sizeof(ovs_be16
));
2829 error
= mf_from_tun_flags_string(s
, &value
->be16
, &mask
->be16
);
2833 ovs_assert(mf
->n_bytes
== sizeof(ovs_be16
));
2834 error
= mf_from_tcp_flags_string(s
, &value
->be16
, &mask
->be16
);
2837 case MFS_PACKET_TYPE
:
2838 ovs_assert(mf
->n_bytes
== sizeof(ovs_be32
));
2839 error
= mf_from_packet_type_string(s
, &value
->be32
);
2840 mask
->be32
= OVS_BE32_MAX
;
2847 if (!error
&& !mf_is_mask_valid(mf
, mask
)) {
2848 error
= xasprintf("%s: invalid mask for field %s", s
, mf
->name
);
2853 /* Parses 's', a string value for field 'mf', into 'value'. Returns NULL if
2854 * successful, otherwise a malloc()'d string describing the error. */
2856 mf_parse_value(const struct mf_field
*mf
, const char *s
,
2857 const struct ofputil_port_map
*port_map
, union mf_value
*value
)
2859 union mf_value mask
;
2862 error
= mf_parse(mf
, s
, port_map
, value
, &mask
);
2867 if (!is_all_ones((const uint8_t *) &mask
, mf
->n_bytes
)) {
2868 return xasprintf("%s: wildcards not allowed here", s
);
2874 mf_format_integer_string(const struct mf_field
*mf
, const uint8_t *valuep
,
2875 const uint8_t *maskp
, struct ds
*s
)
2877 if (mf
->string
== MFS_HEXADECIMAL
) {
2878 ds_put_hex(s
, valuep
, mf
->n_bytes
);
2880 unsigned long long int integer
= 0;
2883 ovs_assert(mf
->n_bytes
<= 8);
2884 for (i
= 0; i
< mf
->n_bytes
; i
++) {
2885 integer
= (integer
<< 8) | valuep
[i
];
2887 ds_put_format(s
, "%lld", integer
);
2891 /* I guess we could write the mask in decimal for MFS_DECIMAL but I'm
2892 * not sure that that a bit-mask written in decimal is ever easier to
2893 * understand than the same bit-mask written in hexadecimal. */
2894 ds_put_char(s
, '/');
2895 ds_put_hex(s
, maskp
, mf
->n_bytes
);
2900 mf_format_frag_string(uint8_t value
, uint8_t mask
, struct ds
*s
)
2902 const struct frag_handling
*h
;
2904 mask
&= FLOW_NW_FRAG_MASK
;
2907 for (h
= all_frags
; h
< &all_frags
[ARRAY_SIZE(all_frags
)]; h
++) {
2908 if (value
== h
->value
&& mask
== h
->mask
) {
2909 ds_put_cstr(s
, h
->name
);
2913 ds_put_cstr(s
, "<error>");
2917 mf_format_tnl_flags_string(ovs_be16 value
, ovs_be16 mask
, struct ds
*s
)
2919 format_flags_masked(s
, NULL
, flow_tun_flag_to_string
, ntohs(value
),
2920 ntohs(mask
) & FLOW_TNL_PUB_F_MASK
, FLOW_TNL_PUB_F_MASK
);
2924 mf_format_tcp_flags_string(ovs_be16 value
, ovs_be16 mask
, struct ds
*s
)
2926 format_flags_masked(s
, NULL
, packet_tcp_flag_to_string
, ntohs(value
),
2927 TCP_FLAGS(mask
), TCP_FLAGS(OVS_BE16_MAX
));
2931 mf_format_ct_state_string(ovs_be32 value
, ovs_be32 mask
, struct ds
*s
)
2933 format_flags_masked(s
, NULL
, ct_state_to_string
, ntohl(value
),
2934 ntohl(mask
), UINT16_MAX
);
2938 mf_format_packet_type_string(ovs_be32 value
, ovs_be32 mask
, struct ds
*s
)
2940 format_packet_type_masked(s
, value
, mask
);
2943 /* Appends to 's' a string representation of field 'mf' whose value is in
2944 * 'value' and 'mask'. 'mask' may be NULL to indicate an exact match. */
2946 mf_format(const struct mf_field
*mf
,
2947 const union mf_value
*value
, const union mf_value
*mask
,
2948 const struct ofputil_port_map
*port_map
,
2952 if (is_all_zeros(mask
, mf
->n_bytes
)) {
2953 ds_put_cstr(s
, "ANY");
2955 } else if (is_all_ones(mask
, mf
->n_bytes
)) {
2960 switch (mf
->string
) {
2961 case MFS_OFP_PORT_OXM
:
2964 ofputil_port_from_ofp11(value
->be32
, &port
);
2965 ofputil_format_port(port
, port_map
, s
);
2971 ofputil_format_port(u16_to_ofp(ntohs(value
->be16
)), port_map
, s
);
2976 case MFS_HEXADECIMAL
:
2977 mf_format_integer_string(mf
, (uint8_t *) value
, (uint8_t *) mask
, s
);
2981 mf_format_ct_state_string(value
->be32
,
2982 mask
? mask
->be32
: OVS_BE32_MAX
, s
);
2986 eth_format_masked(value
->mac
, mask
? &mask
->mac
: NULL
, s
);
2990 ip_format_masked(value
->be32
, mask
? mask
->be32
: OVS_BE32_MAX
, s
);
2994 ipv6_format_masked(&value
->ipv6
, mask
? &mask
->ipv6
: NULL
, s
);
2998 mf_format_frag_string(value
->u8
, mask
? mask
->u8
: UINT8_MAX
, s
);
3002 mf_format_tnl_flags_string(value
->be16
,
3003 mask
? mask
->be16
: OVS_BE16_MAX
, s
);
3007 mf_format_tcp_flags_string(value
->be16
,
3008 mask
? mask
->be16
: OVS_BE16_MAX
, s
);
3011 case MFS_PACKET_TYPE
:
3012 mf_format_packet_type_string(value
->be32
,
3013 mask
? mask
->be32
: OVS_BE32_MAX
, s
);
3021 /* Makes subfield 'sf' within 'flow' exactly match the 'sf->n_bits'
3022 * least-significant bits in 'x'.
3025 mf_write_subfield_flow(const struct mf_subfield
*sf
,
3026 const union mf_subvalue
*x
, struct flow
*flow
)
3028 const struct mf_field
*field
= sf
->field
;
3029 union mf_value value
;
3031 mf_get_value(field
, flow
, &value
);
3032 bitwise_copy(x
, sizeof *x
, 0, &value
, field
->n_bytes
,
3033 sf
->ofs
, sf
->n_bits
);
3034 mf_set_flow_value(field
, &value
, flow
);
3037 /* Makes subfield 'sf' within 'match' exactly match the 'sf->n_bits'
3038 * least-significant bits in 'x'.
3041 mf_write_subfield(const struct mf_subfield
*sf
, const union mf_subvalue
*x
,
3042 struct match
*match
)
3044 const struct mf_field
*field
= sf
->field
;
3045 union mf_value value
, mask
;
3047 mf_get(field
, match
, &value
, &mask
);
3048 bitwise_copy(x
, sizeof *x
, 0, &value
, field
->n_bytes
, sf
->ofs
, sf
->n_bits
);
3049 bitwise_one ( &mask
, field
->n_bytes
, sf
->ofs
, sf
->n_bits
);
3050 mf_set(field
, &value
, &mask
, match
, NULL
);
3054 mf_write_subfield_value(const struct mf_subfield
*sf
, const void *src
,
3055 struct match
*match
)
3057 const struct mf_field
*field
= sf
->field
;
3058 union mf_value value
, mask
;
3059 unsigned int size
= DIV_ROUND_UP(sf
->n_bits
, 8);
3061 mf_get(field
, match
, &value
, &mask
);
3062 bitwise_copy(src
, size
, 0, &value
, field
->n_bytes
, sf
->ofs
, sf
->n_bits
);
3063 bitwise_one ( &mask
, field
->n_bytes
, sf
->ofs
, sf
->n_bits
);
3064 mf_set(field
, &value
, &mask
, match
, NULL
);
3067 /* 'v' and 'm' correspond to values of 'field'. This function copies them into
3068 * 'match' in the correspond positions. */
3070 mf_mask_subfield(const struct mf_field
*field
,
3071 const union mf_subvalue
*v
,
3072 const union mf_subvalue
*m
,
3073 struct match
*match
)
3075 union mf_value value
, mask
;
3077 mf_get(field
, match
, &value
, &mask
);
3078 bitwise_copy(v
, sizeof *v
, 0, &value
, field
->n_bytes
, 0, field
->n_bits
);
3079 bitwise_copy(m
, sizeof *m
, 0, &mask
, field
->n_bytes
, 0, field
->n_bits
);
3080 mf_set(field
, &value
, &mask
, match
, NULL
);
3083 /* Initializes 'x' to the value of 'sf' within 'flow'. 'sf' must be valid for
3084 * reading 'flow', e.g. as checked by mf_check_src(). */
3086 mf_read_subfield(const struct mf_subfield
*sf
, const struct flow
*flow
,
3087 union mf_subvalue
*x
)
3089 union mf_value value
;
3091 mf_get_value(sf
->field
, flow
, &value
);
3093 memset(x
, 0, sizeof *x
);
3094 bitwise_copy(&value
, sf
->field
->n_bytes
, sf
->ofs
,
3099 /* Returns the value of 'sf' within 'flow'. 'sf' must be valid for reading
3100 * 'flow', e.g. as checked by mf_check_src() and sf->n_bits must be 64 or
3103 mf_get_subfield(const struct mf_subfield
*sf
, const struct flow
*flow
)
3105 union mf_value value
;
3107 mf_get_value(sf
->field
, flow
, &value
);
3108 return bitwise_get(&value
, sf
->field
->n_bytes
, sf
->ofs
, sf
->n_bits
);
3112 mf_format_subvalue(const union mf_subvalue
*subvalue
, struct ds
*s
)
3114 ds_put_hex(s
, subvalue
->u8
, sizeof subvalue
->u8
);
3118 field_array_set(enum mf_field_id id
, const union mf_value
*value
,
3119 struct field_array
*fa
)
3121 size_t i
, offset
= 0;
3123 ovs_assert(id
< MFF_N_IDS
);
3125 /* Find the spot for 'id'. */
3126 BITMAP_FOR_EACH_1 (i
, id
, fa
->used
.bm
) {
3127 offset
+= mf_from_id(i
)->n_bytes
;
3130 size_t value_size
= mf_from_id(id
)->n_bytes
;
3132 /* make room if necessary. */
3133 if (!bitmap_is_set(fa
->used
.bm
, id
)) {
3134 fa
->values
= xrealloc(fa
->values
, fa
->values_size
+ value_size
);
3135 /* Move remainder forward, if any. */
3136 if (offset
< fa
->values_size
) {
3137 memmove(fa
->values
+ offset
+ value_size
, fa
->values
+ offset
,
3138 fa
->values_size
- offset
);
3140 fa
->values_size
+= value_size
;
3142 bitmap_set1(fa
->used
.bm
, id
);
3144 memcpy(fa
->values
+ offset
, value
, value_size
);
3147 /* A wrapper for variable length mf_fields that is maintained by
3148 * struct vl_mff_map.*/
3149 struct vl_mf_field
{
3151 struct ovs_refcount ref_cnt
;
3152 struct cmap_node cmap_node
; /* In ofproto->vl_mff_map->cmap. */
3155 static inline uint32_t
3156 mf_field_hash(uint32_t key
)
3158 return hash_int(key
, 0);
3162 vmf_delete(struct vl_mf_field
*vmf
)
3164 if (ovs_refcount_unref(&vmf
->ref_cnt
) == 1) {
3165 /* Postpone as this function is typically called immediately
3166 * after removing from cmap. */
3167 ovsrcu_postpone(free
, vmf
);
3170 "Attempted to delete VMF %s but refcount is nonzero!",
3176 mf_vl_mff_map_clear(struct vl_mff_map
*vl_mff_map
, bool force
)
3177 OVS_REQUIRES(vl_mff_map
->mutex
)
3179 struct vl_mf_field
*vmf
;
3182 CMAP_FOR_EACH (vmf
, cmap_node
, &vl_mff_map
->cmap
) {
3183 if (ovs_refcount_read(&vmf
->ref_cnt
) != 1) {
3184 return OFPERR_NXTTMFC_INVALID_TLV_DEL
;
3189 CMAP_FOR_EACH (vmf
, cmap_node
, &vl_mff_map
->cmap
) {
3190 cmap_remove(&vl_mff_map
->cmap
, &vmf
->cmap_node
,
3191 mf_field_hash(vmf
->mf
.id
));
3198 static struct vl_mf_field
*
3199 mf_get_vl_mff__(uint32_t id
, const struct vl_mff_map
*vl_mff_map
)
3201 struct vl_mf_field
*vmf
;
3203 CMAP_FOR_EACH_WITH_HASH (vmf
, cmap_node
, mf_field_hash(id
),
3204 &vl_mff_map
->cmap
) {
3205 if (vmf
->mf
.id
== id
) {
3213 /* If 'mff' is a variable length field, looks up 'vl_mff_map', returns a
3214 * pointer to the variable length meta-flow field corresponding to 'mff'.
3215 * Returns NULL if no mapping is existed for 'mff'. */
3216 const struct mf_field
*
3217 mf_get_vl_mff(const struct mf_field
*mff
,
3218 const struct vl_mff_map
*vl_mff_map
)
3220 if (mff
&& mff
->variable_len
&& vl_mff_map
) {
3221 return &mf_get_vl_mff__(mff
->id
, vl_mff_map
)->mf
;
3228 mf_vl_mff_map_del(struct vl_mff_map
*vl_mff_map
,
3229 const struct ofputil_tlv_table_mod
*ttm
, bool force
)
3230 OVS_REQUIRES(vl_mff_map
->mutex
)
3232 struct ofputil_tlv_map
*tlv_map
;
3233 struct vl_mf_field
*vmf
;
3237 LIST_FOR_EACH (tlv_map
, list_node
, &ttm
->mappings
) {
3238 idx
= MFF_TUN_METADATA0
+ tlv_map
->index
;
3239 if (idx
>= MFF_TUN_METADATA0
+ TUN_METADATA_NUM_OPTS
) {
3240 return OFPERR_NXTTMFC_BAD_FIELD_IDX
;
3243 vmf
= mf_get_vl_mff__(idx
, vl_mff_map
);
3244 if (vmf
&& ovs_refcount_read(&vmf
->ref_cnt
) != 1) {
3245 return OFPERR_NXTTMFC_INVALID_TLV_DEL
;
3250 LIST_FOR_EACH (tlv_map
, list_node
, &ttm
->mappings
) {
3251 idx
= MFF_TUN_METADATA0
+ tlv_map
->index
;
3252 if (idx
>= MFF_TUN_METADATA0
+ TUN_METADATA_NUM_OPTS
) {
3253 return OFPERR_NXTTMFC_BAD_FIELD_IDX
;
3256 vmf
= mf_get_vl_mff__(idx
, vl_mff_map
);
3258 cmap_remove(&vl_mff_map
->cmap
, &vmf
->cmap_node
,
3259 mf_field_hash(idx
));
3268 mf_vl_mff_map_add(struct vl_mff_map
*vl_mff_map
,
3269 const struct ofputil_tlv_table_mod
*ttm
)
3270 OVS_REQUIRES(vl_mff_map
->mutex
)
3272 struct ofputil_tlv_map
*tlv_map
;
3273 struct vl_mf_field
*vmf
;
3276 LIST_FOR_EACH (tlv_map
, list_node
, &ttm
->mappings
) {
3277 idx
= MFF_TUN_METADATA0
+ tlv_map
->index
;
3278 if (idx
>= MFF_TUN_METADATA0
+ TUN_METADATA_NUM_OPTS
) {
3279 return OFPERR_NXTTMFC_BAD_FIELD_IDX
;
3282 vmf
= xmalloc(sizeof *vmf
);
3283 vmf
->mf
= mf_fields
[idx
];
3284 vmf
->mf
.n_bytes
= tlv_map
->option_len
;
3285 vmf
->mf
.n_bits
= tlv_map
->option_len
* 8;
3286 vmf
->mf
.mapped
= true;
3287 ovs_refcount_init(&vmf
->ref_cnt
);
3289 cmap_insert(&vl_mff_map
->cmap
, &vmf
->cmap_node
,
3290 mf_field_hash(idx
));
3296 /* Updates the tun_metadata mf_field in 'vl_mff_map' according to 'ttm'.
3297 * This function must be invoked after tun_metadata_table_mod().
3298 * Returns OFPERR_NXTTMFC_BAD_FIELD_IDX, if the index for the vl_mf_field is
3300 * Returns OFPERR_NXTTMFC_INVALID_TLV_DEL, if 'ttm' tries to delete an
3301 * vl_mf_field that is still used by any active flow.*/
3303 mf_vl_mff_map_mod_from_tun_metadata(struct vl_mff_map
*vl_mff_map
,
3304 const struct ofputil_tlv_table_mod
*ttm
)
3305 OVS_REQUIRES(vl_mff_map
->mutex
)
3307 switch (ttm
->command
) {
3309 return mf_vl_mff_map_add(vl_mff_map
, ttm
);
3312 return mf_vl_mff_map_del(vl_mff_map
, ttm
, false);
3315 return mf_vl_mff_map_clear(vl_mff_map
, false);
3324 /* Returns true if a variable length meta-flow field 'mff' is not mapped in
3325 * the 'vl_mff_map'. */
3327 mf_vl_mff_invalid(const struct mf_field
*mff
, const struct vl_mff_map
*map
)
3329 return map
&& mff
&& mff
->variable_len
&& !mff
->mapped
;
3333 mf_vl_mff_set_tlv_bitmap(const struct mf_field
*mff
, uint64_t *tlv_bitmap
)
3335 if (mff
&& mff
->mapped
) {
3336 ovs_assert(mf_is_tun_metadata(mff
));
3337 ULLONG_SET1(*tlv_bitmap
, mff
->id
- MFF_TUN_METADATA0
);
3342 mf_vl_mff_ref_cnt_mod(const struct vl_mff_map
*map
, uint64_t tlv_bitmap
,
3345 struct vl_mf_field
*vmf
;
3349 ULLONG_FOR_EACH_1 (i
, tlv_bitmap
) {
3350 vmf
= mf_get_vl_mff__(i
+ MFF_TUN_METADATA0
, map
);
3353 ovs_refcount_ref(&vmf
->ref_cnt
);
3355 ovs_refcount_unref(&vmf
->ref_cnt
);
3358 VLOG_WARN("Invalid TLV index %d.", i
);
3365 mf_vl_mff_ref(const struct vl_mff_map
*map
, uint64_t tlv_bitmap
)
3367 mf_vl_mff_ref_cnt_mod(map
, tlv_bitmap
, true);
3371 mf_vl_mff_unref(const struct vl_mff_map
*map
, uint64_t tlv_bitmap
)
3373 mf_vl_mff_ref_cnt_mod(map
, tlv_bitmap
, false);
3377 mf_vl_mff_nx_pull_header(struct ofpbuf
*b
, const struct vl_mff_map
*vl_mff_map
,
3378 const struct mf_field
**field
, bool *masked
,
3379 uint64_t *tlv_bitmap
)
3381 enum ofperr error
= nx_pull_header(b
, vl_mff_map
, field
, masked
);
3386 mf_vl_mff_set_tlv_bitmap(*field
, tlv_bitmap
);
3391 mf_vl_mff_nx_pull_entry(struct ofpbuf
*b
, const struct vl_mff_map
*vl_mff_map
,
3392 const struct mf_field
**field
, union mf_value
*value
,
3393 union mf_value
*mask
, uint64_t *tlv_bitmap
)
3395 enum ofperr error
= nx_pull_entry(b
, vl_mff_map
, field
, value
, mask
);
3400 mf_vl_mff_set_tlv_bitmap(*field
, tlv_bitmap
);
3405 mf_vl_mff_mf_from_nxm_header(uint32_t header
,
3406 const struct vl_mff_map
*vl_mff_map
,
3407 const struct mf_field
**field
,
3408 uint64_t *tlv_bitmap
)
3410 *field
= mf_from_nxm_header(header
, vl_mff_map
);
3411 if (mf_vl_mff_invalid(*field
, vl_mff_map
)) {
3412 return OFPERR_NXFMFC_INVALID_TLV_FIELD
;
3415 mf_vl_mff_set_tlv_bitmap(*field
, tlv_bitmap
);