2 * Copyright (c) 2011, 2012, 2013, 2014, 2015 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 "meta-flow.h"
23 #include <netinet/icmp6.h>
24 #include <netinet/ip6.h>
26 #include "classifier.h"
27 #include "dynamic-string.h"
29 #include "ofp-errors.h"
31 #include "ovs-thread.h"
35 #include "socket-util.h"
36 #include "tun-metadata.h"
37 #include "unaligned.h"
39 #include "openvswitch/vlog.h"
41 VLOG_DEFINE_THIS_MODULE(meta_flow
);
43 #define FLOW_U32OFS(FIELD) \
44 offsetof(struct flow, FIELD) % 4 ? -1 : offsetof(struct flow, FIELD) / 4
46 #define MF_FIELD_SIZES(MEMBER) \
47 sizeof ((union mf_value *)0)->MEMBER, \
48 8 * sizeof ((union mf_value *)0)->MEMBER
50 extern const struct mf_field mf_fields
[MFF_N_IDS
]; /* Silence a warning. */
52 const struct mf_field mf_fields
[MFF_N_IDS
] = {
53 #include "meta-flow.inc"
56 /* Maps from an mf_field's 'name' or 'extra_name' to the mf_field. */
57 static struct shash mf_by_name
;
59 /* Rate limit for parse errors. These always indicate a bug in an OpenFlow
60 * controller and so there's not much point in showing a lot of them. */
61 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
63 #define MF_VALUE_EXACT_8 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
64 #define MF_VALUE_EXACT_16 MF_VALUE_EXACT_8, MF_VALUE_EXACT_8
65 #define MF_VALUE_EXACT_32 MF_VALUE_EXACT_16, MF_VALUE_EXACT_16
66 #define MF_VALUE_EXACT_64 MF_VALUE_EXACT_32, MF_VALUE_EXACT_32
67 #define MF_VALUE_EXACT_128 MF_VALUE_EXACT_64, MF_VALUE_EXACT_64
68 #define MF_VALUE_EXACT_INITIALIZER { .tun_metadata = { MF_VALUE_EXACT_128 } }
70 const union mf_value exact_match_mask
= MF_VALUE_EXACT_INITIALIZER
;
72 static void nxm_init(void);
74 /* Returns the field with the given 'name', or a null pointer if no field has
76 const struct mf_field
*
77 mf_from_name(const char *name
)
80 return shash_find_data(&mf_by_name
, name
);
88 shash_init(&mf_by_name
);
89 for (i
= 0; i
< MFF_N_IDS
; i
++) {
90 const struct mf_field
*mf
= &mf_fields
[i
];
92 ovs_assert(mf
->id
== i
); /* Fields must be in the enum order. */
94 shash_add_once(&mf_by_name
, mf
->name
, mf
);
96 shash_add_once(&mf_by_name
, mf
->extra_name
, mf
);
104 static pthread_once_t once
= PTHREAD_ONCE_INIT
;
105 pthread_once(&once
, nxm_do_init
);
108 /* Consider the two value/mask pairs 'a_value/a_mask' and 'b_value/b_mask' as
109 * restrictions on a field's value. Then, this function initializes
110 * 'dst_value/dst_mask' such that it combines the restrictions of both pairs.
111 * This is not always possible, i.e. if one pair insists on a value of 0 in
112 * some bit and the other pair insists on a value of 1 in that bit. This
113 * function returns false in a case where the combined restriction is
114 * impossible (in which case 'dst_value/dst_mask' is not fully initialized),
117 * (As usually true for value/mask pairs in OVS, any 1-bit in a value must have
118 * a corresponding 1-bit in its mask.) */
120 mf_subvalue_intersect(const union mf_subvalue
*a_value
,
121 const union mf_subvalue
*a_mask
,
122 const union mf_subvalue
*b_value
,
123 const union mf_subvalue
*b_mask
,
124 union mf_subvalue
*dst_value
,
125 union mf_subvalue
*dst_mask
)
127 for (int i
= 0; i
< ARRAY_SIZE(a_value
->be64
); i
++) {
128 ovs_be64 av
= a_value
->be64
[i
];
129 ovs_be64 am
= a_mask
->be64
[i
];
130 ovs_be64 bv
= b_value
->be64
[i
];
131 ovs_be64 bm
= b_mask
->be64
[i
];
132 ovs_be64
*dv
= &dst_value
->be64
[i
];
133 ovs_be64
*dm
= &dst_mask
->be64
[i
];
135 if ((av
^ bv
) & (am
& bm
)) {
144 /* Returns the "number of bits" in 'v', e.g. 1 if only the lowest-order bit is
145 * set, 2 if the second-lowest-order bit is set, and so on. */
147 mf_subvalue_width(const union mf_subvalue
*v
)
149 return 1 + bitwise_rscan(v
, sizeof *v
, true, sizeof *v
* 8 - 1, -1);
152 /* For positive 'n', shifts the bits in 'value' 'n' bits to the left, and for
153 * negative 'n', shifts the bits '-n' bits to the right. */
155 mf_subvalue_shift(union mf_subvalue
*value
, int n
)
158 union mf_subvalue tmp
;
159 memset(&tmp
, 0, sizeof tmp
);
161 if (n
> 0 && n
< 8 * sizeof tmp
) {
162 bitwise_copy(value
, sizeof *value
, 0,
165 } else if (n
< 0 && n
> -8 * sizeof tmp
) {
166 bitwise_copy(value
, sizeof *value
, -n
,
174 /* Returns true if 'wc' wildcards all the bits in field 'mf', false if 'wc'
175 * specifies at least one bit in the field.
177 * The caller is responsible for ensuring that 'wc' corresponds to a flow that
178 * meets 'mf''s prerequisites. */
180 mf_is_all_wild(const struct mf_field
*mf
, const struct flow_wildcards
*wc
)
184 return !wc
->masks
.dp_hash
;
186 return !wc
->masks
.recirc_id
;
188 return !wc
->masks
.conj_id
;
190 return !wc
->masks
.tunnel
.ip_src
;
192 return !wc
->masks
.tunnel
.ip_dst
;
194 return !wc
->masks
.tunnel
.tun_id
;
196 return !wc
->masks
.tunnel
.ip_tos
;
198 return !wc
->masks
.tunnel
.ip_ttl
;
200 return !(wc
->masks
.tunnel
.flags
& FLOW_TNL_PUB_F_MASK
);
202 return !wc
->masks
.tunnel
.gbp_id
;
203 case MFF_TUN_GBP_FLAGS
:
204 return !wc
->masks
.tunnel
.gbp_flags
;
205 CASE_MFF_TUN_METADATA
:
206 return !ULLONG_GET(wc
->masks
.tunnel
.metadata
.present
.map
,
207 mf
->id
- MFF_TUN_METADATA0
);
209 return !wc
->masks
.metadata
;
211 case MFF_IN_PORT_OXM
:
212 return !wc
->masks
.in_port
.ofp_port
;
213 case MFF_SKB_PRIORITY
:
214 return !wc
->masks
.skb_priority
;
216 return !wc
->masks
.pkt_mark
;
218 return !wc
->masks
.ct_state
;
220 return !wc
->masks
.ct_zone
;
222 return !wc
->masks
.ct_mark
;
224 return ovs_u128_is_zero(&wc
->masks
.ct_label
);
226 return !wc
->masks
.regs
[mf
->id
- MFF_REG0
];
228 return !flow_get_xreg(&wc
->masks
, mf
->id
- MFF_XREG0
);
229 case MFF_ACTSET_OUTPUT
:
230 return !wc
->masks
.actset_output
;
233 return eth_addr_is_zero(wc
->masks
.dl_src
);
235 return eth_addr_is_zero(wc
->masks
.dl_dst
);
237 return !wc
->masks
.dl_type
;
241 return eth_addr_is_zero(wc
->masks
.arp_sha
);
245 return eth_addr_is_zero(wc
->masks
.arp_tha
);
248 return !wc
->masks
.vlan_tci
;
250 return !(wc
->masks
.vlan_tci
& htons(VLAN_VID_MASK
));
252 return !(wc
->masks
.vlan_tci
& htons(VLAN_VID_MASK
| VLAN_CFI
));
253 case MFF_DL_VLAN_PCP
:
255 return !(wc
->masks
.vlan_tci
& htons(VLAN_PCP_MASK
));
258 return !(wc
->masks
.mpls_lse
[0] & htonl(MPLS_LABEL_MASK
));
260 return !(wc
->masks
.mpls_lse
[0] & htonl(MPLS_TC_MASK
));
262 return !(wc
->masks
.mpls_lse
[0] & htonl(MPLS_BOS_MASK
));
265 return !wc
->masks
.nw_src
;
267 return !wc
->masks
.nw_dst
;
270 return ipv6_mask_is_any(&wc
->masks
.ipv6_src
);
272 return ipv6_mask_is_any(&wc
->masks
.ipv6_dst
);
275 return !wc
->masks
.ipv6_label
;
278 return !wc
->masks
.nw_proto
;
280 case MFF_IP_DSCP_SHIFTED
:
281 return !(wc
->masks
.nw_tos
& IP_DSCP_MASK
);
283 return !(wc
->masks
.nw_tos
& IP_ECN_MASK
);
285 return !wc
->masks
.nw_ttl
;
288 return ipv6_mask_is_any(&wc
->masks
.nd_target
);
291 return !(wc
->masks
.nw_frag
& FLOW_NW_FRAG_MASK
);
294 return !wc
->masks
.nw_proto
;
296 return !wc
->masks
.nw_src
;
298 return !wc
->masks
.nw_dst
;
303 case MFF_ICMPV4_TYPE
:
304 case MFF_ICMPV6_TYPE
:
305 return !wc
->masks
.tp_src
;
309 case MFF_ICMPV4_CODE
:
310 case MFF_ICMPV6_CODE
:
311 return !wc
->masks
.tp_dst
;
313 return !wc
->masks
.tcp_flags
;
321 /* Initializes 'mask' with the wildcard bit pattern for field 'mf' within 'wc'.
322 * Each bit in 'mask' will be set to 1 if the bit is significant for matching
323 * purposes, or to 0 if it is wildcarded.
325 * The caller is responsible for ensuring that 'wc' corresponds to a flow that
326 * meets 'mf''s prerequisites. */
328 mf_get_mask(const struct mf_field
*mf
, const struct flow_wildcards
*wc
,
329 union mf_value
*mask
)
331 mf_get_value(mf
, &wc
->masks
, mask
);
334 /* Tests whether 'mask' is a valid wildcard bit pattern for 'mf'. Returns true
335 * if the mask is valid, false otherwise. */
337 mf_is_mask_valid(const struct mf_field
*mf
, const union mf_value
*mask
)
339 switch (mf
->maskable
) {
341 return (is_all_zeros(mask
, mf
->n_bytes
) ||
342 is_all_ones(mask
, mf
->n_bytes
));
351 /* Returns true if 'flow' meets the prerequisites for 'mf', false otherwise. */
353 mf_are_prereqs_ok(const struct mf_field
*mf
, const struct flow
*flow
)
355 switch (mf
->prereqs
) {
360 return (flow
->dl_type
== htons(ETH_TYPE_ARP
) ||
361 flow
->dl_type
== htons(ETH_TYPE_RARP
));
363 return flow
->dl_type
== htons(ETH_TYPE_IP
);
365 return flow
->dl_type
== htons(ETH_TYPE_IPV6
);
367 return (flow
->vlan_tci
& htons(VLAN_CFI
)) != 0;
369 return eth_type_mpls(flow
->dl_type
);
371 return is_ip_any(flow
);
374 return is_ip_any(flow
) && flow
->nw_proto
== IPPROTO_TCP
375 && !(flow
->nw_frag
& FLOW_NW_FRAG_LATER
);
377 return is_ip_any(flow
) && flow
->nw_proto
== IPPROTO_UDP
378 && !(flow
->nw_frag
& FLOW_NW_FRAG_LATER
);
380 return is_ip_any(flow
) && flow
->nw_proto
== IPPROTO_SCTP
381 && !(flow
->nw_frag
& FLOW_NW_FRAG_LATER
);
383 return is_icmpv4(flow
);
385 return is_icmpv6(flow
);
388 return (is_icmpv6(flow
)
389 && flow
->tp_dst
== htons(0)
390 && (flow
->tp_src
== htons(ND_NEIGHBOR_SOLICIT
) ||
391 flow
->tp_src
== htons(ND_NEIGHBOR_ADVERT
)));
393 return (is_icmpv6(flow
)
394 && flow
->tp_dst
== htons(0)
395 && (flow
->tp_src
== htons(ND_NEIGHBOR_SOLICIT
)));
397 return (is_icmpv6(flow
)
398 && flow
->tp_dst
== htons(0)
399 && (flow
->tp_src
== htons(ND_NEIGHBOR_ADVERT
)));
405 /* Set field and it's prerequisities in the mask.
406 * This is only ever called for writeable 'mf's, but we do not make the
407 * distinction here. */
409 mf_mask_field_and_prereqs(const struct mf_field
*mf
, struct flow_wildcards
*wc
)
411 mf_set_flow_value(mf
, &exact_match_mask
, &wc
->masks
);
413 switch (mf
->prereqs
) {
417 WC_MASK_FIELD(wc
, tp_src
);
418 WC_MASK_FIELD(wc
, tp_dst
);
425 /* nw_frag always unwildcarded. */
426 WC_MASK_FIELD(wc
, nw_proto
);
433 /* dl_type always unwildcarded. */
436 WC_MASK_FIELD_MASK(wc
, vlan_tci
, htons(VLAN_CFI
));
443 /* Set bits of 'bm' corresponding to the field 'mf' and it's prerequisities. */
445 mf_bitmap_set_field_and_prereqs(const struct mf_field
*mf
, struct mf_bitmap
*bm
)
447 bitmap_set1(bm
->bm
, mf
->id
);
449 switch (mf
->prereqs
) {
453 bitmap_set1(bm
->bm
, MFF_TCP_SRC
);
454 bitmap_set1(bm
->bm
, MFF_TCP_DST
);
461 /* nw_frag always unwildcarded. */
462 bitmap_set1(bm
->bm
, MFF_IP_PROTO
);
469 bitmap_set1(bm
->bm
, MFF_ETH_TYPE
);
472 bitmap_set1(bm
->bm
, MFF_VLAN_TCI
);
479 /* Returns true if 'value' may be a valid value *as part of a masked match*,
482 * A value is not rejected just because it is not valid for the field in
483 * question, but only if it doesn't make sense to test the bits in question at
484 * all. For example, the MFF_VLAN_TCI field will never have a nonzero value
485 * without the VLAN_CFI bit being set, but we can't reject those values because
486 * it is still legitimate to test just for those bits (see the documentation
487 * for NXM_OF_VLAN_TCI in nicira-ext.h). On the other hand, there is never a
488 * reason to set the low bit of MFF_IP_DSCP to 1, so we reject that. */
490 mf_is_value_valid(const struct mf_field
*mf
, const union mf_value
*value
)
502 case MFF_TUN_GBP_FLAGS
:
503 CASE_MFF_TUN_METADATA
:
506 case MFF_SKB_PRIORITY
:
533 case MFF_ICMPV4_TYPE
:
534 case MFF_ICMPV4_CODE
:
535 case MFF_ICMPV6_TYPE
:
536 case MFF_ICMPV6_CODE
:
542 case MFF_IN_PORT_OXM
:
543 case MFF_ACTSET_OUTPUT
: {
545 return !ofputil_port_from_ofp11(value
->be32
, &port
);
549 return !(value
->u8
& ~IP_DSCP_MASK
);
550 case MFF_IP_DSCP_SHIFTED
:
551 return !(value
->u8
& (~IP_DSCP_MASK
>> 2));
553 return !(value
->u8
& ~IP_ECN_MASK
);
555 return !(value
->u8
& ~FLOW_NW_FRAG_MASK
);
557 return !(value
->be16
& ~htons(0x0fff));
560 return !(value
->be16
& htons(0xff00));
563 return !(value
->be16
& htons(VLAN_CFI
| VLAN_PCP_MASK
));
565 return !(value
->be16
& htons(VLAN_PCP_MASK
));
567 case MFF_DL_VLAN_PCP
:
569 return !(value
->u8
& ~(VLAN_PCP_MASK
>> VLAN_PCP_SHIFT
));
572 return !(value
->be32
& ~htonl(IPV6_LABEL_MASK
));
575 return !(value
->be32
& ~htonl(MPLS_LABEL_MASK
>> MPLS_LABEL_SHIFT
));
578 return !(value
->u8
& ~(MPLS_TC_MASK
>> MPLS_TC_SHIFT
));
581 return !(value
->u8
& ~(MPLS_BOS_MASK
>> MPLS_BOS_SHIFT
));
584 return !(value
->be16
& ~htons(FLOW_TNL_PUB_F_MASK
));
587 return !(value
->be32
& ~htonl(CS_SUPPORTED_MASK
));
595 /* Copies the value of field 'mf' from 'flow' into 'value'. The caller is
596 * responsible for ensuring that 'flow' meets 'mf''s prerequisites. */
598 mf_get_value(const struct mf_field
*mf
, const struct flow
*flow
,
599 union mf_value
*value
)
603 value
->be32
= htonl(flow
->dp_hash
);
606 value
->be32
= htonl(flow
->recirc_id
);
609 value
->be32
= htonl(flow
->conj_id
);
612 value
->be64
= flow
->tunnel
.tun_id
;
615 value
->be32
= flow
->tunnel
.ip_src
;
618 value
->be32
= flow
->tunnel
.ip_dst
;
621 value
->be16
= htons(flow
->tunnel
.flags
& FLOW_TNL_PUB_F_MASK
);
624 value
->be16
= flow
->tunnel
.gbp_id
;
626 case MFF_TUN_GBP_FLAGS
:
627 value
->u8
= flow
->tunnel
.gbp_flags
;
630 value
->u8
= flow
->tunnel
.ip_ttl
;
633 value
->u8
= flow
->tunnel
.ip_tos
;
635 CASE_MFF_TUN_METADATA
:
636 tun_metadata_read(&flow
->tunnel
, mf
, value
);
640 value
->be64
= flow
->metadata
;
644 value
->be16
= htons(ofp_to_u16(flow
->in_port
.ofp_port
));
646 case MFF_IN_PORT_OXM
:
647 value
->be32
= ofputil_port_to_ofp11(flow
->in_port
.ofp_port
);
649 case MFF_ACTSET_OUTPUT
:
650 value
->be32
= ofputil_port_to_ofp11(flow
->actset_output
);
653 case MFF_SKB_PRIORITY
:
654 value
->be32
= htonl(flow
->skb_priority
);
658 value
->be32
= htonl(flow
->pkt_mark
);
662 value
->be32
= htonl(flow
->ct_state
);
666 value
->be16
= htons(flow
->ct_zone
);
670 value
->be32
= htonl(flow
->ct_mark
);
674 hton128(&flow
->ct_label
, &value
->be128
);
678 value
->be32
= htonl(flow
->regs
[mf
->id
- MFF_REG0
]);
682 value
->be64
= htonll(flow_get_xreg(flow
, mf
->id
- MFF_XREG0
));
686 value
->mac
= flow
->dl_src
;
690 value
->mac
= flow
->dl_dst
;
694 value
->be16
= flow
->dl_type
;
698 value
->be16
= flow
->vlan_tci
;
702 value
->be16
= flow
->vlan_tci
& htons(VLAN_VID_MASK
);
705 value
->be16
= flow
->vlan_tci
& htons(VLAN_VID_MASK
| VLAN_CFI
);
708 case MFF_DL_VLAN_PCP
:
710 value
->u8
= vlan_tci_to_pcp(flow
->vlan_tci
);
714 value
->be32
= htonl(mpls_lse_to_label(flow
->mpls_lse
[0]));
718 value
->u8
= mpls_lse_to_tc(flow
->mpls_lse
[0]);
722 value
->u8
= mpls_lse_to_bos(flow
->mpls_lse
[0]);
726 value
->be32
= flow
->nw_src
;
730 value
->be32
= flow
->nw_dst
;
734 value
->ipv6
= flow
->ipv6_src
;
738 value
->ipv6
= flow
->ipv6_dst
;
742 value
->be32
= flow
->ipv6_label
;
746 value
->u8
= flow
->nw_proto
;
750 value
->u8
= flow
->nw_tos
& IP_DSCP_MASK
;
753 case MFF_IP_DSCP_SHIFTED
:
754 value
->u8
= flow
->nw_tos
>> 2;
758 value
->u8
= flow
->nw_tos
& IP_ECN_MASK
;
762 value
->u8
= flow
->nw_ttl
;
766 value
->u8
= flow
->nw_frag
;
770 value
->be16
= htons(flow
->nw_proto
);
774 value
->be32
= flow
->nw_src
;
778 value
->be32
= flow
->nw_dst
;
783 value
->mac
= flow
->arp_sha
;
788 value
->mac
= flow
->arp_tha
;
794 value
->be16
= flow
->tp_src
;
800 value
->be16
= flow
->tp_dst
;
804 value
->be16
= flow
->tcp_flags
;
807 case MFF_ICMPV4_TYPE
:
808 case MFF_ICMPV6_TYPE
:
809 value
->u8
= ntohs(flow
->tp_src
);
812 case MFF_ICMPV4_CODE
:
813 case MFF_ICMPV6_CODE
:
814 value
->u8
= ntohs(flow
->tp_dst
);
818 value
->ipv6
= flow
->nd_target
;
827 /* Makes 'match' match field 'mf' exactly, with the value matched taken from
828 * 'value'. The caller is responsible for ensuring that 'match' meets 'mf''s
831 * If non-NULL, 'err_str' returns a malloc'ed string describing any errors
832 * with the request or NULL if there is no error. The caller is reponsible
833 * for freeing the string. */
835 mf_set_value(const struct mf_field
*mf
,
836 const union mf_value
*value
, struct match
*match
, char **err_str
)
844 match_set_dp_hash(match
, ntohl(value
->be32
));
847 match_set_recirc_id(match
, ntohl(value
->be32
));
850 match_set_conj_id(match
, ntohl(value
->be32
));
853 match_set_tun_id(match
, value
->be64
);
856 match_set_tun_src(match
, value
->be32
);
859 match_set_tun_dst(match
, value
->be32
);
862 match_set_tun_flags(match
, ntohs(value
->be16
));
865 match_set_tun_gbp_id(match
, value
->be16
);
867 case MFF_TUN_GBP_FLAGS
:
868 match_set_tun_gbp_flags(match
, value
->u8
);
871 match_set_tun_tos(match
, value
->u8
);
874 match_set_tun_ttl(match
, value
->u8
);
876 CASE_MFF_TUN_METADATA
:
877 tun_metadata_set_match(mf
, value
, NULL
, match
, err_str
);
881 match_set_metadata(match
, value
->be64
);
885 match_set_in_port(match
, u16_to_ofp(ntohs(value
->be16
)));
888 case MFF_IN_PORT_OXM
: {
890 ofputil_port_from_ofp11(value
->be32
, &port
);
891 match_set_in_port(match
, port
);
894 case MFF_ACTSET_OUTPUT
: {
896 ofputil_port_from_ofp11(value
->be32
, &port
);
897 match_set_actset_output(match
, port
);
901 case MFF_SKB_PRIORITY
:
902 match_set_skb_priority(match
, ntohl(value
->be32
));
906 match_set_pkt_mark(match
, ntohl(value
->be32
));
910 match_set_ct_state(match
, ntohl(value
->be32
));
914 match_set_ct_zone(match
, ntohs(value
->be16
));
918 match_set_ct_mark(match
, ntohl(value
->be32
));
924 ntoh128(&value
->be128
, &label
);
925 match_set_ct_label(match
, label
);
930 match_set_reg(match
, mf
->id
- MFF_REG0
, ntohl(value
->be32
));
934 match_set_xreg(match
, mf
->id
- MFF_XREG0
, ntohll(value
->be64
));
938 match_set_dl_src(match
, value
->mac
);
942 match_set_dl_dst(match
, value
->mac
);
946 match_set_dl_type(match
, value
->be16
);
950 match_set_dl_tci(match
, value
->be16
);
954 match_set_dl_vlan(match
, value
->be16
);
957 match_set_vlan_vid(match
, value
->be16
);
960 case MFF_DL_VLAN_PCP
:
962 match_set_dl_vlan_pcp(match
, value
->u8
);
966 match_set_mpls_label(match
, 0, value
->be32
);
970 match_set_mpls_tc(match
, 0, value
->u8
);
974 match_set_mpls_bos(match
, 0, value
->u8
);
978 match_set_nw_src(match
, value
->be32
);
982 match_set_nw_dst(match
, value
->be32
);
986 match_set_ipv6_src(match
, &value
->ipv6
);
990 match_set_ipv6_dst(match
, &value
->ipv6
);
994 match_set_ipv6_label(match
, value
->be32
);
998 match_set_nw_proto(match
, value
->u8
);
1002 match_set_nw_dscp(match
, value
->u8
);
1005 case MFF_IP_DSCP_SHIFTED
:
1006 match_set_nw_dscp(match
, value
->u8
<< 2);
1010 match_set_nw_ecn(match
, value
->u8
);
1014 match_set_nw_ttl(match
, value
->u8
);
1018 match_set_nw_frag(match
, value
->u8
);
1022 match_set_nw_proto(match
, ntohs(value
->be16
));
1026 match_set_nw_src(match
, value
->be32
);
1030 match_set_nw_dst(match
, value
->be32
);
1035 match_set_arp_sha(match
, value
->mac
);
1040 match_set_arp_tha(match
, value
->mac
);
1046 match_set_tp_src(match
, value
->be16
);
1052 match_set_tp_dst(match
, value
->be16
);
1056 match_set_tcp_flags(match
, value
->be16
);
1059 case MFF_ICMPV4_TYPE
:
1060 case MFF_ICMPV6_TYPE
:
1061 match_set_icmp_type(match
, value
->u8
);
1064 case MFF_ICMPV4_CODE
:
1065 case MFF_ICMPV6_CODE
:
1066 match_set_icmp_code(match
, value
->u8
);
1070 match_set_nd_target(match
, &value
->ipv6
);
1079 /* Unwildcard 'mask' member field described by 'mf'. The caller is
1080 * responsible for ensuring that 'mask' meets 'mf''s prerequisites. */
1082 mf_mask_field(const struct mf_field
*mf
, struct flow
*mask
)
1084 /* For MFF_DL_VLAN, we cannot send a all 1's to flow_set_dl_vlan()
1085 * as that will be considered as OFP10_VLAN_NONE. So consider it as a
1086 * special case. For the rest, calling mf_set_flow_value() is good
1088 if (mf
->id
== MFF_DL_VLAN
) {
1089 flow_set_dl_vlan(mask
, htons(VLAN_VID_MASK
));
1091 mf_set_flow_value(mf
, &exact_match_mask
, mask
);
1096 field_len(const struct mf_field
*mf
, const union mf_value
*value_
)
1098 const uint8_t *value
= &value_
->u8
;
1101 if (!mf
->variable_len
) {
1109 for (i
= 0; i
< mf
->n_bytes
; i
++) {
1110 if (value
[i
] != 0) {
1115 return mf
->n_bytes
- i
;
1118 /* Returns the effective length of the field. For fixed length fields,
1119 * this is just the defined length. For variable length fields, it is
1120 * the minimum size encoding that retains the same meaning (i.e.
1121 * discarding leading zeros).
1123 * 'is_masked' returns (if non-NULL) whether the original contained
1124 * a mask. Otherwise, a mask that is the same length as the value
1125 * might be misinterpreted as an exact match. */
1127 mf_field_len(const struct mf_field
*mf
, const union mf_value
*value
,
1128 const union mf_value
*mask
, bool *is_masked_
)
1131 bool is_masked
= mask
&& !is_all_ones(mask
, mf
->n_bytes
);
1133 len
= field_len(mf
, value
);
1135 mask_len
= field_len(mf
, mask
);
1136 len
= MAX(len
, mask_len
);
1140 *is_masked_
= is_masked
;
1146 /* Sets 'flow' member field described by 'mf' to 'value'. The caller is
1147 * responsible for ensuring that 'flow' meets 'mf''s prerequisites.*/
1149 mf_set_flow_value(const struct mf_field
*mf
,
1150 const union mf_value
*value
, struct flow
*flow
)
1154 flow
->dp_hash
= ntohl(value
->be32
);
1157 flow
->recirc_id
= ntohl(value
->be32
);
1160 flow
->conj_id
= ntohl(value
->be32
);
1163 flow
->tunnel
.tun_id
= value
->be64
;
1166 flow
->tunnel
.ip_src
= value
->be32
;
1169 flow
->tunnel
.ip_dst
= value
->be32
;
1172 flow
->tunnel
.flags
= (flow
->tunnel
.flags
& ~FLOW_TNL_PUB_F_MASK
) |
1175 case MFF_TUN_GBP_ID
:
1176 flow
->tunnel
.gbp_id
= value
->be16
;
1178 case MFF_TUN_GBP_FLAGS
:
1179 flow
->tunnel
.gbp_flags
= value
->u8
;
1182 flow
->tunnel
.ip_tos
= value
->u8
;
1185 flow
->tunnel
.ip_ttl
= value
->u8
;
1187 CASE_MFF_TUN_METADATA
:
1188 tun_metadata_write(&flow
->tunnel
, mf
, value
);
1191 flow
->metadata
= value
->be64
;
1195 flow
->in_port
.ofp_port
= u16_to_ofp(ntohs(value
->be16
));
1198 case MFF_IN_PORT_OXM
:
1199 ofputil_port_from_ofp11(value
->be32
, &flow
->in_port
.ofp_port
);
1201 case MFF_ACTSET_OUTPUT
:
1202 ofputil_port_from_ofp11(value
->be32
, &flow
->actset_output
);
1205 case MFF_SKB_PRIORITY
:
1206 flow
->skb_priority
= ntohl(value
->be32
);
1210 flow
->pkt_mark
= ntohl(value
->be32
);
1214 flow
->ct_state
= ntohl(value
->be32
);
1218 flow
->ct_zone
= ntohs(value
->be16
);
1222 flow
->ct_mark
= ntohl(value
->be32
);
1226 ntoh128(&value
->be128
, &flow
->ct_label
);
1230 flow
->regs
[mf
->id
- MFF_REG0
] = ntohl(value
->be32
);
1234 flow_set_xreg(flow
, mf
->id
- MFF_XREG0
, ntohll(value
->be64
));
1238 flow
->dl_src
= value
->mac
;
1242 flow
->dl_dst
= value
->mac
;
1246 flow
->dl_type
= value
->be16
;
1250 flow
->vlan_tci
= value
->be16
;
1254 flow_set_dl_vlan(flow
, value
->be16
);
1257 flow_set_vlan_vid(flow
, value
->be16
);
1260 case MFF_DL_VLAN_PCP
:
1262 flow_set_vlan_pcp(flow
, value
->u8
);
1265 case MFF_MPLS_LABEL
:
1266 flow_set_mpls_label(flow
, 0, value
->be32
);
1270 flow_set_mpls_tc(flow
, 0, value
->u8
);
1274 flow_set_mpls_bos(flow
, 0, value
->u8
);
1278 flow
->nw_src
= value
->be32
;
1282 flow
->nw_dst
= value
->be32
;
1286 flow
->ipv6_src
= value
->ipv6
;
1290 flow
->ipv6_dst
= value
->ipv6
;
1293 case MFF_IPV6_LABEL
:
1294 flow
->ipv6_label
= value
->be32
& htonl(IPV6_LABEL_MASK
);
1298 flow
->nw_proto
= value
->u8
;
1302 flow
->nw_tos
&= ~IP_DSCP_MASK
;
1303 flow
->nw_tos
|= value
->u8
& IP_DSCP_MASK
;
1306 case MFF_IP_DSCP_SHIFTED
:
1307 flow
->nw_tos
&= ~IP_DSCP_MASK
;
1308 flow
->nw_tos
|= value
->u8
<< 2;
1312 flow
->nw_tos
&= ~IP_ECN_MASK
;
1313 flow
->nw_tos
|= value
->u8
& IP_ECN_MASK
;
1317 flow
->nw_ttl
= value
->u8
;
1321 flow
->nw_frag
= value
->u8
& FLOW_NW_FRAG_MASK
;
1325 flow
->nw_proto
= ntohs(value
->be16
);
1329 flow
->nw_src
= value
->be32
;
1333 flow
->nw_dst
= value
->be32
;
1338 flow
->arp_sha
= value
->mac
;
1343 flow
->arp_tha
= value
->mac
;
1349 flow
->tp_src
= value
->be16
;
1355 flow
->tp_dst
= value
->be16
;
1359 flow
->tcp_flags
= value
->be16
;
1362 case MFF_ICMPV4_TYPE
:
1363 case MFF_ICMPV6_TYPE
:
1364 flow
->tp_src
= htons(value
->u8
);
1367 case MFF_ICMPV4_CODE
:
1368 case MFF_ICMPV6_CODE
:
1369 flow
->tp_dst
= htons(value
->u8
);
1373 flow
->nd_target
= value
->ipv6
;
1382 /* Consider each of 'src', 'mask', and 'dst' as if they were arrays of 8*n
1383 * bits. Then, for each 0 <= i < 8 * n such that mask[i] == 1, sets dst[i] =
1386 apply_mask(const uint8_t *src
, const uint8_t *mask
, uint8_t *dst
, size_t n
)
1390 for (i
= 0; i
< n
; i
++) {
1391 dst
[i
] = (src
[i
] & mask
[i
]) | (dst
[i
] & ~mask
[i
]);
1395 /* Sets 'flow' member field described by 'field' to 'value', except that bits
1396 * for which 'mask' has a 0-bit keep their existing values. The caller is
1397 * responsible for ensuring that 'flow' meets 'field''s prerequisites.*/
1399 mf_set_flow_value_masked(const struct mf_field
*field
,
1400 const union mf_value
*value
,
1401 const union mf_value
*mask
,
1406 mf_get_value(field
, flow
, &tmp
);
1407 apply_mask((const uint8_t *) value
, (const uint8_t *) mask
,
1408 (uint8_t *) &tmp
, field
->n_bytes
);
1409 mf_set_flow_value(field
, &tmp
, flow
);
1413 mf_is_tun_metadata(const struct mf_field
*mf
)
1415 return mf
->id
>= MFF_TUN_METADATA0
&&
1416 mf
->id
< MFF_TUN_METADATA0
+ TUN_METADATA_NUM_OPTS
;
1419 /* Returns true if 'mf' has previously been set in 'flow', false if
1420 * it contains a non-default value.
1422 * The caller is responsible for ensuring that 'flow' meets 'mf''s
1425 mf_is_set(const struct mf_field
*mf
, const struct flow
*flow
)
1427 if (!mf_is_tun_metadata(mf
)) {
1428 union mf_value value
;
1430 mf_get_value(mf
, flow
, &value
);
1431 return !is_all_zeros(&value
, mf
->n_bytes
);
1433 return ULLONG_GET(flow
->tunnel
.metadata
.present
.map
,
1434 mf
->id
- MFF_TUN_METADATA0
);
1438 /* Makes 'match' wildcard field 'mf'.
1440 * The caller is responsible for ensuring that 'match' meets 'mf''s
1443 * If non-NULL, 'err_str' returns a malloc'ed string describing any errors
1444 * with the request or NULL if there is no error. The caller is reponsible
1445 * for freeing the string. */
1447 mf_set_wild(const struct mf_field
*mf
, struct match
*match
, char **err_str
)
1455 match
->flow
.dp_hash
= 0;
1456 match
->wc
.masks
.dp_hash
= 0;
1459 match
->flow
.recirc_id
= 0;
1460 match
->wc
.masks
.recirc_id
= 0;
1463 match
->flow
.conj_id
= 0;
1464 match
->wc
.masks
.conj_id
= 0;
1467 match_set_tun_id_masked(match
, htonll(0), htonll(0));
1470 match_set_tun_src_masked(match
, htonl(0), htonl(0));
1473 match_set_tun_dst_masked(match
, htonl(0), htonl(0));
1476 match_set_tun_flags_masked(match
, 0, 0);
1478 case MFF_TUN_GBP_ID
:
1479 match_set_tun_gbp_id_masked(match
, 0, 0);
1481 case MFF_TUN_GBP_FLAGS
:
1482 match_set_tun_gbp_flags_masked(match
, 0, 0);
1485 match_set_tun_tos_masked(match
, 0, 0);
1488 match_set_tun_ttl_masked(match
, 0, 0);
1490 CASE_MFF_TUN_METADATA
:
1491 tun_metadata_set_match(mf
, NULL
, NULL
, match
, err_str
);
1495 match_set_metadata_masked(match
, htonll(0), htonll(0));
1499 case MFF_IN_PORT_OXM
:
1500 match
->flow
.in_port
.ofp_port
= 0;
1501 match
->wc
.masks
.in_port
.ofp_port
= 0;
1503 case MFF_ACTSET_OUTPUT
:
1504 match
->flow
.actset_output
= 0;
1505 match
->wc
.masks
.actset_output
= 0;
1508 case MFF_SKB_PRIORITY
:
1509 match
->flow
.skb_priority
= 0;
1510 match
->wc
.masks
.skb_priority
= 0;
1514 match
->flow
.pkt_mark
= 0;
1515 match
->wc
.masks
.pkt_mark
= 0;
1519 match
->flow
.ct_state
= 0;
1520 match
->wc
.masks
.ct_state
= 0;
1524 match
->flow
.ct_zone
= 0;
1525 match
->wc
.masks
.ct_zone
= 0;
1529 match
->flow
.ct_mark
= 0;
1530 match
->wc
.masks
.ct_mark
= 0;
1534 memset(&match
->flow
.ct_label
, 0, sizeof(match
->flow
.ct_label
));
1535 memset(&match
->wc
.masks
.ct_label
, 0, sizeof(match
->wc
.masks
.ct_label
));
1539 match_set_reg_masked(match
, mf
->id
- MFF_REG0
, 0, 0);
1543 match_set_xreg_masked(match
, mf
->id
- MFF_XREG0
, 0, 0);
1547 match
->flow
.dl_src
= eth_addr_zero
;
1548 match
->wc
.masks
.dl_src
= eth_addr_zero
;
1552 match
->flow
.dl_dst
= eth_addr_zero
;
1553 match
->wc
.masks
.dl_dst
= eth_addr_zero
;
1557 match
->flow
.dl_type
= htons(0);
1558 match
->wc
.masks
.dl_type
= htons(0);
1562 match_set_dl_tci_masked(match
, htons(0), htons(0));
1567 match_set_any_vid(match
);
1570 case MFF_DL_VLAN_PCP
:
1572 match_set_any_pcp(match
);
1575 case MFF_MPLS_LABEL
:
1576 match_set_any_mpls_label(match
, 0);
1580 match_set_any_mpls_tc(match
, 0);
1584 match_set_any_mpls_bos(match
, 0);
1589 match_set_nw_src_masked(match
, htonl(0), htonl(0));
1594 match_set_nw_dst_masked(match
, htonl(0), htonl(0));
1598 memset(&match
->wc
.masks
.ipv6_src
, 0, sizeof match
->wc
.masks
.ipv6_src
);
1599 memset(&match
->flow
.ipv6_src
, 0, sizeof match
->flow
.ipv6_src
);
1603 memset(&match
->wc
.masks
.ipv6_dst
, 0, sizeof match
->wc
.masks
.ipv6_dst
);
1604 memset(&match
->flow
.ipv6_dst
, 0, sizeof match
->flow
.ipv6_dst
);
1607 case MFF_IPV6_LABEL
:
1608 match
->wc
.masks
.ipv6_label
= htonl(0);
1609 match
->flow
.ipv6_label
= htonl(0);
1613 match
->wc
.masks
.nw_proto
= 0;
1614 match
->flow
.nw_proto
= 0;
1618 case MFF_IP_DSCP_SHIFTED
:
1619 match
->wc
.masks
.nw_tos
&= ~IP_DSCP_MASK
;
1620 match
->flow
.nw_tos
&= ~IP_DSCP_MASK
;
1624 match
->wc
.masks
.nw_tos
&= ~IP_ECN_MASK
;
1625 match
->flow
.nw_tos
&= ~IP_ECN_MASK
;
1629 match
->wc
.masks
.nw_ttl
= 0;
1630 match
->flow
.nw_ttl
= 0;
1634 match
->wc
.masks
.nw_frag
&= ~FLOW_NW_FRAG_MASK
;
1635 match
->flow
.nw_frag
&= ~FLOW_NW_FRAG_MASK
;
1639 match
->wc
.masks
.nw_proto
= 0;
1640 match
->flow
.nw_proto
= 0;
1645 match
->flow
.arp_sha
= eth_addr_zero
;
1646 match
->wc
.masks
.arp_sha
= eth_addr_zero
;
1651 match
->flow
.arp_tha
= eth_addr_zero
;
1652 match
->wc
.masks
.arp_tha
= eth_addr_zero
;
1658 case MFF_ICMPV4_TYPE
:
1659 case MFF_ICMPV6_TYPE
:
1660 match
->wc
.masks
.tp_src
= htons(0);
1661 match
->flow
.tp_src
= htons(0);
1667 case MFF_ICMPV4_CODE
:
1668 case MFF_ICMPV6_CODE
:
1669 match
->wc
.masks
.tp_dst
= htons(0);
1670 match
->flow
.tp_dst
= htons(0);
1674 match
->wc
.masks
.tcp_flags
= htons(0);
1675 match
->flow
.tcp_flags
= htons(0);
1679 memset(&match
->wc
.masks
.nd_target
, 0,
1680 sizeof match
->wc
.masks
.nd_target
);
1681 memset(&match
->flow
.nd_target
, 0, sizeof match
->flow
.nd_target
);
1690 /* Makes 'match' match field 'mf' with the specified 'value' and 'mask'.
1691 * 'value' specifies a value to match and 'mask' specifies a wildcard pattern,
1692 * with a 1-bit indicating that the corresponding value bit must match and a
1693 * 0-bit indicating a don't-care.
1695 * If 'mask' is NULL or points to all-1-bits, then this call is equivalent to
1696 * mf_set_value(mf, value, match). If 'mask' points to all-0-bits, then this
1697 * call is equivalent to mf_set_wild(mf, match).
1699 * 'mask' must be a valid mask for 'mf' (see mf_is_mask_valid()). The caller
1700 * is responsible for ensuring that 'match' meets 'mf''s prerequisites.
1702 * If non-NULL, 'err_str' returns a malloc'ed string describing any errors
1703 * with the request or NULL if there is no error. The caller is reponsible
1704 * for freeing the string.
1706 * Return a set of enum ofputil_protocol bits (as an uint32_t to avoid circular
1707 * dependency on enum ofputil_protocol definition) indicating which OpenFlow
1708 * protocol versions can support this functionality. */
1710 mf_set(const struct mf_field
*mf
,
1711 const union mf_value
*value
, const union mf_value
*mask
,
1712 struct match
*match
, char **err_str
)
1714 if (!mask
|| is_all_ones(mask
, mf
->n_bytes
)) {
1715 mf_set_value(mf
, value
, match
, err_str
);
1716 return mf
->usable_protocols_exact
;
1717 } else if (is_all_zeros(mask
, mf
->n_bytes
) && !mf_is_tun_metadata(mf
)) {
1718 /* Tunnel metadata matches on the existence of the field itself, so
1719 * it still needs to be encoded even if the value is wildcarded. */
1720 mf_set_wild(mf
, match
, err_str
);
1721 return OFPUTIL_P_ANY
;
1733 case MFF_IN_PORT_OXM
:
1734 case MFF_ACTSET_OUTPUT
:
1735 case MFF_SKB_PRIORITY
:
1738 case MFF_DL_VLAN_PCP
:
1740 case MFF_MPLS_LABEL
:
1746 case MFF_IP_DSCP_SHIFTED
:
1749 case MFF_ICMPV4_TYPE
:
1750 case MFF_ICMPV4_CODE
:
1751 case MFF_ICMPV6_TYPE
:
1752 case MFF_ICMPV6_CODE
:
1753 return OFPUTIL_P_NONE
;
1756 match_set_dp_hash_masked(match
, ntohl(value
->be32
), ntohl(mask
->be32
));
1759 match_set_tun_id_masked(match
, value
->be64
, mask
->be64
);
1762 match_set_tun_src_masked(match
, value
->be32
, mask
->be32
);
1765 match_set_tun_dst_masked(match
, value
->be32
, mask
->be32
);
1768 match_set_tun_flags_masked(match
, ntohs(value
->be16
), ntohs(mask
->be16
));
1770 case MFF_TUN_GBP_ID
:
1771 match_set_tun_gbp_id_masked(match
, value
->be16
, mask
->be16
);
1773 case MFF_TUN_GBP_FLAGS
:
1774 match_set_tun_gbp_flags_masked(match
, value
->u8
, mask
->u8
);
1777 match_set_tun_ttl_masked(match
, value
->u8
, mask
->u8
);
1780 match_set_tun_tos_masked(match
, value
->u8
, mask
->u8
);
1782 CASE_MFF_TUN_METADATA
:
1783 tun_metadata_set_match(mf
, value
, mask
, match
, err_str
);
1787 match_set_metadata_masked(match
, value
->be64
, mask
->be64
);
1791 match_set_reg_masked(match
, mf
->id
- MFF_REG0
,
1792 ntohl(value
->be32
), ntohl(mask
->be32
));
1796 match_set_xreg_masked(match
, mf
->id
- MFF_XREG0
,
1797 ntohll(value
->be64
), ntohll(mask
->be64
));
1801 match_set_pkt_mark_masked(match
, ntohl(value
->be32
),
1806 match_set_ct_state_masked(match
, ntohl(value
->be32
), ntohl(mask
->be32
));
1810 match_set_ct_mark_masked(match
, ntohl(value
->be32
), ntohl(mask
->be32
));
1813 case MFF_CT_LABEL
: {
1814 ovs_u128 hlabel
, hmask
;
1816 ntoh128(&value
->be128
, &hlabel
);
1818 ntoh128(&mask
->be128
, &hmask
);
1820 hmask
.u64
.lo
= hmask
.u64
.hi
= UINT64_MAX
;
1822 match_set_ct_label_masked(match
, hlabel
, hmask
);
1827 match_set_dl_dst_masked(match
, value
->mac
, mask
->mac
);
1831 match_set_dl_src_masked(match
, value
->mac
, mask
->mac
);
1836 match_set_arp_sha_masked(match
, value
->mac
, mask
->mac
);
1841 match_set_arp_tha_masked(match
, value
->mac
, mask
->mac
);
1845 match_set_dl_tci_masked(match
, value
->be16
, mask
->be16
);
1849 match_set_vlan_vid_masked(match
, value
->be16
, mask
->be16
);
1853 match_set_nw_src_masked(match
, value
->be32
, mask
->be32
);
1857 match_set_nw_dst_masked(match
, value
->be32
, mask
->be32
);
1861 match_set_ipv6_src_masked(match
, &value
->ipv6
, &mask
->ipv6
);
1865 match_set_ipv6_dst_masked(match
, &value
->ipv6
, &mask
->ipv6
);
1868 case MFF_IPV6_LABEL
:
1869 if ((mask
->be32
& htonl(IPV6_LABEL_MASK
)) == htonl(IPV6_LABEL_MASK
)) {
1870 mf_set_value(mf
, value
, match
, err_str
);
1872 match_set_ipv6_label_masked(match
, value
->be32
, mask
->be32
);
1877 match_set_nd_target_masked(match
, &value
->ipv6
, &mask
->ipv6
);
1881 match_set_nw_frag_masked(match
, value
->u8
, mask
->u8
);
1885 match_set_nw_src_masked(match
, value
->be32
, mask
->be32
);
1889 match_set_nw_dst_masked(match
, value
->be32
, mask
->be32
);
1895 match_set_tp_src_masked(match
, value
->be16
, mask
->be16
);
1901 match_set_tp_dst_masked(match
, value
->be16
, mask
->be16
);
1905 match_set_tcp_flags_masked(match
, value
->be16
, mask
->be16
);
1913 return ((mf
->usable_protocols_bitwise
== mf
->usable_protocols_cidr
1914 || ip_is_cidr(mask
->be32
))
1915 ? mf
->usable_protocols_cidr
1916 : mf
->usable_protocols_bitwise
);
1920 mf_check__(const struct mf_subfield
*sf
, const struct flow
*flow
,
1924 VLOG_WARN_RL(&rl
, "unknown %s field", type
);
1925 return OFPERR_OFPBAC_BAD_SET_TYPE
;
1926 } else if (!sf
->n_bits
) {
1927 VLOG_WARN_RL(&rl
, "zero bit %s field %s", type
, sf
->field
->name
);
1928 return OFPERR_OFPBAC_BAD_SET_LEN
;
1929 } else if (sf
->ofs
>= sf
->field
->n_bits
) {
1930 VLOG_WARN_RL(&rl
, "bit offset %d exceeds %d-bit width of %s field %s",
1931 sf
->ofs
, sf
->field
->n_bits
, type
, sf
->field
->name
);
1932 return OFPERR_OFPBAC_BAD_SET_LEN
;
1933 } else if (sf
->ofs
+ sf
->n_bits
> sf
->field
->n_bits
) {
1934 VLOG_WARN_RL(&rl
, "bit offset %d and width %d exceeds %d-bit width "
1935 "of %s field %s", sf
->ofs
, sf
->n_bits
,
1936 sf
->field
->n_bits
, type
, sf
->field
->name
);
1937 return OFPERR_OFPBAC_BAD_SET_LEN
;
1938 } else if (flow
&& !mf_are_prereqs_ok(sf
->field
, flow
)) {
1939 VLOG_WARN_RL(&rl
, "%s field %s lacks correct prerequisites",
1940 type
, sf
->field
->name
);
1941 return OFPERR_OFPBAC_MATCH_INCONSISTENT
;
1947 /* Checks whether 'sf' is valid for reading a subfield out of 'flow'. Returns
1948 * 0 if so, otherwise an OpenFlow error code (e.g. as returned by
1951 mf_check_src(const struct mf_subfield
*sf
, const struct flow
*flow
)
1953 return mf_check__(sf
, flow
, "source");
1956 /* Checks whether 'sf' is valid for writing a subfield into 'flow'. Returns 0
1957 * if so, otherwise an OpenFlow error code (e.g. as returned by
1960 mf_check_dst(const struct mf_subfield
*sf
, const struct flow
*flow
)
1962 int error
= mf_check__(sf
, flow
, "destination");
1963 if (!error
&& !sf
->field
->writable
) {
1964 VLOG_WARN_RL(&rl
, "destination field %s is not writable",
1966 return OFPERR_OFPBAC_BAD_SET_ARGUMENT
;
1971 /* Copies the value and wildcard bit pattern for 'mf' from 'match' into the
1972 * 'value' and 'mask', respectively. */
1974 mf_get(const struct mf_field
*mf
, const struct match
*match
,
1975 union mf_value
*value
, union mf_value
*mask
)
1977 mf_get_value(mf
, &match
->flow
, value
);
1978 mf_get_mask(mf
, &match
->wc
, mask
);
1982 mf_from_integer_string(const struct mf_field
*mf
, const char *s
,
1983 uint8_t *valuep
, uint8_t *maskp
)
1986 const char *err_str
= "";
1989 err
= parse_int_string(s
, valuep
, mf
->n_bytes
, &tail
);
1990 if (err
|| (*tail
!= '\0' && *tail
!= '/')) {
1996 err
= parse_int_string(tail
+ 1, maskp
, mf
->n_bytes
, &tail
);
1997 if (err
|| *tail
!= '\0') {
2002 memset(maskp
, 0xff, mf
->n_bytes
);
2008 if (err
== ERANGE
) {
2009 return xasprintf("%s: %s too large for %u-byte field %s",
2010 s
, err_str
, mf
->n_bytes
, mf
->name
);
2012 return xasprintf("%s: bad syntax for %s %s", s
, mf
->name
, err_str
);
2017 mf_from_ethernet_string(const struct mf_field
*mf
, const char *s
,
2018 struct eth_addr
*mac
, struct eth_addr
*mask
)
2022 ovs_assert(mf
->n_bytes
== ETH_ADDR_LEN
);
2025 if (ovs_scan(s
, ETH_ADDR_SCAN_FMT
"%n", ETH_ADDR_SCAN_ARGS(*mac
), &n
)
2026 && n
== strlen(s
)) {
2027 *mask
= eth_addr_exact
;
2032 if (ovs_scan(s
, ETH_ADDR_SCAN_FMT
"/"ETH_ADDR_SCAN_FMT
"%n",
2033 ETH_ADDR_SCAN_ARGS(*mac
), ETH_ADDR_SCAN_ARGS(*mask
), &n
)
2034 && n
== strlen(s
)) {
2038 return xasprintf("%s: invalid Ethernet address", s
);
2042 mf_from_ipv4_string(const struct mf_field
*mf
, const char *s
,
2043 ovs_be32
*ip
, ovs_be32
*mask
)
2047 ovs_assert(mf
->n_bytes
== sizeof *ip
);
2049 if (ovs_scan(s
, IP_SCAN_FMT
"/"IP_SCAN_FMT
,
2050 IP_SCAN_ARGS(ip
), IP_SCAN_ARGS(mask
))) {
2052 } else if (ovs_scan(s
, IP_SCAN_FMT
"/%d", IP_SCAN_ARGS(ip
), &prefix
)) {
2053 if (prefix
<= 0 || prefix
> 32) {
2054 return xasprintf("%s: network prefix bits not between 0 and "
2057 *mask
= be32_prefix_mask(prefix
);
2058 } else if (ovs_scan(s
, IP_SCAN_FMT
, IP_SCAN_ARGS(ip
))) {
2059 *mask
= OVS_BE32_MAX
;
2061 return xasprintf("%s: invalid IP address", s
);
2067 mf_from_ipv6_string(const struct mf_field
*mf
, const char *s
,
2068 struct in6_addr
*value
, struct in6_addr
*mask
)
2070 char *str
= xstrdup(s
);
2071 char *save_ptr
= NULL
;
2072 const char *name
, *netmask
;
2075 ovs_assert(mf
->n_bytes
== sizeof *value
);
2077 name
= strtok_r(str
, "/", &save_ptr
);
2078 retval
= name
? lookup_ipv6(name
, value
) : EINVAL
;
2082 err
= xasprintf("%s: could not convert to IPv6 address", str
);
2088 netmask
= strtok_r(NULL
, "/", &save_ptr
);
2090 if (inet_pton(AF_INET6
, netmask
, mask
) != 1) {
2091 int prefix
= atoi(netmask
);
2092 if (prefix
<= 0 || prefix
> 128) {
2094 return xasprintf("%s: prefix bits not between 1 and 128", s
);
2096 *mask
= ipv6_create_mask(prefix
);
2100 *mask
= in6addr_exact
;
2108 mf_from_ofp_port_string(const struct mf_field
*mf
, const char *s
,
2109 ovs_be16
*valuep
, ovs_be16
*maskp
)
2113 ovs_assert(mf
->n_bytes
== sizeof(ovs_be16
));
2115 if (ofputil_port_from_string(s
, &port
)) {
2116 *valuep
= htons(ofp_to_u16(port
));
2117 *maskp
= OVS_BE16_MAX
;
2120 return xasprintf("%s: port value out of range for %s", s
, mf
->name
);
2124 mf_from_ofp_port_string32(const struct mf_field
*mf
, const char *s
,
2125 ovs_be32
*valuep
, ovs_be32
*maskp
)
2129 ovs_assert(mf
->n_bytes
== sizeof(ovs_be32
));
2130 if (ofputil_port_from_string(s
, &port
)) {
2131 *valuep
= ofputil_port_to_ofp11(port
);
2132 *maskp
= OVS_BE32_MAX
;
2135 return xasprintf("%s: port value out of range for %s", s
, mf
->name
);
2138 struct frag_handling
{
2144 static const struct frag_handling all_frags
[] = {
2145 #define A FLOW_NW_FRAG_ANY
2146 #define L FLOW_NW_FRAG_LATER
2147 /* name mask value */
2150 { "first", A
|L
, A
},
2151 { "later", A
|L
, A
|L
},
2156 { "not_later", L
, 0 },
2163 mf_from_frag_string(const char *s
, uint8_t *valuep
, uint8_t *maskp
)
2165 const struct frag_handling
*h
;
2167 for (h
= all_frags
; h
< &all_frags
[ARRAY_SIZE(all_frags
)]; h
++) {
2168 if (!strcasecmp(s
, h
->name
)) {
2169 /* We force the upper bits of the mask on to make mf_parse_value()
2170 * happy (otherwise it will never think it's an exact match.) */
2171 *maskp
= h
->mask
| ~FLOW_NW_FRAG_MASK
;
2177 return xasprintf("%s: unknown fragment type (valid types are \"no\", "
2178 "\"yes\", \"first\", \"later\", \"not_first\"", s
);
2182 parse_mf_flags(const char *s
, const char *(*bit_to_string
)(uint32_t),
2183 const char *field_name
, ovs_be16
*flagsp
, ovs_be16 allowed
,
2188 uint32_t flags
, mask
;
2190 err
= parse_flags(s
, bit_to_string
, '\0', field_name
, &err_str
,
2191 &flags
, ntohs(allowed
), maskp
? &mask
: NULL
);
2196 *flagsp
= htons(flags
);
2198 *maskp
= htons(mask
);
2205 mf_from_tcp_flags_string(const char *s
, ovs_be16
*flagsp
, ovs_be16
*maskp
)
2207 return parse_mf_flags(s
, packet_tcp_flag_to_string
, "TCP", flagsp
,
2208 TCP_FLAGS_BE16(OVS_BE16_MAX
), maskp
);
2212 mf_from_tun_flags_string(const char *s
, ovs_be16
*flagsp
, ovs_be16
*maskp
)
2214 return parse_mf_flags(s
, flow_tun_flag_to_string
, "tunnel", flagsp
,
2215 htons(FLOW_TNL_PUB_F_MASK
), maskp
);
2219 mf_from_ct_state_string(const char *s
, ovs_be32
*flagsp
, ovs_be32
*maskp
)
2223 uint32_t flags
, mask
;
2225 err
= parse_flags(s
, ct_state_to_string
, '\0', "ct_state", &err_str
,
2226 &flags
, CS_SUPPORTED_MASK
, maskp
? &mask
: NULL
);
2231 *flagsp
= htonl(flags
);
2233 *maskp
= htonl(mask
);
2239 /* Parses 's', a string value for field 'mf', into 'value' and 'mask'. Returns
2240 * NULL if successful, otherwise a malloc()'d string describing the error. */
2242 mf_parse(const struct mf_field
*mf
, const char *s
,
2243 union mf_value
*value
, union mf_value
*mask
)
2247 if (!strcmp(s
, "*")) {
2248 memset(value
, 0, mf
->n_bytes
);
2249 memset(mask
, 0, mf
->n_bytes
);
2253 switch (mf
->string
) {
2255 case MFS_HEXADECIMAL
:
2256 error
= mf_from_integer_string(mf
, s
,
2257 (uint8_t *) value
, (uint8_t *) mask
);
2261 ovs_assert(mf
->n_bytes
== sizeof(ovs_be32
));
2262 error
= mf_from_ct_state_string(s
, &value
->be32
, &mask
->be32
);
2266 error
= mf_from_ethernet_string(mf
, s
, &value
->mac
, &mask
->mac
);
2270 error
= mf_from_ipv4_string(mf
, s
, &value
->be32
, &mask
->be32
);
2274 error
= mf_from_ipv6_string(mf
, s
, &value
->ipv6
, &mask
->ipv6
);
2278 error
= mf_from_ofp_port_string(mf
, s
, &value
->be16
, &mask
->be16
);
2281 case MFS_OFP_PORT_OXM
:
2282 error
= mf_from_ofp_port_string32(mf
, s
, &value
->be32
, &mask
->be32
);
2286 error
= mf_from_frag_string(s
, &value
->u8
, &mask
->u8
);
2290 ovs_assert(mf
->n_bytes
== sizeof(ovs_be16
));
2291 error
= mf_from_tun_flags_string(s
, &value
->be16
, &mask
->be16
);
2295 ovs_assert(mf
->n_bytes
== sizeof(ovs_be16
));
2296 error
= mf_from_tcp_flags_string(s
, &value
->be16
, &mask
->be16
);
2303 if (!error
&& !mf_is_mask_valid(mf
, mask
)) {
2304 error
= xasprintf("%s: invalid mask for field %s", s
, mf
->name
);
2309 /* Parses 's', a string value for field 'mf', into 'value'. Returns NULL if
2310 * successful, otherwise a malloc()'d string describing the error. */
2312 mf_parse_value(const struct mf_field
*mf
, const char *s
, union mf_value
*value
)
2314 union mf_value mask
;
2317 error
= mf_parse(mf
, s
, value
, &mask
);
2322 if (!is_all_ones((const uint8_t *) &mask
, mf
->n_bytes
)) {
2323 return xasprintf("%s: wildcards not allowed here", s
);
2329 mf_format_integer_string(const struct mf_field
*mf
, const uint8_t *valuep
,
2330 const uint8_t *maskp
, struct ds
*s
)
2332 if (mf
->string
== MFS_HEXADECIMAL
) {
2333 ds_put_hex(s
, valuep
, mf
->n_bytes
);
2335 unsigned long long int integer
= 0;
2338 ovs_assert(mf
->n_bytes
<= 8);
2339 for (i
= 0; i
< mf
->n_bytes
; i
++) {
2340 integer
= (integer
<< 8) | valuep
[i
];
2342 ds_put_format(s
, "%lld", integer
);
2346 /* I guess we could write the mask in decimal for MFS_DECIMAL but I'm
2347 * not sure that that a bit-mask written in decimal is ever easier to
2348 * understand than the same bit-mask written in hexadecimal. */
2349 ds_put_char(s
, '/');
2350 ds_put_hex(s
, maskp
, mf
->n_bytes
);
2355 mf_format_frag_string(uint8_t value
, uint8_t mask
, struct ds
*s
)
2357 const struct frag_handling
*h
;
2359 mask
&= FLOW_NW_FRAG_MASK
;
2362 for (h
= all_frags
; h
< &all_frags
[ARRAY_SIZE(all_frags
)]; h
++) {
2363 if (value
== h
->value
&& mask
== h
->mask
) {
2364 ds_put_cstr(s
, h
->name
);
2368 ds_put_cstr(s
, "<error>");
2372 mf_format_tnl_flags_string(ovs_be16 value
, ovs_be16 mask
, struct ds
*s
)
2374 format_flags_masked(s
, NULL
, flow_tun_flag_to_string
, ntohs(value
),
2375 ntohs(mask
) & FLOW_TNL_PUB_F_MASK
, FLOW_TNL_PUB_F_MASK
);
2379 mf_format_tcp_flags_string(ovs_be16 value
, ovs_be16 mask
, struct ds
*s
)
2381 format_flags_masked(s
, NULL
, packet_tcp_flag_to_string
, ntohs(value
),
2382 TCP_FLAGS(mask
), TCP_FLAGS(OVS_BE16_MAX
));
2386 mf_format_ct_state_string(ovs_be32 value
, ovs_be32 mask
, struct ds
*s
)
2388 format_flags_masked(s
, NULL
, ct_state_to_string
, ntohl(value
),
2389 ntohl(mask
), UINT16_MAX
);
2392 /* Appends to 's' a string representation of field 'mf' whose value is in
2393 * 'value' and 'mask'. 'mask' may be NULL to indicate an exact match. */
2395 mf_format(const struct mf_field
*mf
,
2396 const union mf_value
*value
, const union mf_value
*mask
,
2400 if (is_all_zeros(mask
, mf
->n_bytes
)) {
2401 ds_put_cstr(s
, "ANY");
2403 } else if (is_all_ones(mask
, mf
->n_bytes
)) {
2408 switch (mf
->string
) {
2409 case MFS_OFP_PORT_OXM
:
2412 ofputil_port_from_ofp11(value
->be32
, &port
);
2413 ofputil_format_port(port
, s
);
2419 ofputil_format_port(u16_to_ofp(ntohs(value
->be16
)), s
);
2424 case MFS_HEXADECIMAL
:
2425 mf_format_integer_string(mf
, (uint8_t *) value
, (uint8_t *) mask
, s
);
2429 mf_format_ct_state_string(value
->be32
,
2430 mask
? mask
->be32
: OVS_BE32_MAX
, s
);
2434 eth_format_masked(value
->mac
, mask
? &mask
->mac
: NULL
, s
);
2438 ip_format_masked(value
->be32
, mask
? mask
->be32
: OVS_BE32_MAX
, s
);
2442 print_ipv6_masked(s
, &value
->ipv6
, mask
? &mask
->ipv6
: NULL
);
2446 mf_format_frag_string(value
->u8
, mask
? mask
->u8
: UINT8_MAX
, s
);
2450 mf_format_tnl_flags_string(value
->be16
,
2451 mask
? mask
->be16
: OVS_BE16_MAX
, s
);
2455 mf_format_tcp_flags_string(value
->be16
,
2456 mask
? mask
->be16
: OVS_BE16_MAX
, s
);
2464 /* Makes subfield 'sf' within 'flow' exactly match the 'sf->n_bits'
2465 * least-significant bits in 'x'.
2468 mf_write_subfield_flow(const struct mf_subfield
*sf
,
2469 const union mf_subvalue
*x
, struct flow
*flow
)
2471 const struct mf_field
*field
= sf
->field
;
2472 union mf_value value
;
2474 mf_get_value(field
, flow
, &value
);
2475 bitwise_copy(x
, sizeof *x
, 0, &value
, field
->n_bytes
,
2476 sf
->ofs
, sf
->n_bits
);
2477 mf_set_flow_value(field
, &value
, flow
);
2480 /* Makes subfield 'sf' within 'match' exactly match the 'sf->n_bits'
2481 * least-significant bits in 'x'.
2484 mf_write_subfield(const struct mf_subfield
*sf
, const union mf_subvalue
*x
,
2485 struct match
*match
)
2487 const struct mf_field
*field
= sf
->field
;
2488 union mf_value value
, mask
;
2490 mf_get(field
, match
, &value
, &mask
);
2491 bitwise_copy(x
, sizeof *x
, 0, &value
, field
->n_bytes
, sf
->ofs
, sf
->n_bits
);
2492 bitwise_one ( &mask
, field
->n_bytes
, sf
->ofs
, sf
->n_bits
);
2493 mf_set(field
, &value
, &mask
, match
, NULL
);
2496 /* 'v' and 'm' correspond to values of 'field'. This function copies them into
2497 * 'match' in the correspond positions. */
2499 mf_mask_subfield(const struct mf_field
*field
,
2500 const union mf_subvalue
*v
,
2501 const union mf_subvalue
*m
,
2502 struct match
*match
)
2504 union mf_value value
, mask
;
2506 mf_get(field
, match
, &value
, &mask
);
2507 bitwise_copy(v
, sizeof *v
, 0, &value
, field
->n_bytes
, 0, field
->n_bits
);
2508 bitwise_copy(m
, sizeof *m
, 0, &mask
, field
->n_bytes
, 0, field
->n_bits
);
2509 mf_set(field
, &value
, &mask
, match
, NULL
);
2512 /* Initializes 'x' to the value of 'sf' within 'flow'. 'sf' must be valid for
2513 * reading 'flow', e.g. as checked by mf_check_src(). */
2515 mf_read_subfield(const struct mf_subfield
*sf
, const struct flow
*flow
,
2516 union mf_subvalue
*x
)
2518 union mf_value value
;
2520 mf_get_value(sf
->field
, flow
, &value
);
2522 memset(x
, 0, sizeof *x
);
2523 bitwise_copy(&value
, sf
->field
->n_bytes
, sf
->ofs
,
2528 /* Returns the value of 'sf' within 'flow'. 'sf' must be valid for reading
2529 * 'flow', e.g. as checked by mf_check_src() and sf->n_bits must be 64 or
2532 mf_get_subfield(const struct mf_subfield
*sf
, const struct flow
*flow
)
2534 union mf_value value
;
2536 mf_get_value(sf
->field
, flow
, &value
);
2537 return bitwise_get(&value
, sf
->field
->n_bytes
, sf
->ofs
, sf
->n_bits
);
2541 mf_format_subvalue(const union mf_subvalue
*subvalue
, struct ds
*s
)
2543 ds_put_hex(s
, subvalue
->u8
, sizeof subvalue
->u8
);
2547 field_array_set(enum mf_field_id id
, const union mf_value
*value
,
2548 struct field_array
*fa
)
2550 ovs_assert(id
< MFF_N_IDS
);
2551 bitmap_set1(fa
->used
.bm
, id
);
2552 fa
->value
[id
] = *value
;