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 "unaligned.h"
38 #include "openvswitch/vlog.h"
40 VLOG_DEFINE_THIS_MODULE(meta_flow
);
42 #define FLOW_U32OFS(FIELD) \
43 offsetof(struct flow, FIELD) % 4 ? -1 : offsetof(struct flow, FIELD) / 4
45 #define MF_FIELD_SIZES(MEMBER) \
46 sizeof ((union mf_value *)0)->MEMBER, \
47 8 * sizeof ((union mf_value *)0)->MEMBER
49 extern const struct mf_field mf_fields
[MFF_N_IDS
]; /* Silence a warning. */
51 const struct mf_field mf_fields
[MFF_N_IDS
] = {
52 #include "meta-flow.inc"
55 /* Maps from an mf_field's 'name' or 'extra_name' to the mf_field. */
56 static struct shash mf_by_name
;
58 /* Rate limit for parse errors. These always indicate a bug in an OpenFlow
59 * controller and so there's not much point in showing a lot of them. */
60 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
62 static void nxm_init(void);
64 /* Returns the field with the given 'name', or a null pointer if no field has
66 const struct mf_field
*
67 mf_from_name(const char *name
)
70 return shash_find_data(&mf_by_name
, name
);
78 shash_init(&mf_by_name
);
79 for (i
= 0; i
< MFF_N_IDS
; i
++) {
80 const struct mf_field
*mf
= &mf_fields
[i
];
82 ovs_assert(mf
->id
== i
); /* Fields must be in the enum order. */
84 shash_add_once(&mf_by_name
, mf
->name
, mf
);
86 shash_add_once(&mf_by_name
, mf
->extra_name
, mf
);
94 static pthread_once_t once
= PTHREAD_ONCE_INIT
;
95 pthread_once(&once
, nxm_do_init
);
98 /* Consider the two value/mask pairs 'a_value/a_mask' and 'b_value/b_mask' as
99 * restrictions on a field's value. Then, this function initializes
100 * 'dst_value/dst_mask' such that it combines the restrictions of both pairs.
101 * This is not always possible, i.e. if one pair insists on a value of 0 in
102 * some bit and the other pair insists on a value of 1 in that bit. This
103 * function returns false in a case where the combined restriction is
104 * impossible (in which case 'dst_value/dst_mask' is not fully initialized),
107 * (As usually true for value/mask pairs in OVS, any 1-bit in a value must have
108 * a corresponding 1-bit in its mask.) */
110 mf_subvalue_intersect(const union mf_subvalue
*a_value
,
111 const union mf_subvalue
*a_mask
,
112 const union mf_subvalue
*b_value
,
113 const union mf_subvalue
*b_mask
,
114 union mf_subvalue
*dst_value
,
115 union mf_subvalue
*dst_mask
)
117 for (int i
= 0; i
< ARRAY_SIZE(a_value
->be64
); i
++) {
118 ovs_be64 av
= a_value
->be64
[i
];
119 ovs_be64 am
= a_mask
->be64
[i
];
120 ovs_be64 bv
= b_value
->be64
[i
];
121 ovs_be64 bm
= b_mask
->be64
[i
];
122 ovs_be64
*dv
= &dst_value
->be64
[i
];
123 ovs_be64
*dm
= &dst_mask
->be64
[i
];
125 if ((av
^ bv
) & (am
& bm
)) {
134 /* Returns the "number of bits" in 'v', e.g. 1 if only the lowest-order bit is
135 * set, 2 if the second-lowest-order bit is set, and so on. */
137 mf_subvalue_width(const union mf_subvalue
*v
)
139 return 1 + bitwise_rscan(v
, sizeof *v
, true, sizeof *v
* 8 - 1, -1);
142 /* For positive 'n', shifts the bits in 'value' 'n' bits to the left, and for
143 * negative 'n', shifts the bits '-n' bits to the right. */
145 mf_subvalue_shift(union mf_subvalue
*value
, int n
)
148 union mf_subvalue tmp
;
149 memset(&tmp
, 0, sizeof tmp
);
151 if (n
> 0 && n
< 8 * sizeof tmp
) {
152 bitwise_copy(value
, sizeof *value
, 0,
155 } else if (n
< 0 && n
> -8 * sizeof tmp
) {
156 bitwise_copy(value
, sizeof *value
, -n
,
164 /* Returns true if 'wc' wildcards all the bits in field 'mf', false if 'wc'
165 * specifies at least one bit in the field.
167 * The caller is responsible for ensuring that 'wc' corresponds to a flow that
168 * meets 'mf''s prerequisites. */
170 mf_is_all_wild(const struct mf_field
*mf
, const struct flow_wildcards
*wc
)
174 return !wc
->masks
.dp_hash
;
176 return !wc
->masks
.recirc_id
;
178 return !wc
->masks
.conj_id
;
180 return !wc
->masks
.tunnel
.ip_src
;
182 return !wc
->masks
.tunnel
.ip_dst
;
187 return !wc
->masks
.tunnel
.tun_id
;
189 return !wc
->masks
.tunnel
.gbp_id
;
190 case MFF_TUN_GBP_FLAGS
:
191 return !wc
->masks
.tunnel
.gbp_flags
;
193 return !wc
->masks
.metadata
;
195 case MFF_IN_PORT_OXM
:
196 return !wc
->masks
.in_port
.ofp_port
;
197 case MFF_SKB_PRIORITY
:
198 return !wc
->masks
.skb_priority
;
200 return !wc
->masks
.pkt_mark
;
202 return !wc
->masks
.regs
[mf
->id
- MFF_REG0
];
204 return !flow_get_xreg(&wc
->masks
, mf
->id
- MFF_XREG0
);
205 case MFF_ACTSET_OUTPUT
:
206 return !wc
->masks
.actset_output
;
209 return eth_addr_is_zero(wc
->masks
.dl_src
);
211 return eth_addr_is_zero(wc
->masks
.dl_dst
);
213 return !wc
->masks
.dl_type
;
217 return eth_addr_is_zero(wc
->masks
.arp_sha
);
221 return eth_addr_is_zero(wc
->masks
.arp_tha
);
224 return !wc
->masks
.vlan_tci
;
226 return !(wc
->masks
.vlan_tci
& htons(VLAN_VID_MASK
));
228 return !(wc
->masks
.vlan_tci
& htons(VLAN_VID_MASK
| VLAN_CFI
));
229 case MFF_DL_VLAN_PCP
:
231 return !(wc
->masks
.vlan_tci
& htons(VLAN_PCP_MASK
));
234 return !(wc
->masks
.mpls_lse
[0] & htonl(MPLS_LABEL_MASK
));
236 return !(wc
->masks
.mpls_lse
[0] & htonl(MPLS_TC_MASK
));
238 return !(wc
->masks
.mpls_lse
[0] & htonl(MPLS_BOS_MASK
));
241 return !wc
->masks
.nw_src
;
243 return !wc
->masks
.nw_dst
;
246 return ipv6_mask_is_any(&wc
->masks
.ipv6_src
);
248 return ipv6_mask_is_any(&wc
->masks
.ipv6_dst
);
251 return !wc
->masks
.ipv6_label
;
254 return !wc
->masks
.nw_proto
;
256 case MFF_IP_DSCP_SHIFTED
:
257 return !(wc
->masks
.nw_tos
& IP_DSCP_MASK
);
259 return !(wc
->masks
.nw_tos
& IP_ECN_MASK
);
261 return !wc
->masks
.nw_ttl
;
264 return ipv6_mask_is_any(&wc
->masks
.nd_target
);
267 return !(wc
->masks
.nw_frag
& FLOW_NW_FRAG_MASK
);
270 return !wc
->masks
.nw_proto
;
272 return !wc
->masks
.nw_src
;
274 return !wc
->masks
.nw_dst
;
279 case MFF_ICMPV4_TYPE
:
280 case MFF_ICMPV6_TYPE
:
281 return !wc
->masks
.tp_src
;
285 case MFF_ICMPV4_CODE
:
286 case MFF_ICMPV6_CODE
:
287 return !wc
->masks
.tp_dst
;
289 return !wc
->masks
.tcp_flags
;
297 /* Initializes 'mask' with the wildcard bit pattern for field 'mf' within 'wc'.
298 * Each bit in 'mask' will be set to 1 if the bit is significant for matching
299 * purposes, or to 0 if it is wildcarded.
301 * The caller is responsible for ensuring that 'wc' corresponds to a flow that
302 * meets 'mf''s prerequisites. */
304 mf_get_mask(const struct mf_field
*mf
, const struct flow_wildcards
*wc
,
305 union mf_value
*mask
)
307 mf_get_value(mf
, &wc
->masks
, mask
);
310 /* Tests whether 'mask' is a valid wildcard bit pattern for 'mf'. Returns true
311 * if the mask is valid, false otherwise. */
313 mf_is_mask_valid(const struct mf_field
*mf
, const union mf_value
*mask
)
315 switch (mf
->maskable
) {
317 return (is_all_zeros(mask
, mf
->n_bytes
) ||
318 is_all_ones(mask
, mf
->n_bytes
));
327 /* Returns true if 'flow' meets the prerequisites for 'mf', false otherwise. */
329 mf_are_prereqs_ok(const struct mf_field
*mf
, const struct flow
*flow
)
331 switch (mf
->prereqs
) {
336 return (flow
->dl_type
== htons(ETH_TYPE_ARP
) ||
337 flow
->dl_type
== htons(ETH_TYPE_RARP
));
339 return flow
->dl_type
== htons(ETH_TYPE_IP
);
341 return flow
->dl_type
== htons(ETH_TYPE_IPV6
);
343 return (flow
->vlan_tci
& htons(VLAN_CFI
)) != 0;
345 return eth_type_mpls(flow
->dl_type
);
347 return is_ip_any(flow
);
350 return is_ip_any(flow
) && flow
->nw_proto
== IPPROTO_TCP
351 && !(flow
->nw_frag
& FLOW_NW_FRAG_LATER
);
353 return is_ip_any(flow
) && flow
->nw_proto
== IPPROTO_UDP
354 && !(flow
->nw_frag
& FLOW_NW_FRAG_LATER
);
356 return is_ip_any(flow
) && flow
->nw_proto
== IPPROTO_SCTP
357 && !(flow
->nw_frag
& FLOW_NW_FRAG_LATER
);
359 return is_icmpv4(flow
);
361 return is_icmpv6(flow
);
364 return (is_icmpv6(flow
)
365 && flow
->tp_dst
== htons(0)
366 && (flow
->tp_src
== htons(ND_NEIGHBOR_SOLICIT
) ||
367 flow
->tp_src
== htons(ND_NEIGHBOR_ADVERT
)));
369 return (is_icmpv6(flow
)
370 && flow
->tp_dst
== htons(0)
371 && (flow
->tp_src
== htons(ND_NEIGHBOR_SOLICIT
)));
373 return (is_icmpv6(flow
)
374 && flow
->tp_dst
== htons(0)
375 && (flow
->tp_src
== htons(ND_NEIGHBOR_ADVERT
)));
381 /* Set field and it's prerequisities in the mask.
382 * This is only ever called for writeable 'mf's, but we do not make the
383 * distinction here. */
385 mf_mask_field_and_prereqs(const struct mf_field
*mf
, struct flow
*mask
)
387 static const union mf_value exact_match_mask
= MF_EXACT_MASK_INITIALIZER
;
389 mf_set_flow_value(mf
, &exact_match_mask
, mask
);
391 switch (mf
->prereqs
) {
395 mask
->tp_src
= OVS_BE16_MAX
;
396 mask
->tp_dst
= OVS_BE16_MAX
;
403 /* nw_frag always unwildcarded. */
404 mask
->nw_proto
= 0xff;
411 mask
->dl_type
= OVS_BE16_MAX
;
414 mask
->vlan_tci
|= htons(VLAN_CFI
);
421 /* Set bits of 'bm' corresponding to the field 'mf' and it's prerequisities. */
423 mf_bitmap_set_field_and_prereqs(const struct mf_field
*mf
, struct mf_bitmap
*bm
)
425 bitmap_set1(bm
->bm
, mf
->id
);
427 switch (mf
->prereqs
) {
431 bitmap_set1(bm
->bm
, MFF_TCP_SRC
);
432 bitmap_set1(bm
->bm
, MFF_TCP_DST
);
439 /* nw_frag always unwildcarded. */
440 bitmap_set1(bm
->bm
, MFF_IP_PROTO
);
447 bitmap_set1(bm
->bm
, MFF_ETH_TYPE
);
450 bitmap_set1(bm
->bm
, MFF_VLAN_TCI
);
457 /* Returns true if 'value' may be a valid value *as part of a masked match*,
460 * A value is not rejected just because it is not valid for the field in
461 * question, but only if it doesn't make sense to test the bits in question at
462 * all. For example, the MFF_VLAN_TCI field will never have a nonzero value
463 * without the VLAN_CFI bit being set, but we can't reject those values because
464 * it is still legitimate to test just for those bits (see the documentation
465 * for NXM_OF_VLAN_TCI in nicira-ext.h). On the other hand, there is never a
466 * reason to set the low bit of MFF_IP_DSCP to 1, so we reject that. */
468 mf_is_value_valid(const struct mf_field
*mf
, const union mf_value
*value
)
481 case MFF_TUN_GBP_FLAGS
:
484 case MFF_SKB_PRIORITY
:
508 case MFF_ICMPV4_TYPE
:
509 case MFF_ICMPV4_CODE
:
510 case MFF_ICMPV6_TYPE
:
511 case MFF_ICMPV6_CODE
:
517 case MFF_IN_PORT_OXM
:
518 case MFF_ACTSET_OUTPUT
: {
520 return !ofputil_port_from_ofp11(value
->be32
, &port
);
524 return !(value
->u8
& ~IP_DSCP_MASK
);
525 case MFF_IP_DSCP_SHIFTED
:
526 return !(value
->u8
& (~IP_DSCP_MASK
>> 2));
528 return !(value
->u8
& ~IP_ECN_MASK
);
530 return !(value
->u8
& ~FLOW_NW_FRAG_MASK
);
532 return !(value
->be16
& ~htons(0x0fff));
535 return !(value
->be16
& htons(0xff00));
538 return !(value
->be16
& htons(VLAN_CFI
| VLAN_PCP_MASK
));
540 return !(value
->be16
& htons(VLAN_PCP_MASK
));
542 case MFF_DL_VLAN_PCP
:
544 return !(value
->u8
& ~(VLAN_PCP_MASK
>> VLAN_PCP_SHIFT
));
547 return !(value
->be32
& ~htonl(IPV6_LABEL_MASK
));
550 return !(value
->be32
& ~htonl(MPLS_LABEL_MASK
>> MPLS_LABEL_SHIFT
));
553 return !(value
->u8
& ~(MPLS_TC_MASK
>> MPLS_TC_SHIFT
));
556 return !(value
->u8
& ~(MPLS_BOS_MASK
>> MPLS_BOS_SHIFT
));
564 /* Copies the value of field 'mf' from 'flow' into 'value'. The caller is
565 * responsible for ensuring that 'flow' meets 'mf''s prerequisites. */
567 mf_get_value(const struct mf_field
*mf
, const struct flow
*flow
,
568 union mf_value
*value
)
572 value
->be32
= htonl(flow
->dp_hash
);
575 value
->be32
= htonl(flow
->recirc_id
);
578 value
->be32
= htonl(flow
->conj_id
);
581 value
->be64
= flow
->tunnel
.tun_id
;
584 value
->be32
= flow
->tunnel
.ip_src
;
587 value
->be32
= flow
->tunnel
.ip_dst
;
590 value
->be16
= htons(flow
->tunnel
.flags
);
593 value
->be16
= flow
->tunnel
.gbp_id
;
595 case MFF_TUN_GBP_FLAGS
:
596 value
->u8
= flow
->tunnel
.gbp_flags
;
599 value
->u8
= flow
->tunnel
.ip_ttl
;
602 value
->u8
= flow
->tunnel
.ip_tos
;
606 value
->be64
= flow
->metadata
;
610 value
->be16
= htons(ofp_to_u16(flow
->in_port
.ofp_port
));
612 case MFF_IN_PORT_OXM
:
613 value
->be32
= ofputil_port_to_ofp11(flow
->in_port
.ofp_port
);
615 case MFF_ACTSET_OUTPUT
:
616 value
->be32
= ofputil_port_to_ofp11(flow
->actset_output
);
619 case MFF_SKB_PRIORITY
:
620 value
->be32
= htonl(flow
->skb_priority
);
624 value
->be32
= htonl(flow
->pkt_mark
);
628 value
->be32
= htonl(flow
->regs
[mf
->id
- MFF_REG0
]);
632 value
->be64
= htonll(flow_get_xreg(flow
, mf
->id
- MFF_XREG0
));
636 memcpy(value
->mac
, flow
->dl_src
, ETH_ADDR_LEN
);
640 memcpy(value
->mac
, flow
->dl_dst
, ETH_ADDR_LEN
);
644 value
->be16
= flow
->dl_type
;
648 value
->be16
= flow
->vlan_tci
;
652 value
->be16
= flow
->vlan_tci
& htons(VLAN_VID_MASK
);
655 value
->be16
= flow
->vlan_tci
& htons(VLAN_VID_MASK
| VLAN_CFI
);
658 case MFF_DL_VLAN_PCP
:
660 value
->u8
= vlan_tci_to_pcp(flow
->vlan_tci
);
664 value
->be32
= htonl(mpls_lse_to_label(flow
->mpls_lse
[0]));
668 value
->u8
= mpls_lse_to_tc(flow
->mpls_lse
[0]);
672 value
->u8
= mpls_lse_to_bos(flow
->mpls_lse
[0]);
676 value
->be32
= flow
->nw_src
;
680 value
->be32
= flow
->nw_dst
;
684 value
->ipv6
= flow
->ipv6_src
;
688 value
->ipv6
= flow
->ipv6_dst
;
692 value
->be32
= flow
->ipv6_label
;
696 value
->u8
= flow
->nw_proto
;
700 value
->u8
= flow
->nw_tos
& IP_DSCP_MASK
;
703 case MFF_IP_DSCP_SHIFTED
:
704 value
->u8
= flow
->nw_tos
>> 2;
708 value
->u8
= flow
->nw_tos
& IP_ECN_MASK
;
712 value
->u8
= flow
->nw_ttl
;
716 value
->u8
= flow
->nw_frag
;
720 value
->be16
= htons(flow
->nw_proto
);
724 value
->be32
= flow
->nw_src
;
728 value
->be32
= flow
->nw_dst
;
733 memcpy(value
->mac
, flow
->arp_sha
, ETH_ADDR_LEN
);
738 memcpy(value
->mac
, flow
->arp_tha
, ETH_ADDR_LEN
);
744 value
->be16
= flow
->tp_src
;
750 value
->be16
= flow
->tp_dst
;
754 value
->be16
= flow
->tcp_flags
;
757 case MFF_ICMPV4_TYPE
:
758 case MFF_ICMPV6_TYPE
:
759 value
->u8
= ntohs(flow
->tp_src
);
762 case MFF_ICMPV4_CODE
:
763 case MFF_ICMPV6_CODE
:
764 value
->u8
= ntohs(flow
->tp_dst
);
768 value
->ipv6
= flow
->nd_target
;
777 /* Makes 'match' match field 'mf' exactly, with the value matched taken from
778 * 'value'. The caller is responsible for ensuring that 'match' meets 'mf''s
781 mf_set_value(const struct mf_field
*mf
,
782 const union mf_value
*value
, struct match
*match
)
786 match_set_dp_hash(match
, ntohl(value
->be32
));
789 match_set_recirc_id(match
, ntohl(value
->be32
));
792 match_set_conj_id(match
, ntohl(value
->be32
));
795 match_set_tun_id(match
, value
->be64
);
798 match_set_tun_src(match
, value
->be32
);
801 match_set_tun_dst(match
, value
->be32
);
804 match_set_tun_flags(match
, ntohs(value
->be16
));
807 match_set_tun_gbp_id(match
, value
->be16
);
809 case MFF_TUN_GBP_FLAGS
:
810 match_set_tun_gbp_flags(match
, value
->u8
);
813 match_set_tun_tos(match
, value
->u8
);
816 match_set_tun_ttl(match
, value
->u8
);
820 match_set_metadata(match
, value
->be64
);
824 match_set_in_port(match
, u16_to_ofp(ntohs(value
->be16
)));
827 case MFF_IN_PORT_OXM
: {
829 ofputil_port_from_ofp11(value
->be32
, &port
);
830 match_set_in_port(match
, port
);
833 case MFF_ACTSET_OUTPUT
: {
835 ofputil_port_from_ofp11(value
->be32
, &port
);
836 match_set_actset_output(match
, port
);
840 case MFF_SKB_PRIORITY
:
841 match_set_skb_priority(match
, ntohl(value
->be32
));
845 match_set_pkt_mark(match
, ntohl(value
->be32
));
849 match_set_reg(match
, mf
->id
- MFF_REG0
, ntohl(value
->be32
));
853 match_set_xreg(match
, mf
->id
- MFF_XREG0
, ntohll(value
->be64
));
857 match_set_dl_src(match
, value
->mac
);
861 match_set_dl_dst(match
, value
->mac
);
865 match_set_dl_type(match
, value
->be16
);
869 match_set_dl_tci(match
, value
->be16
);
873 match_set_dl_vlan(match
, value
->be16
);
876 match_set_vlan_vid(match
, value
->be16
);
879 case MFF_DL_VLAN_PCP
:
881 match_set_dl_vlan_pcp(match
, value
->u8
);
885 match_set_mpls_label(match
, 0, value
->be32
);
889 match_set_mpls_tc(match
, 0, value
->u8
);
893 match_set_mpls_bos(match
, 0, value
->u8
);
897 match_set_nw_src(match
, value
->be32
);
901 match_set_nw_dst(match
, value
->be32
);
905 match_set_ipv6_src(match
, &value
->ipv6
);
909 match_set_ipv6_dst(match
, &value
->ipv6
);
913 match_set_ipv6_label(match
, value
->be32
);
917 match_set_nw_proto(match
, value
->u8
);
921 match_set_nw_dscp(match
, value
->u8
);
924 case MFF_IP_DSCP_SHIFTED
:
925 match_set_nw_dscp(match
, value
->u8
<< 2);
929 match_set_nw_ecn(match
, value
->u8
);
933 match_set_nw_ttl(match
, value
->u8
);
937 match_set_nw_frag(match
, value
->u8
);
941 match_set_nw_proto(match
, ntohs(value
->be16
));
945 match_set_nw_src(match
, value
->be32
);
949 match_set_nw_dst(match
, value
->be32
);
954 match_set_arp_sha(match
, value
->mac
);
959 match_set_arp_tha(match
, value
->mac
);
965 match_set_tp_src(match
, value
->be16
);
971 match_set_tp_dst(match
, value
->be16
);
975 match_set_tcp_flags(match
, value
->be16
);
978 case MFF_ICMPV4_TYPE
:
979 case MFF_ICMPV6_TYPE
:
980 match_set_icmp_type(match
, value
->u8
);
983 case MFF_ICMPV4_CODE
:
984 case MFF_ICMPV6_CODE
:
985 match_set_icmp_code(match
, value
->u8
);
989 match_set_nd_target(match
, &value
->ipv6
);
998 /* Unwildcard 'mask' member field described by 'mf'. The caller is
999 * responsible for ensuring that 'mask' meets 'mf''s prerequisites. */
1001 mf_mask_field(const struct mf_field
*mf
, struct flow
*mask
)
1003 static const union mf_value exact_match_mask
= MF_EXACT_MASK_INITIALIZER
;
1005 /* For MFF_DL_VLAN, we cannot send a all 1's to flow_set_dl_vlan()
1006 * as that will be considered as OFP10_VLAN_NONE. So consider it as a
1007 * special case. For the rest, calling mf_set_flow_value() is good
1009 if (mf
->id
== MFF_DL_VLAN
) {
1010 flow_set_dl_vlan(mask
, htons(VLAN_VID_MASK
));
1012 mf_set_flow_value(mf
, &exact_match_mask
, mask
);
1017 field_len(const struct mf_field
*mf
, const union mf_value
*value_
)
1019 const uint8_t *value
= &value_
->u8
;
1022 if (!mf
->variable_len
) {
1030 for (i
= 0; i
< mf
->n_bytes
; i
++) {
1031 if (value
[i
] != 0) {
1036 return mf
->n_bytes
- i
;
1039 /* Returns the effective length of the field. For fixed length fields,
1040 * this is just the defined length. For variable length fields, it is
1041 * the minimum size encoding that retains the same meaning (i.e.
1042 * discarding leading zeros). */
1044 mf_field_len(const struct mf_field
*mf
, const union mf_value
*value
,
1045 const union mf_value
*mask
)
1049 len
= field_len(mf
, value
);
1050 if (mask
&& !is_all_ones(mask
, mf
->n_bytes
)) {
1051 mask_len
= field_len(mf
, mask
);
1052 len
= MAX(len
, mask_len
);
1058 /* Sets 'flow' member field described by 'mf' to 'value'. The caller is
1059 * responsible for ensuring that 'flow' meets 'mf''s prerequisites.*/
1061 mf_set_flow_value(const struct mf_field
*mf
,
1062 const union mf_value
*value
, struct flow
*flow
)
1066 flow
->dp_hash
= ntohl(value
->be32
);
1069 flow
->recirc_id
= ntohl(value
->be32
);
1072 flow
->conj_id
= ntohl(value
->be32
);
1075 flow
->tunnel
.tun_id
= value
->be64
;
1078 flow
->tunnel
.ip_src
= value
->be32
;
1081 flow
->tunnel
.ip_dst
= value
->be32
;
1084 flow
->tunnel
.flags
= ntohs(value
->be16
);
1086 case MFF_TUN_GBP_ID
:
1087 flow
->tunnel
.gbp_id
= value
->be16
;
1089 case MFF_TUN_GBP_FLAGS
:
1090 flow
->tunnel
.gbp_flags
= value
->u8
;
1093 flow
->tunnel
.ip_tos
= value
->u8
;
1096 flow
->tunnel
.ip_ttl
= value
->u8
;
1100 flow
->metadata
= value
->be64
;
1104 flow
->in_port
.ofp_port
= u16_to_ofp(ntohs(value
->be16
));
1107 case MFF_IN_PORT_OXM
:
1108 ofputil_port_from_ofp11(value
->be32
, &flow
->in_port
.ofp_port
);
1110 case MFF_ACTSET_OUTPUT
:
1111 ofputil_port_from_ofp11(value
->be32
, &flow
->actset_output
);
1114 case MFF_SKB_PRIORITY
:
1115 flow
->skb_priority
= ntohl(value
->be32
);
1119 flow
->pkt_mark
= ntohl(value
->be32
);
1123 flow
->regs
[mf
->id
- MFF_REG0
] = ntohl(value
->be32
);
1127 flow_set_xreg(flow
, mf
->id
- MFF_XREG0
, ntohll(value
->be64
));
1131 memcpy(flow
->dl_src
, value
->mac
, ETH_ADDR_LEN
);
1135 memcpy(flow
->dl_dst
, value
->mac
, ETH_ADDR_LEN
);
1139 flow
->dl_type
= value
->be16
;
1143 flow
->vlan_tci
= value
->be16
;
1147 flow_set_dl_vlan(flow
, value
->be16
);
1150 flow_set_vlan_vid(flow
, value
->be16
);
1153 case MFF_DL_VLAN_PCP
:
1155 flow_set_vlan_pcp(flow
, value
->u8
);
1158 case MFF_MPLS_LABEL
:
1159 flow_set_mpls_label(flow
, 0, value
->be32
);
1163 flow_set_mpls_tc(flow
, 0, value
->u8
);
1167 flow_set_mpls_bos(flow
, 0, value
->u8
);
1171 flow
->nw_src
= value
->be32
;
1175 flow
->nw_dst
= value
->be32
;
1179 flow
->ipv6_src
= value
->ipv6
;
1183 flow
->ipv6_dst
= value
->ipv6
;
1186 case MFF_IPV6_LABEL
:
1187 flow
->ipv6_label
= value
->be32
& htonl(IPV6_LABEL_MASK
);
1191 flow
->nw_proto
= value
->u8
;
1195 flow
->nw_tos
&= ~IP_DSCP_MASK
;
1196 flow
->nw_tos
|= value
->u8
& IP_DSCP_MASK
;
1199 case MFF_IP_DSCP_SHIFTED
:
1200 flow
->nw_tos
&= ~IP_DSCP_MASK
;
1201 flow
->nw_tos
|= value
->u8
<< 2;
1205 flow
->nw_tos
&= ~IP_ECN_MASK
;
1206 flow
->nw_tos
|= value
->u8
& IP_ECN_MASK
;
1210 flow
->nw_ttl
= value
->u8
;
1214 flow
->nw_frag
= value
->u8
& FLOW_NW_FRAG_MASK
;
1218 flow
->nw_proto
= ntohs(value
->be16
);
1222 flow
->nw_src
= value
->be32
;
1226 flow
->nw_dst
= value
->be32
;
1231 memcpy(flow
->arp_sha
, value
->mac
, ETH_ADDR_LEN
);
1236 memcpy(flow
->arp_tha
, value
->mac
, ETH_ADDR_LEN
);
1242 flow
->tp_src
= value
->be16
;
1248 flow
->tp_dst
= value
->be16
;
1252 flow
->tcp_flags
= value
->be16
;
1255 case MFF_ICMPV4_TYPE
:
1256 case MFF_ICMPV6_TYPE
:
1257 flow
->tp_src
= htons(value
->u8
);
1260 case MFF_ICMPV4_CODE
:
1261 case MFF_ICMPV6_CODE
:
1262 flow
->tp_dst
= htons(value
->u8
);
1266 flow
->nd_target
= value
->ipv6
;
1275 /* Consider each of 'src', 'mask', and 'dst' as if they were arrays of 8*n
1276 * bits. Then, for each 0 <= i < 8 * n such that mask[i] == 1, sets dst[i] =
1279 apply_mask(const uint8_t *src
, const uint8_t *mask
, uint8_t *dst
, size_t n
)
1283 for (i
= 0; i
< n
; i
++) {
1284 dst
[i
] = (src
[i
] & mask
[i
]) | (dst
[i
] & ~mask
[i
]);
1288 /* Sets 'flow' member field described by 'field' to 'value', except that bits
1289 * for which 'mask' has a 0-bit keep their existing values. The caller is
1290 * responsible for ensuring that 'flow' meets 'field''s prerequisites.*/
1292 mf_set_flow_value_masked(const struct mf_field
*field
,
1293 const union mf_value
*value
,
1294 const union mf_value
*mask
,
1299 mf_get_value(field
, flow
, &tmp
);
1300 apply_mask((const uint8_t *) value
, (const uint8_t *) mask
,
1301 (uint8_t *) &tmp
, field
->n_bytes
);
1302 mf_set_flow_value(field
, &tmp
, flow
);
1305 /* Returns true if 'mf' has a zero value in 'flow', false if it is nonzero.
1307 * The caller is responsible for ensuring that 'flow' meets 'mf''s
1310 mf_is_zero(const struct mf_field
*mf
, const struct flow
*flow
)
1312 union mf_value value
;
1314 mf_get_value(mf
, flow
, &value
);
1315 return is_all_zeros(&value
, mf
->n_bytes
);
1318 /* Makes 'match' wildcard field 'mf'.
1320 * The caller is responsible for ensuring that 'match' meets 'mf''s
1323 mf_set_wild(const struct mf_field
*mf
, struct match
*match
)
1327 match
->flow
.dp_hash
= 0;
1328 match
->wc
.masks
.dp_hash
= 0;
1331 match
->flow
.recirc_id
= 0;
1332 match
->wc
.masks
.recirc_id
= 0;
1335 match
->flow
.conj_id
= 0;
1336 match
->wc
.masks
.conj_id
= 0;
1339 match_set_tun_id_masked(match
, htonll(0), htonll(0));
1342 match_set_tun_src_masked(match
, htonl(0), htonl(0));
1345 match_set_tun_dst_masked(match
, htonl(0), htonl(0));
1348 match_set_tun_flags_masked(match
, 0, 0);
1350 case MFF_TUN_GBP_ID
:
1351 match_set_tun_gbp_id_masked(match
, 0, 0);
1353 case MFF_TUN_GBP_FLAGS
:
1354 match_set_tun_gbp_flags_masked(match
, 0, 0);
1357 match_set_tun_tos_masked(match
, 0, 0);
1360 match_set_tun_ttl_masked(match
, 0, 0);
1364 match_set_metadata_masked(match
, htonll(0), htonll(0));
1368 case MFF_IN_PORT_OXM
:
1369 match
->flow
.in_port
.ofp_port
= 0;
1370 match
->wc
.masks
.in_port
.ofp_port
= 0;
1372 case MFF_ACTSET_OUTPUT
:
1373 match
->flow
.actset_output
= 0;
1374 match
->wc
.masks
.actset_output
= 0;
1377 case MFF_SKB_PRIORITY
:
1378 match
->flow
.skb_priority
= 0;
1379 match
->wc
.masks
.skb_priority
= 0;
1383 match
->flow
.pkt_mark
= 0;
1384 match
->wc
.masks
.pkt_mark
= 0;
1388 match_set_reg_masked(match
, mf
->id
- MFF_REG0
, 0, 0);
1392 match_set_xreg_masked(match
, mf
->id
- MFF_XREG0
, 0, 0);
1396 memset(match
->flow
.dl_src
, 0, ETH_ADDR_LEN
);
1397 memset(match
->wc
.masks
.dl_src
, 0, ETH_ADDR_LEN
);
1401 memset(match
->flow
.dl_dst
, 0, ETH_ADDR_LEN
);
1402 memset(match
->wc
.masks
.dl_dst
, 0, ETH_ADDR_LEN
);
1406 match
->flow
.dl_type
= htons(0);
1407 match
->wc
.masks
.dl_type
= htons(0);
1411 match_set_dl_tci_masked(match
, htons(0), htons(0));
1416 match_set_any_vid(match
);
1419 case MFF_DL_VLAN_PCP
:
1421 match_set_any_pcp(match
);
1424 case MFF_MPLS_LABEL
:
1425 match_set_any_mpls_label(match
, 0);
1429 match_set_any_mpls_tc(match
, 0);
1433 match_set_any_mpls_bos(match
, 0);
1438 match_set_nw_src_masked(match
, htonl(0), htonl(0));
1443 match_set_nw_dst_masked(match
, htonl(0), htonl(0));
1447 memset(&match
->wc
.masks
.ipv6_src
, 0, sizeof match
->wc
.masks
.ipv6_src
);
1448 memset(&match
->flow
.ipv6_src
, 0, sizeof match
->flow
.ipv6_src
);
1452 memset(&match
->wc
.masks
.ipv6_dst
, 0, sizeof match
->wc
.masks
.ipv6_dst
);
1453 memset(&match
->flow
.ipv6_dst
, 0, sizeof match
->flow
.ipv6_dst
);
1456 case MFF_IPV6_LABEL
:
1457 match
->wc
.masks
.ipv6_label
= htonl(0);
1458 match
->flow
.ipv6_label
= htonl(0);
1462 match
->wc
.masks
.nw_proto
= 0;
1463 match
->flow
.nw_proto
= 0;
1467 case MFF_IP_DSCP_SHIFTED
:
1468 match
->wc
.masks
.nw_tos
&= ~IP_DSCP_MASK
;
1469 match
->flow
.nw_tos
&= ~IP_DSCP_MASK
;
1473 match
->wc
.masks
.nw_tos
&= ~IP_ECN_MASK
;
1474 match
->flow
.nw_tos
&= ~IP_ECN_MASK
;
1478 match
->wc
.masks
.nw_ttl
= 0;
1479 match
->flow
.nw_ttl
= 0;
1483 match
->wc
.masks
.nw_frag
|= FLOW_NW_FRAG_MASK
;
1484 match
->flow
.nw_frag
&= ~FLOW_NW_FRAG_MASK
;
1488 match
->wc
.masks
.nw_proto
= 0;
1489 match
->flow
.nw_proto
= 0;
1494 memset(match
->flow
.arp_sha
, 0, ETH_ADDR_LEN
);
1495 memset(match
->wc
.masks
.arp_sha
, 0, ETH_ADDR_LEN
);
1500 memset(match
->flow
.arp_tha
, 0, ETH_ADDR_LEN
);
1501 memset(match
->wc
.masks
.arp_tha
, 0, ETH_ADDR_LEN
);
1507 case MFF_ICMPV4_TYPE
:
1508 case MFF_ICMPV6_TYPE
:
1509 match
->wc
.masks
.tp_src
= htons(0);
1510 match
->flow
.tp_src
= htons(0);
1516 case MFF_ICMPV4_CODE
:
1517 case MFF_ICMPV6_CODE
:
1518 match
->wc
.masks
.tp_dst
= htons(0);
1519 match
->flow
.tp_dst
= htons(0);
1523 match
->wc
.masks
.tcp_flags
= htons(0);
1524 match
->flow
.tcp_flags
= htons(0);
1528 memset(&match
->wc
.masks
.nd_target
, 0,
1529 sizeof match
->wc
.masks
.nd_target
);
1530 memset(&match
->flow
.nd_target
, 0, sizeof match
->flow
.nd_target
);
1539 /* Makes 'match' match field 'mf' with the specified 'value' and 'mask'.
1540 * 'value' specifies a value to match and 'mask' specifies a wildcard pattern,
1541 * with a 1-bit indicating that the corresponding value bit must match and a
1542 * 0-bit indicating a don't-care.
1544 * If 'mask' is NULL or points to all-1-bits, then this call is equivalent to
1545 * mf_set_value(mf, value, match). If 'mask' points to all-0-bits, then this
1546 * call is equivalent to mf_set_wild(mf, match).
1548 * 'mask' must be a valid mask for 'mf' (see mf_is_mask_valid()). The caller
1549 * is responsible for ensuring that 'match' meets 'mf''s prerequisites. */
1550 enum ofputil_protocol
1551 mf_set(const struct mf_field
*mf
,
1552 const union mf_value
*value
, const union mf_value
*mask
,
1553 struct match
*match
)
1555 if (!mask
|| is_all_ones(mask
, mf
->n_bytes
)) {
1556 mf_set_value(mf
, value
, match
);
1557 return mf
->usable_protocols_exact
;
1558 } else if (is_all_zeros(mask
, mf
->n_bytes
)) {
1559 mf_set_wild(mf
, match
);
1560 return OFPUTIL_P_ANY
;
1567 case MFF_IN_PORT_OXM
:
1568 case MFF_ACTSET_OUTPUT
:
1569 case MFF_SKB_PRIORITY
:
1572 case MFF_DL_VLAN_PCP
:
1574 case MFF_MPLS_LABEL
:
1580 case MFF_IP_DSCP_SHIFTED
:
1583 case MFF_ICMPV4_TYPE
:
1584 case MFF_ICMPV4_CODE
:
1585 case MFF_ICMPV6_TYPE
:
1586 case MFF_ICMPV6_CODE
:
1587 return OFPUTIL_P_NONE
;
1590 match_set_dp_hash_masked(match
, ntohl(value
->be32
), ntohl(mask
->be32
));
1593 match_set_tun_id_masked(match
, value
->be64
, mask
->be64
);
1596 match_set_tun_src_masked(match
, value
->be32
, mask
->be32
);
1599 match_set_tun_dst_masked(match
, value
->be32
, mask
->be32
);
1602 match_set_tun_flags_masked(match
, ntohs(value
->be16
), ntohs(mask
->be16
));
1604 case MFF_TUN_GBP_ID
:
1605 match_set_tun_gbp_id_masked(match
, value
->be16
, mask
->be16
);
1607 case MFF_TUN_GBP_FLAGS
:
1608 match_set_tun_gbp_flags_masked(match
, value
->u8
, mask
->u8
);
1611 match_set_tun_ttl_masked(match
, value
->u8
, mask
->u8
);
1614 match_set_tun_tos_masked(match
, value
->u8
, mask
->u8
);
1618 match_set_metadata_masked(match
, value
->be64
, mask
->be64
);
1622 match_set_reg_masked(match
, mf
->id
- MFF_REG0
,
1623 ntohl(value
->be32
), ntohl(mask
->be32
));
1627 match_set_xreg_masked(match
, mf
->id
- MFF_XREG0
,
1628 ntohll(value
->be64
), ntohll(mask
->be64
));
1632 match_set_pkt_mark_masked(match
, ntohl(value
->be32
),
1637 match_set_dl_dst_masked(match
, value
->mac
, mask
->mac
);
1641 match_set_dl_src_masked(match
, value
->mac
, mask
->mac
);
1646 match_set_arp_sha_masked(match
, value
->mac
, mask
->mac
);
1651 match_set_arp_tha_masked(match
, value
->mac
, mask
->mac
);
1655 match_set_dl_tci_masked(match
, value
->be16
, mask
->be16
);
1659 match_set_vlan_vid_masked(match
, value
->be16
, mask
->be16
);
1663 match_set_nw_src_masked(match
, value
->be32
, mask
->be32
);
1667 match_set_nw_dst_masked(match
, value
->be32
, mask
->be32
);
1671 match_set_ipv6_src_masked(match
, &value
->ipv6
, &mask
->ipv6
);
1675 match_set_ipv6_dst_masked(match
, &value
->ipv6
, &mask
->ipv6
);
1678 case MFF_IPV6_LABEL
:
1679 if ((mask
->be32
& htonl(IPV6_LABEL_MASK
)) == htonl(IPV6_LABEL_MASK
)) {
1680 mf_set_value(mf
, value
, match
);
1682 match_set_ipv6_label_masked(match
, value
->be32
, mask
->be32
);
1687 match_set_nd_target_masked(match
, &value
->ipv6
, &mask
->ipv6
);
1691 match_set_nw_frag_masked(match
, value
->u8
, mask
->u8
);
1695 match_set_nw_src_masked(match
, value
->be32
, mask
->be32
);
1699 match_set_nw_dst_masked(match
, value
->be32
, mask
->be32
);
1705 match_set_tp_src_masked(match
, value
->be16
, mask
->be16
);
1711 match_set_tp_dst_masked(match
, value
->be16
, mask
->be16
);
1715 match_set_tcp_flags_masked(match
, value
->be16
, mask
->be16
);
1723 return ((mf
->usable_protocols_bitwise
== mf
->usable_protocols_cidr
1724 || ip_is_cidr(mask
->be32
))
1725 ? mf
->usable_protocols_cidr
1726 : mf
->usable_protocols_bitwise
);
1730 mf_check__(const struct mf_subfield
*sf
, const struct flow
*flow
,
1734 VLOG_WARN_RL(&rl
, "unknown %s field", type
);
1735 return OFPERR_OFPBAC_BAD_SET_TYPE
;
1736 } else if (!sf
->n_bits
) {
1737 VLOG_WARN_RL(&rl
, "zero bit %s field %s", type
, sf
->field
->name
);
1738 return OFPERR_OFPBAC_BAD_SET_LEN
;
1739 } else if (sf
->ofs
>= sf
->field
->n_bits
) {
1740 VLOG_WARN_RL(&rl
, "bit offset %d exceeds %d-bit width of %s field %s",
1741 sf
->ofs
, sf
->field
->n_bits
, type
, sf
->field
->name
);
1742 return OFPERR_OFPBAC_BAD_SET_LEN
;
1743 } else if (sf
->ofs
+ sf
->n_bits
> sf
->field
->n_bits
) {
1744 VLOG_WARN_RL(&rl
, "bit offset %d and width %d exceeds %d-bit width "
1745 "of %s field %s", sf
->ofs
, sf
->n_bits
,
1746 sf
->field
->n_bits
, type
, sf
->field
->name
);
1747 return OFPERR_OFPBAC_BAD_SET_LEN
;
1748 } else if (flow
&& !mf_are_prereqs_ok(sf
->field
, flow
)) {
1749 VLOG_WARN_RL(&rl
, "%s field %s lacks correct prerequisites",
1750 type
, sf
->field
->name
);
1751 return OFPERR_OFPBAC_MATCH_INCONSISTENT
;
1757 /* Checks whether 'sf' is valid for reading a subfield out of 'flow'. Returns
1758 * 0 if so, otherwise an OpenFlow error code (e.g. as returned by
1761 mf_check_src(const struct mf_subfield
*sf
, const struct flow
*flow
)
1763 return mf_check__(sf
, flow
, "source");
1766 /* Checks whether 'sf' is valid for writing a subfield into 'flow'. Returns 0
1767 * if so, otherwise an OpenFlow error code (e.g. as returned by
1770 mf_check_dst(const struct mf_subfield
*sf
, const struct flow
*flow
)
1772 int error
= mf_check__(sf
, flow
, "destination");
1773 if (!error
&& !sf
->field
->writable
) {
1774 VLOG_WARN_RL(&rl
, "destination field %s is not writable",
1776 return OFPERR_OFPBAC_BAD_SET_ARGUMENT
;
1781 /* Copies the value and wildcard bit pattern for 'mf' from 'match' into the
1782 * 'value' and 'mask', respectively. */
1784 mf_get(const struct mf_field
*mf
, const struct match
*match
,
1785 union mf_value
*value
, union mf_value
*mask
)
1787 mf_get_value(mf
, &match
->flow
, value
);
1788 mf_get_mask(mf
, &match
->wc
, mask
);
1792 mf_from_integer_string(const struct mf_field
*mf
, const char *s
,
1793 uint8_t *valuep
, uint8_t *maskp
)
1796 const char *err_str
= "";
1799 err
= parse_int_string(s
, valuep
, mf
->n_bytes
, &tail
);
1800 if (err
|| (*tail
!= '\0' && *tail
!= '/')) {
1806 err
= parse_int_string(tail
+ 1, maskp
, mf
->n_bytes
, &tail
);
1807 if (err
|| *tail
!= '\0') {
1812 memset(maskp
, 0xff, mf
->n_bytes
);
1818 if (err
== ERANGE
) {
1819 return xasprintf("%s: %s too large for %u-byte field %s",
1820 s
, err_str
, mf
->n_bytes
, mf
->name
);
1822 return xasprintf("%s: bad syntax for %s %s", s
, mf
->name
, err_str
);
1827 mf_from_ethernet_string(const struct mf_field
*mf
, const char *s
,
1828 uint8_t mac
[ETH_ADDR_LEN
],
1829 uint8_t mask
[ETH_ADDR_LEN
])
1833 ovs_assert(mf
->n_bytes
== ETH_ADDR_LEN
);
1836 if (ovs_scan(s
, ETH_ADDR_SCAN_FMT
"%n", ETH_ADDR_SCAN_ARGS(mac
), &n
)
1837 && n
== strlen(s
)) {
1838 memset(mask
, 0xff, ETH_ADDR_LEN
);
1843 if (ovs_scan(s
, ETH_ADDR_SCAN_FMT
"/"ETH_ADDR_SCAN_FMT
"%n",
1844 ETH_ADDR_SCAN_ARGS(mac
), ETH_ADDR_SCAN_ARGS(mask
), &n
)
1845 && n
== strlen(s
)) {
1849 return xasprintf("%s: invalid Ethernet address", s
);
1853 mf_from_ipv4_string(const struct mf_field
*mf
, const char *s
,
1854 ovs_be32
*ip
, ovs_be32
*mask
)
1858 ovs_assert(mf
->n_bytes
== sizeof *ip
);
1860 if (ovs_scan(s
, IP_SCAN_FMT
"/"IP_SCAN_FMT
,
1861 IP_SCAN_ARGS(ip
), IP_SCAN_ARGS(mask
))) {
1863 } else if (ovs_scan(s
, IP_SCAN_FMT
"/%d", IP_SCAN_ARGS(ip
), &prefix
)) {
1864 if (prefix
<= 0 || prefix
> 32) {
1865 return xasprintf("%s: network prefix bits not between 0 and "
1868 *mask
= be32_prefix_mask(prefix
);
1869 } else if (ovs_scan(s
, IP_SCAN_FMT
, IP_SCAN_ARGS(ip
))) {
1870 *mask
= OVS_BE32_MAX
;
1872 return xasprintf("%s: invalid IP address", s
);
1878 mf_from_ipv6_string(const struct mf_field
*mf
, const char *s
,
1879 struct in6_addr
*value
, struct in6_addr
*mask
)
1881 char *str
= xstrdup(s
);
1882 char *save_ptr
= NULL
;
1883 const char *name
, *netmask
;
1886 ovs_assert(mf
->n_bytes
== sizeof *value
);
1888 name
= strtok_r(str
, "/", &save_ptr
);
1889 retval
= name
? lookup_ipv6(name
, value
) : EINVAL
;
1893 err
= xasprintf("%s: could not convert to IPv6 address", str
);
1899 netmask
= strtok_r(NULL
, "/", &save_ptr
);
1901 if (inet_pton(AF_INET6
, netmask
, mask
) != 1) {
1902 int prefix
= atoi(netmask
);
1903 if (prefix
<= 0 || prefix
> 128) {
1905 return xasprintf("%s: prefix bits not between 1 and 128", s
);
1907 *mask
= ipv6_create_mask(prefix
);
1911 *mask
= in6addr_exact
;
1919 mf_from_ofp_port_string(const struct mf_field
*mf
, const char *s
,
1920 ovs_be16
*valuep
, ovs_be16
*maskp
)
1924 ovs_assert(mf
->n_bytes
== sizeof(ovs_be16
));
1926 if (ofputil_port_from_string(s
, &port
)) {
1927 *valuep
= htons(ofp_to_u16(port
));
1928 *maskp
= OVS_BE16_MAX
;
1931 return xasprintf("%s: port value out of range for %s", s
, mf
->name
);
1935 mf_from_ofp_port_string32(const struct mf_field
*mf
, const char *s
,
1936 ovs_be32
*valuep
, ovs_be32
*maskp
)
1940 ovs_assert(mf
->n_bytes
== sizeof(ovs_be32
));
1941 if (ofputil_port_from_string(s
, &port
)) {
1942 *valuep
= ofputil_port_to_ofp11(port
);
1943 *maskp
= OVS_BE32_MAX
;
1946 return xasprintf("%s: port value out of range for %s", s
, mf
->name
);
1949 struct frag_handling
{
1955 static const struct frag_handling all_frags
[] = {
1956 #define A FLOW_NW_FRAG_ANY
1957 #define L FLOW_NW_FRAG_LATER
1958 /* name mask value */
1961 { "first", A
|L
, A
},
1962 { "later", A
|L
, A
|L
},
1967 { "not_later", L
, 0 },
1974 mf_from_frag_string(const char *s
, uint8_t *valuep
, uint8_t *maskp
)
1976 const struct frag_handling
*h
;
1978 for (h
= all_frags
; h
< &all_frags
[ARRAY_SIZE(all_frags
)]; h
++) {
1979 if (!strcasecmp(s
, h
->name
)) {
1980 /* We force the upper bits of the mask on to make mf_parse_value()
1981 * happy (otherwise it will never think it's an exact match.) */
1982 *maskp
= h
->mask
| ~FLOW_NW_FRAG_MASK
;
1988 return xasprintf("%s: unknown fragment type (valid types are \"no\", "
1989 "\"yes\", \"first\", \"later\", \"not_first\"", s
);
1993 parse_flow_tun_flags(const char *s_
, const char *(*bit_to_string
)(uint32_t),
1996 uint32_t result
= 0;
1997 char *save_ptr
= NULL
;
2000 char *s
= xstrdup(s_
);
2002 for (name
= strtok_r((char *)s
, " |", &save_ptr
); name
;
2003 name
= strtok_r(NULL
, " |", &save_ptr
)) {
2005 unsigned long long int flags
;
2008 if (ovs_scan(name
, "%lli", &flags
)) {
2012 name_len
= strlen(name
);
2013 for (bit
= 1; bit
; bit
<<= 1) {
2014 const char *fname
= bit_to_string(bit
);
2021 len
= strlen(fname
);
2022 if (len
!= name_len
) {
2025 if (!strncmp(name
, fname
, len
)) {
2037 *res
= htons(result
);
2044 mf_from_tun_flags_string(const char *s
, ovs_be16
*valuep
, ovs_be16
*maskp
)
2046 if (!parse_flow_tun_flags(s
, flow_tun_flag_to_string
, valuep
)) {
2047 *maskp
= OVS_BE16_MAX
;
2051 return xasprintf("%s: unknown tunnel flags (valid flags are \"df\", "
2052 "\"csum\", \"key\")", s
);
2056 mf_from_tcp_flags_string(const char *s
, ovs_be16
*flagsp
, ovs_be16
*maskp
)
2063 if (ovs_scan(s
, "%"SCNi16
"/%"SCNi16
"%n", &flags
, &mask
, &n
) && !s
[n
]) {
2064 *flagsp
= htons(flags
);
2065 *maskp
= htons(mask
);
2068 if (ovs_scan(s
, "%"SCNi16
"%n", &flags
, &n
) && !s
[n
]) {
2069 *flagsp
= htons(flags
);
2070 *maskp
= OVS_BE16_MAX
;
2074 while (*s
!= '\0') {
2086 return xasprintf("%s: TCP flag must be preceded by '+' (for SET) "
2087 "or '-' (NOT SET)", s
);
2091 name_len
= strcspn(s
,"+-");
2093 for (bit
= 1; bit
; bit
<<= 1) {
2094 const char *fname
= packet_tcp_flag_to_string(bit
);
2101 len
= strlen(fname
);
2102 if (len
!= name_len
) {
2105 if (!strncmp(s
, fname
, len
)) {
2107 return xasprintf("%s: Each TCP flag can be specified only "
2119 return xasprintf("%s: unknown TCP flag(s)", s
);
2124 *flagsp
= htons(flags
);
2125 *maskp
= htons(mask
);
2130 /* Parses 's', a string value for field 'mf', into 'value' and 'mask'. Returns
2131 * NULL if successful, otherwise a malloc()'d string describing the error. */
2133 mf_parse(const struct mf_field
*mf
, const char *s
,
2134 union mf_value
*value
, union mf_value
*mask
)
2138 if (!strcmp(s
, "*")) {
2139 memset(value
, 0, mf
->n_bytes
);
2140 memset(mask
, 0, mf
->n_bytes
);
2144 switch (mf
->string
) {
2146 case MFS_HEXADECIMAL
:
2147 error
= mf_from_integer_string(mf
, s
,
2148 (uint8_t *) value
, (uint8_t *) mask
);
2152 error
= mf_from_ethernet_string(mf
, s
, value
->mac
, mask
->mac
);
2156 error
= mf_from_ipv4_string(mf
, s
, &value
->be32
, &mask
->be32
);
2160 error
= mf_from_ipv6_string(mf
, s
, &value
->ipv6
, &mask
->ipv6
);
2164 error
= mf_from_ofp_port_string(mf
, s
, &value
->be16
, &mask
->be16
);
2167 case MFS_OFP_PORT_OXM
:
2168 error
= mf_from_ofp_port_string32(mf
, s
, &value
->be32
, &mask
->be32
);
2172 error
= mf_from_frag_string(s
, &value
->u8
, &mask
->u8
);
2176 ovs_assert(mf
->n_bytes
== sizeof(ovs_be16
));
2177 error
= mf_from_tun_flags_string(s
, &value
->be16
, &mask
->be16
);
2181 ovs_assert(mf
->n_bytes
== sizeof(ovs_be16
));
2182 error
= mf_from_tcp_flags_string(s
, &value
->be16
, &mask
->be16
);
2189 if (!error
&& !mf_is_mask_valid(mf
, mask
)) {
2190 error
= xasprintf("%s: invalid mask for field %s", s
, mf
->name
);
2195 /* Parses 's', a string value for field 'mf', into 'value'. Returns NULL if
2196 * successful, otherwise a malloc()'d string describing the error. */
2198 mf_parse_value(const struct mf_field
*mf
, const char *s
, union mf_value
*value
)
2200 union mf_value mask
;
2203 error
= mf_parse(mf
, s
, value
, &mask
);
2208 if (!is_all_ones((const uint8_t *) &mask
, mf
->n_bytes
)) {
2209 return xasprintf("%s: wildcards not allowed here", s
);
2215 mf_format_integer_string(const struct mf_field
*mf
, const uint8_t *valuep
,
2216 const uint8_t *maskp
, struct ds
*s
)
2218 if (mf
->string
== MFS_HEXADECIMAL
) {
2219 ds_put_hex(s
, valuep
, mf
->n_bytes
);
2221 unsigned long long int integer
= 0;
2224 ovs_assert(mf
->n_bytes
<= 8);
2225 for (i
= 0; i
< mf
->n_bytes
; i
++) {
2226 integer
= (integer
<< 8) | valuep
[i
];
2228 ds_put_format(s
, "%lld", integer
);
2232 /* I guess we could write the mask in decimal for MFS_DECIMAL but I'm
2233 * not sure that that a bit-mask written in decimal is ever easier to
2234 * understand than the same bit-mask written in hexadecimal. */
2235 ds_put_char(s
, '/');
2236 ds_put_hex(s
, maskp
, mf
->n_bytes
);
2241 mf_format_frag_string(uint8_t value
, uint8_t mask
, struct ds
*s
)
2243 const struct frag_handling
*h
;
2245 mask
&= FLOW_NW_FRAG_MASK
;
2248 for (h
= all_frags
; h
< &all_frags
[ARRAY_SIZE(all_frags
)]; h
++) {
2249 if (value
== h
->value
&& mask
== h
->mask
) {
2250 ds_put_cstr(s
, h
->name
);
2254 ds_put_cstr(s
, "<error>");
2258 mf_format_tnl_flags_string(const ovs_be16
*valuep
, struct ds
*s
)
2260 format_flags(s
, flow_tun_flag_to_string
, ntohs(*valuep
), '|');
2264 mf_format_tcp_flags_string(ovs_be16 value
, ovs_be16 mask
, struct ds
*s
)
2266 format_flags_masked(s
, NULL
, packet_tcp_flag_to_string
, ntohs(value
),
2270 /* Appends to 's' a string representation of field 'mf' whose value is in
2271 * 'value' and 'mask'. 'mask' may be NULL to indicate an exact match. */
2273 mf_format(const struct mf_field
*mf
,
2274 const union mf_value
*value
, const union mf_value
*mask
,
2278 if (is_all_zeros(mask
, mf
->n_bytes
)) {
2279 ds_put_cstr(s
, "ANY");
2281 } else if (is_all_ones(mask
, mf
->n_bytes
)) {
2286 switch (mf
->string
) {
2287 case MFS_OFP_PORT_OXM
:
2290 ofputil_port_from_ofp11(value
->be32
, &port
);
2291 ofputil_format_port(port
, s
);
2297 ofputil_format_port(u16_to_ofp(ntohs(value
->be16
)), s
);
2302 case MFS_HEXADECIMAL
:
2303 mf_format_integer_string(mf
, (uint8_t *) value
, (uint8_t *) mask
, s
);
2307 eth_format_masked(value
->mac
, mask
->mac
, s
);
2311 ip_format_masked(value
->be32
, mask
? mask
->be32
: OVS_BE32_MAX
, s
);
2315 print_ipv6_masked(s
, &value
->ipv6
, mask
? &mask
->ipv6
: NULL
);
2319 mf_format_frag_string(value
->u8
, mask
? mask
->u8
: UINT8_MAX
, s
);
2323 mf_format_tnl_flags_string(&value
->be16
, s
);
2327 mf_format_tcp_flags_string(value
->be16
,
2328 mask
? mask
->be16
: OVS_BE16_MAX
, s
);
2336 /* Makes subfield 'sf' within 'flow' exactly match the 'sf->n_bits'
2337 * least-significant bits in 'x'.
2340 mf_write_subfield_flow(const struct mf_subfield
*sf
,
2341 const union mf_subvalue
*x
, struct flow
*flow
)
2343 const struct mf_field
*field
= sf
->field
;
2344 union mf_value value
;
2346 mf_get_value(field
, flow
, &value
);
2347 bitwise_copy(x
, sizeof *x
, 0, &value
, field
->n_bytes
,
2348 sf
->ofs
, sf
->n_bits
);
2349 mf_set_flow_value(field
, &value
, flow
);
2352 /* Makes subfield 'sf' within 'match' exactly match the 'sf->n_bits'
2353 * least-significant bits in 'x'.
2356 mf_write_subfield(const struct mf_subfield
*sf
, const union mf_subvalue
*x
,
2357 struct match
*match
)
2359 const struct mf_field
*field
= sf
->field
;
2360 union mf_value value
, mask
;
2362 mf_get(field
, match
, &value
, &mask
);
2363 bitwise_copy(x
, sizeof *x
, 0, &value
, field
->n_bytes
, sf
->ofs
, sf
->n_bits
);
2364 bitwise_one ( &mask
, field
->n_bytes
, sf
->ofs
, sf
->n_bits
);
2365 mf_set(field
, &value
, &mask
, match
);
2368 /* 'v' and 'm' correspond to values of 'field'. This function copies them into
2369 * 'match' in the correspond positions. */
2371 mf_mask_subfield(const struct mf_field
*field
,
2372 const union mf_subvalue
*v
,
2373 const union mf_subvalue
*m
,
2374 struct match
*match
)
2376 union mf_value value
, mask
;
2378 mf_get(field
, match
, &value
, &mask
);
2379 bitwise_copy(v
, sizeof *v
, 0, &value
, field
->n_bytes
, 0, field
->n_bits
);
2380 bitwise_copy(m
, sizeof *m
, 0, &mask
, field
->n_bytes
, 0, field
->n_bits
);
2381 mf_set(field
, &value
, &mask
, match
);
2384 /* Initializes 'x' to the value of 'sf' within 'flow'. 'sf' must be valid for
2385 * reading 'flow', e.g. as checked by mf_check_src(). */
2387 mf_read_subfield(const struct mf_subfield
*sf
, const struct flow
*flow
,
2388 union mf_subvalue
*x
)
2390 union mf_value value
;
2392 mf_get_value(sf
->field
, flow
, &value
);
2394 memset(x
, 0, sizeof *x
);
2395 bitwise_copy(&value
, sf
->field
->n_bytes
, sf
->ofs
,
2400 /* Returns the value of 'sf' within 'flow'. 'sf' must be valid for reading
2401 * 'flow', e.g. as checked by mf_check_src() and sf->n_bits must be 64 or
2404 mf_get_subfield(const struct mf_subfield
*sf
, const struct flow
*flow
)
2406 union mf_value value
;
2408 mf_get_value(sf
->field
, flow
, &value
);
2409 return bitwise_get(&value
, sf
->field
->n_bytes
, sf
->ofs
, sf
->n_bits
);
2413 mf_format_subvalue(const union mf_subvalue
*subvalue
, struct ds
*s
)
2415 ds_put_hex(s
, subvalue
->u8
, sizeof subvalue
->u8
);
2419 field_array_set(enum mf_field_id id
, const union mf_value
*value
,
2420 struct field_array
*fa
)
2422 ovs_assert(id
< MFF_N_IDS
);
2423 bitmap_set1(fa
->used
.bm
, id
);
2424 fa
->value
[id
] = *value
;