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 union mf_value value
;
208 tun_metadata_read(&wc
->masks
.tunnel
, mf
, &value
);
209 return is_all_zeros(&value
.tun_metadata
, mf
->n_bytes
);
212 return !wc
->masks
.metadata
;
214 case MFF_IN_PORT_OXM
:
215 return !wc
->masks
.in_port
.ofp_port
;
216 case MFF_SKB_PRIORITY
:
217 return !wc
->masks
.skb_priority
;
219 return !wc
->masks
.pkt_mark
;
221 return !wc
->masks
.regs
[mf
->id
- MFF_REG0
];
223 return !flow_get_xreg(&wc
->masks
, mf
->id
- MFF_XREG0
);
224 case MFF_ACTSET_OUTPUT
:
225 return !wc
->masks
.actset_output
;
228 return eth_addr_is_zero(wc
->masks
.dl_src
);
230 return eth_addr_is_zero(wc
->masks
.dl_dst
);
232 return !wc
->masks
.dl_type
;
236 return eth_addr_is_zero(wc
->masks
.arp_sha
);
240 return eth_addr_is_zero(wc
->masks
.arp_tha
);
243 return !wc
->masks
.vlan_tci
;
245 return !(wc
->masks
.vlan_tci
& htons(VLAN_VID_MASK
));
247 return !(wc
->masks
.vlan_tci
& htons(VLAN_VID_MASK
| VLAN_CFI
));
248 case MFF_DL_VLAN_PCP
:
250 return !(wc
->masks
.vlan_tci
& htons(VLAN_PCP_MASK
));
253 return !(wc
->masks
.mpls_lse
[0] & htonl(MPLS_LABEL_MASK
));
255 return !(wc
->masks
.mpls_lse
[0] & htonl(MPLS_TC_MASK
));
257 return !(wc
->masks
.mpls_lse
[0] & htonl(MPLS_BOS_MASK
));
260 return !wc
->masks
.nw_src
;
262 return !wc
->masks
.nw_dst
;
265 return ipv6_mask_is_any(&wc
->masks
.ipv6_src
);
267 return ipv6_mask_is_any(&wc
->masks
.ipv6_dst
);
270 return !wc
->masks
.ipv6_label
;
273 return !wc
->masks
.nw_proto
;
275 case MFF_IP_DSCP_SHIFTED
:
276 return !(wc
->masks
.nw_tos
& IP_DSCP_MASK
);
278 return !(wc
->masks
.nw_tos
& IP_ECN_MASK
);
280 return !wc
->masks
.nw_ttl
;
283 return ipv6_mask_is_any(&wc
->masks
.nd_target
);
286 return !(wc
->masks
.nw_frag
& FLOW_NW_FRAG_MASK
);
289 return !wc
->masks
.nw_proto
;
291 return !wc
->masks
.nw_src
;
293 return !wc
->masks
.nw_dst
;
298 case MFF_ICMPV4_TYPE
:
299 case MFF_ICMPV6_TYPE
:
300 return !wc
->masks
.tp_src
;
304 case MFF_ICMPV4_CODE
:
305 case MFF_ICMPV6_CODE
:
306 return !wc
->masks
.tp_dst
;
308 return !wc
->masks
.tcp_flags
;
316 /* Initializes 'mask' with the wildcard bit pattern for field 'mf' within 'wc'.
317 * Each bit in 'mask' will be set to 1 if the bit is significant for matching
318 * purposes, or to 0 if it is wildcarded.
320 * The caller is responsible for ensuring that 'wc' corresponds to a flow that
321 * meets 'mf''s prerequisites. */
323 mf_get_mask(const struct mf_field
*mf
, const struct flow_wildcards
*wc
,
324 union mf_value
*mask
)
326 mf_get_value(mf
, &wc
->masks
, mask
);
329 /* Tests whether 'mask' is a valid wildcard bit pattern for 'mf'. Returns true
330 * if the mask is valid, false otherwise. */
332 mf_is_mask_valid(const struct mf_field
*mf
, const union mf_value
*mask
)
334 switch (mf
->maskable
) {
336 return (is_all_zeros(mask
, mf
->n_bytes
) ||
337 is_all_ones(mask
, mf
->n_bytes
));
346 /* Returns true if 'flow' meets the prerequisites for 'mf', false otherwise. */
348 mf_are_prereqs_ok(const struct mf_field
*mf
, const struct flow
*flow
)
350 switch (mf
->prereqs
) {
355 return (flow
->dl_type
== htons(ETH_TYPE_ARP
) ||
356 flow
->dl_type
== htons(ETH_TYPE_RARP
));
358 return flow
->dl_type
== htons(ETH_TYPE_IP
);
360 return flow
->dl_type
== htons(ETH_TYPE_IPV6
);
362 return (flow
->vlan_tci
& htons(VLAN_CFI
)) != 0;
364 return eth_type_mpls(flow
->dl_type
);
366 return is_ip_any(flow
);
369 return is_ip_any(flow
) && flow
->nw_proto
== IPPROTO_TCP
370 && !(flow
->nw_frag
& FLOW_NW_FRAG_LATER
);
372 return is_ip_any(flow
) && flow
->nw_proto
== IPPROTO_UDP
373 && !(flow
->nw_frag
& FLOW_NW_FRAG_LATER
);
375 return is_ip_any(flow
) && flow
->nw_proto
== IPPROTO_SCTP
376 && !(flow
->nw_frag
& FLOW_NW_FRAG_LATER
);
378 return is_icmpv4(flow
);
380 return is_icmpv6(flow
);
383 return (is_icmpv6(flow
)
384 && flow
->tp_dst
== htons(0)
385 && (flow
->tp_src
== htons(ND_NEIGHBOR_SOLICIT
) ||
386 flow
->tp_src
== htons(ND_NEIGHBOR_ADVERT
)));
388 return (is_icmpv6(flow
)
389 && flow
->tp_dst
== htons(0)
390 && (flow
->tp_src
== htons(ND_NEIGHBOR_SOLICIT
)));
392 return (is_icmpv6(flow
)
393 && flow
->tp_dst
== htons(0)
394 && (flow
->tp_src
== htons(ND_NEIGHBOR_ADVERT
)));
400 /* Set field and it's prerequisities in the mask.
401 * This is only ever called for writeable 'mf's, but we do not make the
402 * distinction here. */
404 mf_mask_field_and_prereqs(const struct mf_field
*mf
, struct flow_wildcards
*wc
)
406 mf_set_flow_value(mf
, &exact_match_mask
, &wc
->masks
);
408 switch (mf
->prereqs
) {
412 WC_MASK_FIELD(wc
, tp_src
);
413 WC_MASK_FIELD(wc
, tp_dst
);
420 /* nw_frag always unwildcarded. */
421 WC_MASK_FIELD(wc
, nw_proto
);
428 /* dl_type always unwildcarded. */
431 WC_MASK_FIELD_MASK(wc
, vlan_tci
, htons(VLAN_CFI
));
438 /* Set bits of 'bm' corresponding to the field 'mf' and it's prerequisities. */
440 mf_bitmap_set_field_and_prereqs(const struct mf_field
*mf
, struct mf_bitmap
*bm
)
442 bitmap_set1(bm
->bm
, mf
->id
);
444 switch (mf
->prereqs
) {
448 bitmap_set1(bm
->bm
, MFF_TCP_SRC
);
449 bitmap_set1(bm
->bm
, MFF_TCP_DST
);
456 /* nw_frag always unwildcarded. */
457 bitmap_set1(bm
->bm
, MFF_IP_PROTO
);
464 bitmap_set1(bm
->bm
, MFF_ETH_TYPE
);
467 bitmap_set1(bm
->bm
, MFF_VLAN_TCI
);
474 /* Returns true if 'value' may be a valid value *as part of a masked match*,
477 * A value is not rejected just because it is not valid for the field in
478 * question, but only if it doesn't make sense to test the bits in question at
479 * all. For example, the MFF_VLAN_TCI field will never have a nonzero value
480 * without the VLAN_CFI bit being set, but we can't reject those values because
481 * it is still legitimate to test just for those bits (see the documentation
482 * for NXM_OF_VLAN_TCI in nicira-ext.h). On the other hand, there is never a
483 * reason to set the low bit of MFF_IP_DSCP to 1, so we reject that. */
485 mf_is_value_valid(const struct mf_field
*mf
, const union mf_value
*value
)
497 case MFF_TUN_GBP_FLAGS
:
498 CASE_MFF_TUN_METADATA
:
501 case MFF_SKB_PRIORITY
:
525 case MFF_ICMPV4_TYPE
:
526 case MFF_ICMPV4_CODE
:
527 case MFF_ICMPV6_TYPE
:
528 case MFF_ICMPV6_CODE
:
534 case MFF_IN_PORT_OXM
:
535 case MFF_ACTSET_OUTPUT
: {
537 return !ofputil_port_from_ofp11(value
->be32
, &port
);
541 return !(value
->u8
& ~IP_DSCP_MASK
);
542 case MFF_IP_DSCP_SHIFTED
:
543 return !(value
->u8
& (~IP_DSCP_MASK
>> 2));
545 return !(value
->u8
& ~IP_ECN_MASK
);
547 return !(value
->u8
& ~FLOW_NW_FRAG_MASK
);
549 return !(value
->be16
& ~htons(0x0fff));
552 return !(value
->be16
& htons(0xff00));
555 return !(value
->be16
& htons(VLAN_CFI
| VLAN_PCP_MASK
));
557 return !(value
->be16
& htons(VLAN_PCP_MASK
));
559 case MFF_DL_VLAN_PCP
:
561 return !(value
->u8
& ~(VLAN_PCP_MASK
>> VLAN_PCP_SHIFT
));
564 return !(value
->be32
& ~htonl(IPV6_LABEL_MASK
));
567 return !(value
->be32
& ~htonl(MPLS_LABEL_MASK
>> MPLS_LABEL_SHIFT
));
570 return !(value
->u8
& ~(MPLS_TC_MASK
>> MPLS_TC_SHIFT
));
573 return !(value
->u8
& ~(MPLS_BOS_MASK
>> MPLS_BOS_SHIFT
));
576 return !(value
->be16
& ~htons(FLOW_TNL_PUB_F_MASK
));
584 /* Copies the value of field 'mf' from 'flow' into 'value'. The caller is
585 * responsible for ensuring that 'flow' meets 'mf''s prerequisites. */
587 mf_get_value(const struct mf_field
*mf
, const struct flow
*flow
,
588 union mf_value
*value
)
592 value
->be32
= htonl(flow
->dp_hash
);
595 value
->be32
= htonl(flow
->recirc_id
);
598 value
->be32
= htonl(flow
->conj_id
);
601 value
->be64
= flow
->tunnel
.tun_id
;
604 value
->be32
= flow
->tunnel
.ip_src
;
607 value
->be32
= flow
->tunnel
.ip_dst
;
610 value
->be16
= htons(flow
->tunnel
.flags
& FLOW_TNL_PUB_F_MASK
);
613 value
->be16
= flow
->tunnel
.gbp_id
;
615 case MFF_TUN_GBP_FLAGS
:
616 value
->u8
= flow
->tunnel
.gbp_flags
;
619 value
->u8
= flow
->tunnel
.ip_ttl
;
622 value
->u8
= flow
->tunnel
.ip_tos
;
624 CASE_MFF_TUN_METADATA
:
625 tun_metadata_read(&flow
->tunnel
, mf
, value
);
629 value
->be64
= flow
->metadata
;
633 value
->be16
= htons(ofp_to_u16(flow
->in_port
.ofp_port
));
635 case MFF_IN_PORT_OXM
:
636 value
->be32
= ofputil_port_to_ofp11(flow
->in_port
.ofp_port
);
638 case MFF_ACTSET_OUTPUT
:
639 value
->be32
= ofputil_port_to_ofp11(flow
->actset_output
);
642 case MFF_SKB_PRIORITY
:
643 value
->be32
= htonl(flow
->skb_priority
);
647 value
->be32
= htonl(flow
->pkt_mark
);
651 value
->be32
= htonl(flow
->regs
[mf
->id
- MFF_REG0
]);
655 value
->be64
= htonll(flow_get_xreg(flow
, mf
->id
- MFF_XREG0
));
659 value
->mac
= flow
->dl_src
;
663 value
->mac
= flow
->dl_dst
;
667 value
->be16
= flow
->dl_type
;
671 value
->be16
= flow
->vlan_tci
;
675 value
->be16
= flow
->vlan_tci
& htons(VLAN_VID_MASK
);
678 value
->be16
= flow
->vlan_tci
& htons(VLAN_VID_MASK
| VLAN_CFI
);
681 case MFF_DL_VLAN_PCP
:
683 value
->u8
= vlan_tci_to_pcp(flow
->vlan_tci
);
687 value
->be32
= htonl(mpls_lse_to_label(flow
->mpls_lse
[0]));
691 value
->u8
= mpls_lse_to_tc(flow
->mpls_lse
[0]);
695 value
->u8
= mpls_lse_to_bos(flow
->mpls_lse
[0]);
699 value
->be32
= flow
->nw_src
;
703 value
->be32
= flow
->nw_dst
;
707 value
->ipv6
= flow
->ipv6_src
;
711 value
->ipv6
= flow
->ipv6_dst
;
715 value
->be32
= flow
->ipv6_label
;
719 value
->u8
= flow
->nw_proto
;
723 value
->u8
= flow
->nw_tos
& IP_DSCP_MASK
;
726 case MFF_IP_DSCP_SHIFTED
:
727 value
->u8
= flow
->nw_tos
>> 2;
731 value
->u8
= flow
->nw_tos
& IP_ECN_MASK
;
735 value
->u8
= flow
->nw_ttl
;
739 value
->u8
= flow
->nw_frag
;
743 value
->be16
= htons(flow
->nw_proto
);
747 value
->be32
= flow
->nw_src
;
751 value
->be32
= flow
->nw_dst
;
756 value
->mac
= flow
->arp_sha
;
761 value
->mac
= flow
->arp_tha
;
767 value
->be16
= flow
->tp_src
;
773 value
->be16
= flow
->tp_dst
;
777 value
->be16
= flow
->tcp_flags
;
780 case MFF_ICMPV4_TYPE
:
781 case MFF_ICMPV6_TYPE
:
782 value
->u8
= ntohs(flow
->tp_src
);
785 case MFF_ICMPV4_CODE
:
786 case MFF_ICMPV6_CODE
:
787 value
->u8
= ntohs(flow
->tp_dst
);
791 value
->ipv6
= flow
->nd_target
;
800 /* Makes 'match' match field 'mf' exactly, with the value matched taken from
801 * 'value'. The caller is responsible for ensuring that 'match' meets 'mf''s
804 mf_set_value(const struct mf_field
*mf
,
805 const union mf_value
*value
, struct match
*match
)
809 match_set_dp_hash(match
, ntohl(value
->be32
));
812 match_set_recirc_id(match
, ntohl(value
->be32
));
815 match_set_conj_id(match
, ntohl(value
->be32
));
818 match_set_tun_id(match
, value
->be64
);
821 match_set_tun_src(match
, value
->be32
);
824 match_set_tun_dst(match
, value
->be32
);
827 match_set_tun_flags(match
, ntohs(value
->be16
));
830 match_set_tun_gbp_id(match
, value
->be16
);
832 case MFF_TUN_GBP_FLAGS
:
833 match_set_tun_gbp_flags(match
, value
->u8
);
836 match_set_tun_tos(match
, value
->u8
);
839 match_set_tun_ttl(match
, value
->u8
);
841 CASE_MFF_TUN_METADATA
:
842 tun_metadata_set_match(mf
, value
, NULL
, match
);
846 match_set_metadata(match
, value
->be64
);
850 match_set_in_port(match
, u16_to_ofp(ntohs(value
->be16
)));
853 case MFF_IN_PORT_OXM
: {
855 ofputil_port_from_ofp11(value
->be32
, &port
);
856 match_set_in_port(match
, port
);
859 case MFF_ACTSET_OUTPUT
: {
861 ofputil_port_from_ofp11(value
->be32
, &port
);
862 match_set_actset_output(match
, port
);
866 case MFF_SKB_PRIORITY
:
867 match_set_skb_priority(match
, ntohl(value
->be32
));
871 match_set_pkt_mark(match
, ntohl(value
->be32
));
875 match_set_reg(match
, mf
->id
- MFF_REG0
, ntohl(value
->be32
));
879 match_set_xreg(match
, mf
->id
- MFF_XREG0
, ntohll(value
->be64
));
883 match_set_dl_src(match
, value
->mac
);
887 match_set_dl_dst(match
, value
->mac
);
891 match_set_dl_type(match
, value
->be16
);
895 match_set_dl_tci(match
, value
->be16
);
899 match_set_dl_vlan(match
, value
->be16
);
902 match_set_vlan_vid(match
, value
->be16
);
905 case MFF_DL_VLAN_PCP
:
907 match_set_dl_vlan_pcp(match
, value
->u8
);
911 match_set_mpls_label(match
, 0, value
->be32
);
915 match_set_mpls_tc(match
, 0, value
->u8
);
919 match_set_mpls_bos(match
, 0, value
->u8
);
923 match_set_nw_src(match
, value
->be32
);
927 match_set_nw_dst(match
, value
->be32
);
931 match_set_ipv6_src(match
, &value
->ipv6
);
935 match_set_ipv6_dst(match
, &value
->ipv6
);
939 match_set_ipv6_label(match
, value
->be32
);
943 match_set_nw_proto(match
, value
->u8
);
947 match_set_nw_dscp(match
, value
->u8
);
950 case MFF_IP_DSCP_SHIFTED
:
951 match_set_nw_dscp(match
, value
->u8
<< 2);
955 match_set_nw_ecn(match
, value
->u8
);
959 match_set_nw_ttl(match
, value
->u8
);
963 match_set_nw_frag(match
, value
->u8
);
967 match_set_nw_proto(match
, ntohs(value
->be16
));
971 match_set_nw_src(match
, value
->be32
);
975 match_set_nw_dst(match
, value
->be32
);
980 match_set_arp_sha(match
, value
->mac
);
985 match_set_arp_tha(match
, value
->mac
);
991 match_set_tp_src(match
, value
->be16
);
997 match_set_tp_dst(match
, value
->be16
);
1001 match_set_tcp_flags(match
, value
->be16
);
1004 case MFF_ICMPV4_TYPE
:
1005 case MFF_ICMPV6_TYPE
:
1006 match_set_icmp_type(match
, value
->u8
);
1009 case MFF_ICMPV4_CODE
:
1010 case MFF_ICMPV6_CODE
:
1011 match_set_icmp_code(match
, value
->u8
);
1015 match_set_nd_target(match
, &value
->ipv6
);
1024 /* Unwildcard 'mask' member field described by 'mf'. The caller is
1025 * responsible for ensuring that 'mask' meets 'mf''s prerequisites. */
1027 mf_mask_field(const struct mf_field
*mf
, struct flow
*mask
)
1029 /* For MFF_DL_VLAN, we cannot send a all 1's to flow_set_dl_vlan()
1030 * as that will be considered as OFP10_VLAN_NONE. So consider it as a
1031 * special case. For the rest, calling mf_set_flow_value() is good
1033 if (mf
->id
== MFF_DL_VLAN
) {
1034 flow_set_dl_vlan(mask
, htons(VLAN_VID_MASK
));
1036 mf_set_flow_value(mf
, &exact_match_mask
, mask
);
1041 field_len(const struct mf_field
*mf
, const union mf_value
*value_
)
1043 const uint8_t *value
= &value_
->u8
;
1046 if (!mf
->variable_len
) {
1054 for (i
= 0; i
< mf
->n_bytes
; i
++) {
1055 if (value
[i
] != 0) {
1060 return mf
->n_bytes
- i
;
1063 /* Returns the effective length of the field. For fixed length fields,
1064 * this is just the defined length. For variable length fields, it is
1065 * the minimum size encoding that retains the same meaning (i.e.
1066 * discarding leading zeros). */
1068 mf_field_len(const struct mf_field
*mf
, const union mf_value
*value
,
1069 const union mf_value
*mask
)
1073 len
= field_len(mf
, value
);
1074 if (mask
&& !is_all_ones(mask
, mf
->n_bytes
)) {
1075 mask_len
= field_len(mf
, mask
);
1076 len
= MAX(len
, mask_len
);
1082 /* Sets 'flow' member field described by 'mf' to 'value'. The caller is
1083 * responsible for ensuring that 'flow' meets 'mf''s prerequisites.*/
1085 mf_set_flow_value(const struct mf_field
*mf
,
1086 const union mf_value
*value
, struct flow
*flow
)
1090 flow
->dp_hash
= ntohl(value
->be32
);
1093 flow
->recirc_id
= ntohl(value
->be32
);
1096 flow
->conj_id
= ntohl(value
->be32
);
1099 flow
->tunnel
.tun_id
= value
->be64
;
1102 flow
->tunnel
.ip_src
= value
->be32
;
1105 flow
->tunnel
.ip_dst
= value
->be32
;
1108 flow
->tunnel
.flags
= (flow
->tunnel
.flags
& ~FLOW_TNL_PUB_F_MASK
) |
1111 case MFF_TUN_GBP_ID
:
1112 flow
->tunnel
.gbp_id
= value
->be16
;
1114 case MFF_TUN_GBP_FLAGS
:
1115 flow
->tunnel
.gbp_flags
= value
->u8
;
1118 flow
->tunnel
.ip_tos
= value
->u8
;
1121 flow
->tunnel
.ip_ttl
= value
->u8
;
1123 CASE_MFF_TUN_METADATA
:
1124 tun_metadata_write(&flow
->tunnel
, mf
, value
);
1127 flow
->metadata
= value
->be64
;
1131 flow
->in_port
.ofp_port
= u16_to_ofp(ntohs(value
->be16
));
1134 case MFF_IN_PORT_OXM
:
1135 ofputil_port_from_ofp11(value
->be32
, &flow
->in_port
.ofp_port
);
1137 case MFF_ACTSET_OUTPUT
:
1138 ofputil_port_from_ofp11(value
->be32
, &flow
->actset_output
);
1141 case MFF_SKB_PRIORITY
:
1142 flow
->skb_priority
= ntohl(value
->be32
);
1146 flow
->pkt_mark
= ntohl(value
->be32
);
1150 flow
->regs
[mf
->id
- MFF_REG0
] = ntohl(value
->be32
);
1154 flow_set_xreg(flow
, mf
->id
- MFF_XREG0
, ntohll(value
->be64
));
1158 flow
->dl_src
= value
->mac
;
1162 flow
->dl_dst
= value
->mac
;
1166 flow
->dl_type
= value
->be16
;
1170 flow
->vlan_tci
= value
->be16
;
1174 flow_set_dl_vlan(flow
, value
->be16
);
1177 flow_set_vlan_vid(flow
, value
->be16
);
1180 case MFF_DL_VLAN_PCP
:
1182 flow_set_vlan_pcp(flow
, value
->u8
);
1185 case MFF_MPLS_LABEL
:
1186 flow_set_mpls_label(flow
, 0, value
->be32
);
1190 flow_set_mpls_tc(flow
, 0, value
->u8
);
1194 flow_set_mpls_bos(flow
, 0, value
->u8
);
1198 flow
->nw_src
= value
->be32
;
1202 flow
->nw_dst
= value
->be32
;
1206 flow
->ipv6_src
= value
->ipv6
;
1210 flow
->ipv6_dst
= value
->ipv6
;
1213 case MFF_IPV6_LABEL
:
1214 flow
->ipv6_label
= value
->be32
& htonl(IPV6_LABEL_MASK
);
1218 flow
->nw_proto
= value
->u8
;
1222 flow
->nw_tos
&= ~IP_DSCP_MASK
;
1223 flow
->nw_tos
|= value
->u8
& IP_DSCP_MASK
;
1226 case MFF_IP_DSCP_SHIFTED
:
1227 flow
->nw_tos
&= ~IP_DSCP_MASK
;
1228 flow
->nw_tos
|= value
->u8
<< 2;
1232 flow
->nw_tos
&= ~IP_ECN_MASK
;
1233 flow
->nw_tos
|= value
->u8
& IP_ECN_MASK
;
1237 flow
->nw_ttl
= value
->u8
;
1241 flow
->nw_frag
= value
->u8
& FLOW_NW_FRAG_MASK
;
1245 flow
->nw_proto
= ntohs(value
->be16
);
1249 flow
->nw_src
= value
->be32
;
1253 flow
->nw_dst
= value
->be32
;
1258 flow
->arp_sha
= value
->mac
;
1263 flow
->arp_tha
= value
->mac
;
1269 flow
->tp_src
= value
->be16
;
1275 flow
->tp_dst
= value
->be16
;
1279 flow
->tcp_flags
= value
->be16
;
1282 case MFF_ICMPV4_TYPE
:
1283 case MFF_ICMPV6_TYPE
:
1284 flow
->tp_src
= htons(value
->u8
);
1287 case MFF_ICMPV4_CODE
:
1288 case MFF_ICMPV6_CODE
:
1289 flow
->tp_dst
= htons(value
->u8
);
1293 flow
->nd_target
= value
->ipv6
;
1302 /* Consider each of 'src', 'mask', and 'dst' as if they were arrays of 8*n
1303 * bits. Then, for each 0 <= i < 8 * n such that mask[i] == 1, sets dst[i] =
1306 apply_mask(const uint8_t *src
, const uint8_t *mask
, uint8_t *dst
, size_t n
)
1310 for (i
= 0; i
< n
; i
++) {
1311 dst
[i
] = (src
[i
] & mask
[i
]) | (dst
[i
] & ~mask
[i
]);
1315 /* Sets 'flow' member field described by 'field' to 'value', except that bits
1316 * for which 'mask' has a 0-bit keep their existing values. The caller is
1317 * responsible for ensuring that 'flow' meets 'field''s prerequisites.*/
1319 mf_set_flow_value_masked(const struct mf_field
*field
,
1320 const union mf_value
*value
,
1321 const union mf_value
*mask
,
1326 mf_get_value(field
, flow
, &tmp
);
1327 apply_mask((const uint8_t *) value
, (const uint8_t *) mask
,
1328 (uint8_t *) &tmp
, field
->n_bytes
);
1329 mf_set_flow_value(field
, &tmp
, flow
);
1332 /* Returns true if 'mf' has a zero value in 'flow', false if it is nonzero.
1334 * The caller is responsible for ensuring that 'flow' meets 'mf''s
1337 mf_is_zero(const struct mf_field
*mf
, const struct flow
*flow
)
1339 union mf_value value
;
1341 mf_get_value(mf
, flow
, &value
);
1342 return is_all_zeros(&value
, mf
->n_bytes
);
1345 /* Makes 'match' wildcard field 'mf'.
1347 * The caller is responsible for ensuring that 'match' meets 'mf''s
1350 mf_set_wild(const struct mf_field
*mf
, struct match
*match
)
1354 match
->flow
.dp_hash
= 0;
1355 match
->wc
.masks
.dp_hash
= 0;
1358 match
->flow
.recirc_id
= 0;
1359 match
->wc
.masks
.recirc_id
= 0;
1362 match
->flow
.conj_id
= 0;
1363 match
->wc
.masks
.conj_id
= 0;
1366 match_set_tun_id_masked(match
, htonll(0), htonll(0));
1369 match_set_tun_src_masked(match
, htonl(0), htonl(0));
1372 match_set_tun_dst_masked(match
, htonl(0), htonl(0));
1375 match_set_tun_flags_masked(match
, 0, 0);
1377 case MFF_TUN_GBP_ID
:
1378 match_set_tun_gbp_id_masked(match
, 0, 0);
1380 case MFF_TUN_GBP_FLAGS
:
1381 match_set_tun_gbp_flags_masked(match
, 0, 0);
1384 match_set_tun_tos_masked(match
, 0, 0);
1387 match_set_tun_ttl_masked(match
, 0, 0);
1389 CASE_MFF_TUN_METADATA
:
1390 tun_metadata_set_match(mf
, NULL
, NULL
, match
);
1394 match_set_metadata_masked(match
, htonll(0), htonll(0));
1398 case MFF_IN_PORT_OXM
:
1399 match
->flow
.in_port
.ofp_port
= 0;
1400 match
->wc
.masks
.in_port
.ofp_port
= 0;
1402 case MFF_ACTSET_OUTPUT
:
1403 match
->flow
.actset_output
= 0;
1404 match
->wc
.masks
.actset_output
= 0;
1407 case MFF_SKB_PRIORITY
:
1408 match
->flow
.skb_priority
= 0;
1409 match
->wc
.masks
.skb_priority
= 0;
1413 match
->flow
.pkt_mark
= 0;
1414 match
->wc
.masks
.pkt_mark
= 0;
1418 match_set_reg_masked(match
, mf
->id
- MFF_REG0
, 0, 0);
1422 match_set_xreg_masked(match
, mf
->id
- MFF_XREG0
, 0, 0);
1426 match
->flow
.dl_src
= eth_addr_zero
;
1427 match
->wc
.masks
.dl_src
= eth_addr_zero
;
1431 match
->flow
.dl_dst
= eth_addr_zero
;
1432 match
->wc
.masks
.dl_dst
= eth_addr_zero
;
1436 match
->flow
.dl_type
= htons(0);
1437 match
->wc
.masks
.dl_type
= htons(0);
1441 match_set_dl_tci_masked(match
, htons(0), htons(0));
1446 match_set_any_vid(match
);
1449 case MFF_DL_VLAN_PCP
:
1451 match_set_any_pcp(match
);
1454 case MFF_MPLS_LABEL
:
1455 match_set_any_mpls_label(match
, 0);
1459 match_set_any_mpls_tc(match
, 0);
1463 match_set_any_mpls_bos(match
, 0);
1468 match_set_nw_src_masked(match
, htonl(0), htonl(0));
1473 match_set_nw_dst_masked(match
, htonl(0), htonl(0));
1477 memset(&match
->wc
.masks
.ipv6_src
, 0, sizeof match
->wc
.masks
.ipv6_src
);
1478 memset(&match
->flow
.ipv6_src
, 0, sizeof match
->flow
.ipv6_src
);
1482 memset(&match
->wc
.masks
.ipv6_dst
, 0, sizeof match
->wc
.masks
.ipv6_dst
);
1483 memset(&match
->flow
.ipv6_dst
, 0, sizeof match
->flow
.ipv6_dst
);
1486 case MFF_IPV6_LABEL
:
1487 match
->wc
.masks
.ipv6_label
= htonl(0);
1488 match
->flow
.ipv6_label
= htonl(0);
1492 match
->wc
.masks
.nw_proto
= 0;
1493 match
->flow
.nw_proto
= 0;
1497 case MFF_IP_DSCP_SHIFTED
:
1498 match
->wc
.masks
.nw_tos
&= ~IP_DSCP_MASK
;
1499 match
->flow
.nw_tos
&= ~IP_DSCP_MASK
;
1503 match
->wc
.masks
.nw_tos
&= ~IP_ECN_MASK
;
1504 match
->flow
.nw_tos
&= ~IP_ECN_MASK
;
1508 match
->wc
.masks
.nw_ttl
= 0;
1509 match
->flow
.nw_ttl
= 0;
1513 match
->wc
.masks
.nw_frag
&= ~FLOW_NW_FRAG_MASK
;
1514 match
->flow
.nw_frag
&= ~FLOW_NW_FRAG_MASK
;
1518 match
->wc
.masks
.nw_proto
= 0;
1519 match
->flow
.nw_proto
= 0;
1524 match
->flow
.arp_sha
= eth_addr_zero
;
1525 match
->wc
.masks
.arp_sha
= eth_addr_zero
;
1530 match
->flow
.arp_tha
= eth_addr_zero
;
1531 match
->wc
.masks
.arp_tha
= eth_addr_zero
;
1537 case MFF_ICMPV4_TYPE
:
1538 case MFF_ICMPV6_TYPE
:
1539 match
->wc
.masks
.tp_src
= htons(0);
1540 match
->flow
.tp_src
= htons(0);
1546 case MFF_ICMPV4_CODE
:
1547 case MFF_ICMPV6_CODE
:
1548 match
->wc
.masks
.tp_dst
= htons(0);
1549 match
->flow
.tp_dst
= htons(0);
1553 match
->wc
.masks
.tcp_flags
= htons(0);
1554 match
->flow
.tcp_flags
= htons(0);
1558 memset(&match
->wc
.masks
.nd_target
, 0,
1559 sizeof match
->wc
.masks
.nd_target
);
1560 memset(&match
->flow
.nd_target
, 0, sizeof match
->flow
.nd_target
);
1569 /* Makes 'match' match field 'mf' with the specified 'value' and 'mask'.
1570 * 'value' specifies a value to match and 'mask' specifies a wildcard pattern,
1571 * with a 1-bit indicating that the corresponding value bit must match and a
1572 * 0-bit indicating a don't-care.
1574 * If 'mask' is NULL or points to all-1-bits, then this call is equivalent to
1575 * mf_set_value(mf, value, match). If 'mask' points to all-0-bits, then this
1576 * call is equivalent to mf_set_wild(mf, match).
1578 * 'mask' must be a valid mask for 'mf' (see mf_is_mask_valid()). The caller
1579 * is responsible for ensuring that 'match' meets 'mf''s prerequisites. */
1580 enum ofputil_protocol
1581 mf_set(const struct mf_field
*mf
,
1582 const union mf_value
*value
, const union mf_value
*mask
,
1583 struct match
*match
)
1585 if (!mask
|| is_all_ones(mask
, mf
->n_bytes
)) {
1586 mf_set_value(mf
, value
, match
);
1587 return mf
->usable_protocols_exact
;
1588 } else if (is_all_zeros(mask
, mf
->n_bytes
)) {
1589 mf_set_wild(mf
, match
);
1590 return OFPUTIL_P_ANY
;
1597 case MFF_IN_PORT_OXM
:
1598 case MFF_ACTSET_OUTPUT
:
1599 case MFF_SKB_PRIORITY
:
1602 case MFF_DL_VLAN_PCP
:
1604 case MFF_MPLS_LABEL
:
1610 case MFF_IP_DSCP_SHIFTED
:
1613 case MFF_ICMPV4_TYPE
:
1614 case MFF_ICMPV4_CODE
:
1615 case MFF_ICMPV6_TYPE
:
1616 case MFF_ICMPV6_CODE
:
1617 return OFPUTIL_P_NONE
;
1620 match_set_dp_hash_masked(match
, ntohl(value
->be32
), ntohl(mask
->be32
));
1623 match_set_tun_id_masked(match
, value
->be64
, mask
->be64
);
1626 match_set_tun_src_masked(match
, value
->be32
, mask
->be32
);
1629 match_set_tun_dst_masked(match
, value
->be32
, mask
->be32
);
1632 match_set_tun_flags_masked(match
, ntohs(value
->be16
), ntohs(mask
->be16
));
1634 case MFF_TUN_GBP_ID
:
1635 match_set_tun_gbp_id_masked(match
, value
->be16
, mask
->be16
);
1637 case MFF_TUN_GBP_FLAGS
:
1638 match_set_tun_gbp_flags_masked(match
, value
->u8
, mask
->u8
);
1641 match_set_tun_ttl_masked(match
, value
->u8
, mask
->u8
);
1644 match_set_tun_tos_masked(match
, value
->u8
, mask
->u8
);
1646 CASE_MFF_TUN_METADATA
:
1647 tun_metadata_set_match(mf
, value
, mask
, match
);
1651 match_set_metadata_masked(match
, value
->be64
, mask
->be64
);
1655 match_set_reg_masked(match
, mf
->id
- MFF_REG0
,
1656 ntohl(value
->be32
), ntohl(mask
->be32
));
1660 match_set_xreg_masked(match
, mf
->id
- MFF_XREG0
,
1661 ntohll(value
->be64
), ntohll(mask
->be64
));
1665 match_set_pkt_mark_masked(match
, ntohl(value
->be32
),
1670 match_set_dl_dst_masked(match
, value
->mac
, mask
->mac
);
1674 match_set_dl_src_masked(match
, value
->mac
, mask
->mac
);
1679 match_set_arp_sha_masked(match
, value
->mac
, mask
->mac
);
1684 match_set_arp_tha_masked(match
, value
->mac
, mask
->mac
);
1688 match_set_dl_tci_masked(match
, value
->be16
, mask
->be16
);
1692 match_set_vlan_vid_masked(match
, value
->be16
, mask
->be16
);
1696 match_set_nw_src_masked(match
, value
->be32
, mask
->be32
);
1700 match_set_nw_dst_masked(match
, value
->be32
, mask
->be32
);
1704 match_set_ipv6_src_masked(match
, &value
->ipv6
, &mask
->ipv6
);
1708 match_set_ipv6_dst_masked(match
, &value
->ipv6
, &mask
->ipv6
);
1711 case MFF_IPV6_LABEL
:
1712 if ((mask
->be32
& htonl(IPV6_LABEL_MASK
)) == htonl(IPV6_LABEL_MASK
)) {
1713 mf_set_value(mf
, value
, match
);
1715 match_set_ipv6_label_masked(match
, value
->be32
, mask
->be32
);
1720 match_set_nd_target_masked(match
, &value
->ipv6
, &mask
->ipv6
);
1724 match_set_nw_frag_masked(match
, value
->u8
, mask
->u8
);
1728 match_set_nw_src_masked(match
, value
->be32
, mask
->be32
);
1732 match_set_nw_dst_masked(match
, value
->be32
, mask
->be32
);
1738 match_set_tp_src_masked(match
, value
->be16
, mask
->be16
);
1744 match_set_tp_dst_masked(match
, value
->be16
, mask
->be16
);
1748 match_set_tcp_flags_masked(match
, value
->be16
, mask
->be16
);
1756 return ((mf
->usable_protocols_bitwise
== mf
->usable_protocols_cidr
1757 || ip_is_cidr(mask
->be32
))
1758 ? mf
->usable_protocols_cidr
1759 : mf
->usable_protocols_bitwise
);
1763 mf_check__(const struct mf_subfield
*sf
, const struct flow
*flow
,
1767 VLOG_WARN_RL(&rl
, "unknown %s field", type
);
1768 return OFPERR_OFPBAC_BAD_SET_TYPE
;
1769 } else if (!sf
->n_bits
) {
1770 VLOG_WARN_RL(&rl
, "zero bit %s field %s", type
, sf
->field
->name
);
1771 return OFPERR_OFPBAC_BAD_SET_LEN
;
1772 } else if (sf
->ofs
>= sf
->field
->n_bits
) {
1773 VLOG_WARN_RL(&rl
, "bit offset %d exceeds %d-bit width of %s field %s",
1774 sf
->ofs
, sf
->field
->n_bits
, type
, sf
->field
->name
);
1775 return OFPERR_OFPBAC_BAD_SET_LEN
;
1776 } else if (sf
->ofs
+ sf
->n_bits
> sf
->field
->n_bits
) {
1777 VLOG_WARN_RL(&rl
, "bit offset %d and width %d exceeds %d-bit width "
1778 "of %s field %s", sf
->ofs
, sf
->n_bits
,
1779 sf
->field
->n_bits
, type
, sf
->field
->name
);
1780 return OFPERR_OFPBAC_BAD_SET_LEN
;
1781 } else if (flow
&& !mf_are_prereqs_ok(sf
->field
, flow
)) {
1782 VLOG_WARN_RL(&rl
, "%s field %s lacks correct prerequisites",
1783 type
, sf
->field
->name
);
1784 return OFPERR_OFPBAC_MATCH_INCONSISTENT
;
1790 /* Checks whether 'sf' is valid for reading a subfield out of 'flow'. Returns
1791 * 0 if so, otherwise an OpenFlow error code (e.g. as returned by
1794 mf_check_src(const struct mf_subfield
*sf
, const struct flow
*flow
)
1796 return mf_check__(sf
, flow
, "source");
1799 /* Checks whether 'sf' is valid for writing a subfield into 'flow'. Returns 0
1800 * if so, otherwise an OpenFlow error code (e.g. as returned by
1803 mf_check_dst(const struct mf_subfield
*sf
, const struct flow
*flow
)
1805 int error
= mf_check__(sf
, flow
, "destination");
1806 if (!error
&& !sf
->field
->writable
) {
1807 VLOG_WARN_RL(&rl
, "destination field %s is not writable",
1809 return OFPERR_OFPBAC_BAD_SET_ARGUMENT
;
1814 /* Copies the value and wildcard bit pattern for 'mf' from 'match' into the
1815 * 'value' and 'mask', respectively. */
1817 mf_get(const struct mf_field
*mf
, const struct match
*match
,
1818 union mf_value
*value
, union mf_value
*mask
)
1820 mf_get_value(mf
, &match
->flow
, value
);
1821 mf_get_mask(mf
, &match
->wc
, mask
);
1825 mf_from_integer_string(const struct mf_field
*mf
, const char *s
,
1826 uint8_t *valuep
, uint8_t *maskp
)
1829 const char *err_str
= "";
1832 err
= parse_int_string(s
, valuep
, mf
->n_bytes
, &tail
);
1833 if (err
|| (*tail
!= '\0' && *tail
!= '/')) {
1839 err
= parse_int_string(tail
+ 1, maskp
, mf
->n_bytes
, &tail
);
1840 if (err
|| *tail
!= '\0') {
1845 memset(maskp
, 0xff, mf
->n_bytes
);
1851 if (err
== ERANGE
) {
1852 return xasprintf("%s: %s too large for %u-byte field %s",
1853 s
, err_str
, mf
->n_bytes
, mf
->name
);
1855 return xasprintf("%s: bad syntax for %s %s", s
, mf
->name
, err_str
);
1860 mf_from_ethernet_string(const struct mf_field
*mf
, const char *s
,
1861 struct eth_addr
*mac
, struct eth_addr
*mask
)
1865 ovs_assert(mf
->n_bytes
== ETH_ADDR_LEN
);
1868 if (ovs_scan(s
, ETH_ADDR_SCAN_FMT
"%n", ETH_ADDR_SCAN_ARGS(*mac
), &n
)
1869 && n
== strlen(s
)) {
1870 *mask
= eth_addr_exact
;
1875 if (ovs_scan(s
, ETH_ADDR_SCAN_FMT
"/"ETH_ADDR_SCAN_FMT
"%n",
1876 ETH_ADDR_SCAN_ARGS(*mac
), ETH_ADDR_SCAN_ARGS(*mask
), &n
)
1877 && n
== strlen(s
)) {
1881 return xasprintf("%s: invalid Ethernet address", s
);
1885 mf_from_ipv4_string(const struct mf_field
*mf
, const char *s
,
1886 ovs_be32
*ip
, ovs_be32
*mask
)
1890 ovs_assert(mf
->n_bytes
== sizeof *ip
);
1892 if (ovs_scan(s
, IP_SCAN_FMT
"/"IP_SCAN_FMT
,
1893 IP_SCAN_ARGS(ip
), IP_SCAN_ARGS(mask
))) {
1895 } else if (ovs_scan(s
, IP_SCAN_FMT
"/%d", IP_SCAN_ARGS(ip
), &prefix
)) {
1896 if (prefix
<= 0 || prefix
> 32) {
1897 return xasprintf("%s: network prefix bits not between 0 and "
1900 *mask
= be32_prefix_mask(prefix
);
1901 } else if (ovs_scan(s
, IP_SCAN_FMT
, IP_SCAN_ARGS(ip
))) {
1902 *mask
= OVS_BE32_MAX
;
1904 return xasprintf("%s: invalid IP address", s
);
1910 mf_from_ipv6_string(const struct mf_field
*mf
, const char *s
,
1911 struct in6_addr
*value
, struct in6_addr
*mask
)
1913 char *str
= xstrdup(s
);
1914 char *save_ptr
= NULL
;
1915 const char *name
, *netmask
;
1918 ovs_assert(mf
->n_bytes
== sizeof *value
);
1920 name
= strtok_r(str
, "/", &save_ptr
);
1921 retval
= name
? lookup_ipv6(name
, value
) : EINVAL
;
1925 err
= xasprintf("%s: could not convert to IPv6 address", str
);
1931 netmask
= strtok_r(NULL
, "/", &save_ptr
);
1933 if (inet_pton(AF_INET6
, netmask
, mask
) != 1) {
1934 int prefix
= atoi(netmask
);
1935 if (prefix
<= 0 || prefix
> 128) {
1937 return xasprintf("%s: prefix bits not between 1 and 128", s
);
1939 *mask
= ipv6_create_mask(prefix
);
1943 *mask
= in6addr_exact
;
1951 mf_from_ofp_port_string(const struct mf_field
*mf
, const char *s
,
1952 ovs_be16
*valuep
, ovs_be16
*maskp
)
1956 ovs_assert(mf
->n_bytes
== sizeof(ovs_be16
));
1958 if (ofputil_port_from_string(s
, &port
)) {
1959 *valuep
= htons(ofp_to_u16(port
));
1960 *maskp
= OVS_BE16_MAX
;
1963 return xasprintf("%s: port value out of range for %s", s
, mf
->name
);
1967 mf_from_ofp_port_string32(const struct mf_field
*mf
, const char *s
,
1968 ovs_be32
*valuep
, ovs_be32
*maskp
)
1972 ovs_assert(mf
->n_bytes
== sizeof(ovs_be32
));
1973 if (ofputil_port_from_string(s
, &port
)) {
1974 *valuep
= ofputil_port_to_ofp11(port
);
1975 *maskp
= OVS_BE32_MAX
;
1978 return xasprintf("%s: port value out of range for %s", s
, mf
->name
);
1981 struct frag_handling
{
1987 static const struct frag_handling all_frags
[] = {
1988 #define A FLOW_NW_FRAG_ANY
1989 #define L FLOW_NW_FRAG_LATER
1990 /* name mask value */
1993 { "first", A
|L
, A
},
1994 { "later", A
|L
, A
|L
},
1999 { "not_later", L
, 0 },
2006 mf_from_frag_string(const char *s
, uint8_t *valuep
, uint8_t *maskp
)
2008 const struct frag_handling
*h
;
2010 for (h
= all_frags
; h
< &all_frags
[ARRAY_SIZE(all_frags
)]; h
++) {
2011 if (!strcasecmp(s
, h
->name
)) {
2012 /* We force the upper bits of the mask on to make mf_parse_value()
2013 * happy (otherwise it will never think it's an exact match.) */
2014 *maskp
= h
->mask
| ~FLOW_NW_FRAG_MASK
;
2020 return xasprintf("%s: unknown fragment type (valid types are \"no\", "
2021 "\"yes\", \"first\", \"later\", \"not_first\"", s
);
2025 parse_mf_flags(const char *s
, const char *(*bit_to_string
)(uint32_t),
2026 const char *field_name
, ovs_be16
*flagsp
, ovs_be16 allowed
,
2031 uint32_t flags
, mask
;
2033 err
= parse_flags(s
, bit_to_string
, '\0', field_name
, &err_str
,
2034 &flags
, ntohs(allowed
), maskp
? &mask
: NULL
);
2039 *flagsp
= htons(flags
);
2041 *maskp
= htons(mask
);
2048 mf_from_tcp_flags_string(const char *s
, ovs_be16
*flagsp
, ovs_be16
*maskp
)
2050 return parse_mf_flags(s
, packet_tcp_flag_to_string
, "TCP", flagsp
,
2051 TCP_FLAGS_BE16(OVS_BE16_MAX
), maskp
);
2055 mf_from_tun_flags_string(const char *s
, ovs_be16
*flagsp
, ovs_be16
*maskp
)
2057 return parse_mf_flags(s
, flow_tun_flag_to_string
, "tunnel", flagsp
,
2058 htons(FLOW_TNL_PUB_F_MASK
), maskp
);
2061 /* Parses 's', a string value for field 'mf', into 'value' and 'mask'. Returns
2062 * NULL if successful, otherwise a malloc()'d string describing the error. */
2064 mf_parse(const struct mf_field
*mf
, const char *s
,
2065 union mf_value
*value
, union mf_value
*mask
)
2069 if (!strcmp(s
, "*")) {
2070 memset(value
, 0, mf
->n_bytes
);
2071 memset(mask
, 0, mf
->n_bytes
);
2075 switch (mf
->string
) {
2077 case MFS_HEXADECIMAL
:
2078 error
= mf_from_integer_string(mf
, s
,
2079 (uint8_t *) value
, (uint8_t *) mask
);
2083 error
= mf_from_ethernet_string(mf
, s
, &value
->mac
, &mask
->mac
);
2087 error
= mf_from_ipv4_string(mf
, s
, &value
->be32
, &mask
->be32
);
2091 error
= mf_from_ipv6_string(mf
, s
, &value
->ipv6
, &mask
->ipv6
);
2095 error
= mf_from_ofp_port_string(mf
, s
, &value
->be16
, &mask
->be16
);
2098 case MFS_OFP_PORT_OXM
:
2099 error
= mf_from_ofp_port_string32(mf
, s
, &value
->be32
, &mask
->be32
);
2103 error
= mf_from_frag_string(s
, &value
->u8
, &mask
->u8
);
2107 ovs_assert(mf
->n_bytes
== sizeof(ovs_be16
));
2108 error
= mf_from_tun_flags_string(s
, &value
->be16
, &mask
->be16
);
2112 ovs_assert(mf
->n_bytes
== sizeof(ovs_be16
));
2113 error
= mf_from_tcp_flags_string(s
, &value
->be16
, &mask
->be16
);
2120 if (!error
&& !mf_is_mask_valid(mf
, mask
)) {
2121 error
= xasprintf("%s: invalid mask for field %s", s
, mf
->name
);
2126 /* Parses 's', a string value for field 'mf', into 'value'. Returns NULL if
2127 * successful, otherwise a malloc()'d string describing the error. */
2129 mf_parse_value(const struct mf_field
*mf
, const char *s
, union mf_value
*value
)
2131 union mf_value mask
;
2134 error
= mf_parse(mf
, s
, value
, &mask
);
2139 if (!is_all_ones((const uint8_t *) &mask
, mf
->n_bytes
)) {
2140 return xasprintf("%s: wildcards not allowed here", s
);
2146 mf_format_integer_string(const struct mf_field
*mf
, const uint8_t *valuep
,
2147 const uint8_t *maskp
, struct ds
*s
)
2149 if (mf
->string
== MFS_HEXADECIMAL
) {
2150 ds_put_hex(s
, valuep
, mf
->n_bytes
);
2152 unsigned long long int integer
= 0;
2155 ovs_assert(mf
->n_bytes
<= 8);
2156 for (i
= 0; i
< mf
->n_bytes
; i
++) {
2157 integer
= (integer
<< 8) | valuep
[i
];
2159 ds_put_format(s
, "%lld", integer
);
2163 /* I guess we could write the mask in decimal for MFS_DECIMAL but I'm
2164 * not sure that that a bit-mask written in decimal is ever easier to
2165 * understand than the same bit-mask written in hexadecimal. */
2166 ds_put_char(s
, '/');
2167 ds_put_hex(s
, maskp
, mf
->n_bytes
);
2172 mf_format_frag_string(uint8_t value
, uint8_t mask
, struct ds
*s
)
2174 const struct frag_handling
*h
;
2176 mask
&= FLOW_NW_FRAG_MASK
;
2179 for (h
= all_frags
; h
< &all_frags
[ARRAY_SIZE(all_frags
)]; h
++) {
2180 if (value
== h
->value
&& mask
== h
->mask
) {
2181 ds_put_cstr(s
, h
->name
);
2185 ds_put_cstr(s
, "<error>");
2189 mf_format_tnl_flags_string(ovs_be16 value
, ovs_be16 mask
, struct ds
*s
)
2191 format_flags_masked(s
, NULL
, flow_tun_flag_to_string
, ntohs(value
),
2192 ntohs(mask
) & FLOW_TNL_PUB_F_MASK
, FLOW_TNL_PUB_F_MASK
);
2196 mf_format_tcp_flags_string(ovs_be16 value
, ovs_be16 mask
, struct ds
*s
)
2198 format_flags_masked(s
, NULL
, packet_tcp_flag_to_string
, ntohs(value
),
2199 TCP_FLAGS(mask
), TCP_FLAGS(OVS_BE16_MAX
));
2202 /* Appends to 's' a string representation of field 'mf' whose value is in
2203 * 'value' and 'mask'. 'mask' may be NULL to indicate an exact match. */
2205 mf_format(const struct mf_field
*mf
,
2206 const union mf_value
*value
, const union mf_value
*mask
,
2210 if (is_all_zeros(mask
, mf
->n_bytes
)) {
2211 ds_put_cstr(s
, "ANY");
2213 } else if (is_all_ones(mask
, mf
->n_bytes
)) {
2218 switch (mf
->string
) {
2219 case MFS_OFP_PORT_OXM
:
2222 ofputil_port_from_ofp11(value
->be32
, &port
);
2223 ofputil_format_port(port
, s
);
2229 ofputil_format_port(u16_to_ofp(ntohs(value
->be16
)), s
);
2234 case MFS_HEXADECIMAL
:
2235 mf_format_integer_string(mf
, (uint8_t *) value
, (uint8_t *) mask
, s
);
2239 eth_format_masked(value
->mac
, mask
? &mask
->mac
: NULL
, s
);
2243 ip_format_masked(value
->be32
, mask
? mask
->be32
: OVS_BE32_MAX
, s
);
2247 print_ipv6_masked(s
, &value
->ipv6
, mask
? &mask
->ipv6
: NULL
);
2251 mf_format_frag_string(value
->u8
, mask
? mask
->u8
: UINT8_MAX
, s
);
2255 mf_format_tnl_flags_string(value
->be16
,
2256 mask
? mask
->be16
: OVS_BE16_MAX
, s
);
2260 mf_format_tcp_flags_string(value
->be16
,
2261 mask
? mask
->be16
: OVS_BE16_MAX
, s
);
2269 /* Makes subfield 'sf' within 'flow' exactly match the 'sf->n_bits'
2270 * least-significant bits in 'x'.
2273 mf_write_subfield_flow(const struct mf_subfield
*sf
,
2274 const union mf_subvalue
*x
, struct flow
*flow
)
2276 const struct mf_field
*field
= sf
->field
;
2277 union mf_value value
;
2279 mf_get_value(field
, flow
, &value
);
2280 bitwise_copy(x
, sizeof *x
, 0, &value
, field
->n_bytes
,
2281 sf
->ofs
, sf
->n_bits
);
2282 mf_set_flow_value(field
, &value
, flow
);
2285 /* Makes subfield 'sf' within 'match' exactly match the 'sf->n_bits'
2286 * least-significant bits in 'x'.
2289 mf_write_subfield(const struct mf_subfield
*sf
, const union mf_subvalue
*x
,
2290 struct match
*match
)
2292 const struct mf_field
*field
= sf
->field
;
2293 union mf_value value
, mask
;
2295 mf_get(field
, match
, &value
, &mask
);
2296 bitwise_copy(x
, sizeof *x
, 0, &value
, field
->n_bytes
, sf
->ofs
, sf
->n_bits
);
2297 bitwise_one ( &mask
, field
->n_bytes
, sf
->ofs
, sf
->n_bits
);
2298 mf_set(field
, &value
, &mask
, match
);
2301 /* 'v' and 'm' correspond to values of 'field'. This function copies them into
2302 * 'match' in the correspond positions. */
2304 mf_mask_subfield(const struct mf_field
*field
,
2305 const union mf_subvalue
*v
,
2306 const union mf_subvalue
*m
,
2307 struct match
*match
)
2309 union mf_value value
, mask
;
2311 mf_get(field
, match
, &value
, &mask
);
2312 bitwise_copy(v
, sizeof *v
, 0, &value
, field
->n_bytes
, 0, field
->n_bits
);
2313 bitwise_copy(m
, sizeof *m
, 0, &mask
, field
->n_bytes
, 0, field
->n_bits
);
2314 mf_set(field
, &value
, &mask
, match
);
2317 /* Initializes 'x' to the value of 'sf' within 'flow'. 'sf' must be valid for
2318 * reading 'flow', e.g. as checked by mf_check_src(). */
2320 mf_read_subfield(const struct mf_subfield
*sf
, const struct flow
*flow
,
2321 union mf_subvalue
*x
)
2323 union mf_value value
;
2325 mf_get_value(sf
->field
, flow
, &value
);
2327 memset(x
, 0, sizeof *x
);
2328 bitwise_copy(&value
, sf
->field
->n_bytes
, sf
->ofs
,
2333 /* Returns the value of 'sf' within 'flow'. 'sf' must be valid for reading
2334 * 'flow', e.g. as checked by mf_check_src() and sf->n_bits must be 64 or
2337 mf_get_subfield(const struct mf_subfield
*sf
, const struct flow
*flow
)
2339 union mf_value value
;
2341 mf_get_value(sf
->field
, flow
, &value
);
2342 return bitwise_get(&value
, sf
->field
->n_bytes
, sf
->ofs
, sf
->n_bits
);
2346 mf_format_subvalue(const union mf_subvalue
*subvalue
, struct ds
*s
)
2348 ds_put_hex(s
, subvalue
->u8
, sizeof subvalue
->u8
);
2352 field_array_set(enum mf_field_id id
, const union mf_value
*value
,
2353 struct field_array
*fa
)
2355 ovs_assert(id
< MFF_N_IDS
);
2356 bitmap_set1(fa
->used
.bm
, id
);
2357 fa
->value
[id
] = *value
;