2 * Copyright (c) 2011, 2012, 2013, 2014, 2015 Nicira, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include "meta-flow.h"
23 #include <netinet/icmp6.h>
24 #include <netinet/ip6.h>
26 #include "classifier.h"
27 #include "dynamic-string.h"
29 #include "ofp-errors.h"
31 #include "ovs-thread.h"
35 #include "socket-util.h"
36 #include "tun-metadata.h"
37 #include "unaligned.h"
39 #include "openvswitch/vlog.h"
41 VLOG_DEFINE_THIS_MODULE(meta_flow);
43 #define FLOW_U32OFS(FIELD) \
44 offsetof(struct flow, FIELD) % 4 ? -1 : offsetof(struct flow, FIELD) / 4
46 #define MF_FIELD_SIZES(MEMBER) \
47 sizeof ((union mf_value *)0)->MEMBER, \
48 8 * sizeof ((union mf_value *)0)->MEMBER
50 extern const struct mf_field mf_fields[MFF_N_IDS]; /* Silence a warning. */
52 const struct mf_field mf_fields[MFF_N_IDS] = {
53 #include "meta-flow.inc"
56 /* Maps from an mf_field's 'name' or 'extra_name' to the mf_field. */
57 static struct shash mf_by_name;
59 /* Rate limit for parse errors. These always indicate a bug in an OpenFlow
60 * controller and so there's not much point in showing a lot of them. */
61 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
63 #define MF_VALUE_EXACT_8 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
64 #define MF_VALUE_EXACT_16 MF_VALUE_EXACT_8, MF_VALUE_EXACT_8
65 #define MF_VALUE_EXACT_32 MF_VALUE_EXACT_16, MF_VALUE_EXACT_16
66 #define MF_VALUE_EXACT_64 MF_VALUE_EXACT_32, MF_VALUE_EXACT_32
67 #define MF_VALUE_EXACT_128 MF_VALUE_EXACT_64, MF_VALUE_EXACT_64
68 #define MF_VALUE_EXACT_INITIALIZER { .tun_metadata = { MF_VALUE_EXACT_128 } }
70 const union mf_value exact_match_mask = MF_VALUE_EXACT_INITIALIZER;
72 static void nxm_init(void);
74 /* Returns the field with the given 'name', or a null pointer if no field has
76 const struct mf_field *
77 mf_from_name(const char *name)
80 return shash_find_data(&mf_by_name, name);
88 shash_init(&mf_by_name);
89 for (i = 0; i < MFF_N_IDS; i++) {
90 const struct mf_field *mf = &mf_fields[i];
92 ovs_assert(mf->id == i); /* Fields must be in the enum order. */
94 shash_add_once(&mf_by_name, mf->name, mf);
96 shash_add_once(&mf_by_name, mf->extra_name, mf);
104 static pthread_once_t once = PTHREAD_ONCE_INIT;
105 pthread_once(&once, nxm_do_init);
108 /* Consider the two value/mask pairs 'a_value/a_mask' and 'b_value/b_mask' as
109 * restrictions on a field's value. Then, this function initializes
110 * 'dst_value/dst_mask' such that it combines the restrictions of both pairs.
111 * This is not always possible, i.e. if one pair insists on a value of 0 in
112 * some bit and the other pair insists on a value of 1 in that bit. This
113 * function returns false in a case where the combined restriction is
114 * impossible (in which case 'dst_value/dst_mask' is not fully initialized),
117 * (As usually true for value/mask pairs in OVS, any 1-bit in a value must have
118 * a corresponding 1-bit in its mask.) */
120 mf_subvalue_intersect(const union mf_subvalue *a_value,
121 const union mf_subvalue *a_mask,
122 const union mf_subvalue *b_value,
123 const union mf_subvalue *b_mask,
124 union mf_subvalue *dst_value,
125 union mf_subvalue *dst_mask)
127 for (int i = 0; i < ARRAY_SIZE(a_value->be64); i++) {
128 ovs_be64 av = a_value->be64[i];
129 ovs_be64 am = a_mask->be64[i];
130 ovs_be64 bv = b_value->be64[i];
131 ovs_be64 bm = b_mask->be64[i];
132 ovs_be64 *dv = &dst_value->be64[i];
133 ovs_be64 *dm = &dst_mask->be64[i];
135 if ((av ^ bv) & (am & bm)) {
144 /* Returns the "number of bits" in 'v', e.g. 1 if only the lowest-order bit is
145 * set, 2 if the second-lowest-order bit is set, and so on. */
147 mf_subvalue_width(const union mf_subvalue *v)
149 return 1 + bitwise_rscan(v, sizeof *v, true, sizeof *v * 8 - 1, -1);
152 /* For positive 'n', shifts the bits in 'value' 'n' bits to the left, and for
153 * negative 'n', shifts the bits '-n' bits to the right. */
155 mf_subvalue_shift(union mf_subvalue *value, int n)
158 union mf_subvalue tmp;
159 memset(&tmp, 0, sizeof tmp);
161 if (n > 0 && n < 8 * sizeof tmp) {
162 bitwise_copy(value, sizeof *value, 0,
165 } else if (n < 0 && n > -8 * sizeof tmp) {
166 bitwise_copy(value, sizeof *value, -n,
174 /* Returns true if 'wc' wildcards all the bits in field 'mf', false if 'wc'
175 * specifies at least one bit in the field.
177 * The caller is responsible for ensuring that 'wc' corresponds to a flow that
178 * meets 'mf''s prerequisites. */
180 mf_is_all_wild(const struct mf_field *mf, const struct flow_wildcards *wc)
184 return !wc->masks.dp_hash;
186 return !wc->masks.recirc_id;
188 return !wc->masks.conj_id;
190 return !wc->masks.tunnel.ip_src;
192 return !wc->masks.tunnel.ip_dst;
194 return !wc->masks.tunnel.tun_id;
196 return !wc->masks.tunnel.ip_tos;
198 return !wc->masks.tunnel.ip_ttl;
200 return !(wc->masks.tunnel.flags & FLOW_TNL_PUB_F_MASK);
202 return !wc->masks.tunnel.gbp_id;
203 case MFF_TUN_GBP_FLAGS:
204 return !wc->masks.tunnel.gbp_flags;
205 CASE_MFF_TUN_METADATA:
206 return !ULLONG_GET(wc->masks.tunnel.metadata.present.map,
207 mf->id - MFF_TUN_METADATA0);
209 return !wc->masks.metadata;
211 case MFF_IN_PORT_OXM:
212 return !wc->masks.in_port.ofp_port;
213 case MFF_SKB_PRIORITY:
214 return !wc->masks.skb_priority;
216 return !wc->masks.pkt_mark;
218 return !wc->masks.regs[mf->id - MFF_REG0];
220 return !flow_get_xreg(&wc->masks, mf->id - MFF_XREG0);
221 case MFF_ACTSET_OUTPUT:
222 return !wc->masks.actset_output;
225 return eth_addr_is_zero(wc->masks.dl_src);
227 return eth_addr_is_zero(wc->masks.dl_dst);
229 return !wc->masks.dl_type;
233 return eth_addr_is_zero(wc->masks.arp_sha);
237 return eth_addr_is_zero(wc->masks.arp_tha);
240 return !wc->masks.vlan_tci;
242 return !(wc->masks.vlan_tci & htons(VLAN_VID_MASK));
244 return !(wc->masks.vlan_tci & htons(VLAN_VID_MASK | VLAN_CFI));
245 case MFF_DL_VLAN_PCP:
247 return !(wc->masks.vlan_tci & htons(VLAN_PCP_MASK));
250 return !(wc->masks.mpls_lse[0] & htonl(MPLS_LABEL_MASK));
252 return !(wc->masks.mpls_lse[0] & htonl(MPLS_TC_MASK));
254 return !(wc->masks.mpls_lse[0] & htonl(MPLS_BOS_MASK));
257 return !wc->masks.nw_src;
259 return !wc->masks.nw_dst;
262 return ipv6_mask_is_any(&wc->masks.ipv6_src);
264 return ipv6_mask_is_any(&wc->masks.ipv6_dst);
267 return !wc->masks.ipv6_label;
270 return !wc->masks.nw_proto;
272 case MFF_IP_DSCP_SHIFTED:
273 return !(wc->masks.nw_tos & IP_DSCP_MASK);
275 return !(wc->masks.nw_tos & IP_ECN_MASK);
277 return !wc->masks.nw_ttl;
280 return ipv6_mask_is_any(&wc->masks.nd_target);
283 return !(wc->masks.nw_frag & FLOW_NW_FRAG_MASK);
286 return !wc->masks.nw_proto;
288 return !wc->masks.nw_src;
290 return !wc->masks.nw_dst;
295 case MFF_ICMPV4_TYPE:
296 case MFF_ICMPV6_TYPE:
297 return !wc->masks.tp_src;
301 case MFF_ICMPV4_CODE:
302 case MFF_ICMPV6_CODE:
303 return !wc->masks.tp_dst;
305 return !wc->masks.tcp_flags;
313 /* Initializes 'mask' with the wildcard bit pattern for field 'mf' within 'wc'.
314 * Each bit in 'mask' will be set to 1 if the bit is significant for matching
315 * purposes, or to 0 if it is wildcarded.
317 * The caller is responsible for ensuring that 'wc' corresponds to a flow that
318 * meets 'mf''s prerequisites. */
320 mf_get_mask(const struct mf_field *mf, const struct flow_wildcards *wc,
321 union mf_value *mask)
323 mf_get_value(mf, &wc->masks, mask);
326 /* Tests whether 'mask' is a valid wildcard bit pattern for 'mf'. Returns true
327 * if the mask is valid, false otherwise. */
329 mf_is_mask_valid(const struct mf_field *mf, const union mf_value *mask)
331 switch (mf->maskable) {
333 return (is_all_zeros(mask, mf->n_bytes) ||
334 is_all_ones(mask, mf->n_bytes));
343 /* Returns true if 'flow' meets the prerequisites for 'mf', false otherwise. */
345 mf_are_prereqs_ok(const struct mf_field *mf, const struct flow *flow)
347 switch (mf->prereqs) {
352 return (flow->dl_type == htons(ETH_TYPE_ARP) ||
353 flow->dl_type == htons(ETH_TYPE_RARP));
355 return flow->dl_type == htons(ETH_TYPE_IP);
357 return flow->dl_type == htons(ETH_TYPE_IPV6);
359 return (flow->vlan_tci & htons(VLAN_CFI)) != 0;
361 return eth_type_mpls(flow->dl_type);
363 return is_ip_any(flow);
366 return is_ip_any(flow) && flow->nw_proto == IPPROTO_TCP
367 && !(flow->nw_frag & FLOW_NW_FRAG_LATER);
369 return is_ip_any(flow) && flow->nw_proto == IPPROTO_UDP
370 && !(flow->nw_frag & FLOW_NW_FRAG_LATER);
372 return is_ip_any(flow) && flow->nw_proto == IPPROTO_SCTP
373 && !(flow->nw_frag & FLOW_NW_FRAG_LATER);
375 return is_icmpv4(flow);
377 return is_icmpv6(flow);
380 return (is_icmpv6(flow)
381 && flow->tp_dst == htons(0)
382 && (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT) ||
383 flow->tp_src == htons(ND_NEIGHBOR_ADVERT)));
385 return (is_icmpv6(flow)
386 && flow->tp_dst == htons(0)
387 && (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT)));
389 return (is_icmpv6(flow)
390 && flow->tp_dst == htons(0)
391 && (flow->tp_src == htons(ND_NEIGHBOR_ADVERT)));
397 /* Set field and it's prerequisities in the mask.
398 * This is only ever called for writeable 'mf's, but we do not make the
399 * distinction here. */
401 mf_mask_field_and_prereqs(const struct mf_field *mf, struct flow_wildcards *wc)
403 mf_set_flow_value(mf, &exact_match_mask, &wc->masks);
405 switch (mf->prereqs) {
409 WC_MASK_FIELD(wc, tp_src);
410 WC_MASK_FIELD(wc, tp_dst);
417 /* nw_frag always unwildcarded. */
418 WC_MASK_FIELD(wc, nw_proto);
425 /* dl_type always unwildcarded. */
428 WC_MASK_FIELD_MASK(wc, vlan_tci, htons(VLAN_CFI));
435 /* Set bits of 'bm' corresponding to the field 'mf' and it's prerequisities. */
437 mf_bitmap_set_field_and_prereqs(const struct mf_field *mf, struct mf_bitmap *bm)
439 bitmap_set1(bm->bm, mf->id);
441 switch (mf->prereqs) {
445 bitmap_set1(bm->bm, MFF_TCP_SRC);
446 bitmap_set1(bm->bm, MFF_TCP_DST);
453 /* nw_frag always unwildcarded. */
454 bitmap_set1(bm->bm, MFF_IP_PROTO);
461 bitmap_set1(bm->bm, MFF_ETH_TYPE);
464 bitmap_set1(bm->bm, MFF_VLAN_TCI);
471 /* Returns true if 'value' may be a valid value *as part of a masked match*,
474 * A value is not rejected just because it is not valid for the field in
475 * question, but only if it doesn't make sense to test the bits in question at
476 * all. For example, the MFF_VLAN_TCI field will never have a nonzero value
477 * without the VLAN_CFI bit being set, but we can't reject those values because
478 * it is still legitimate to test just for those bits (see the documentation
479 * for NXM_OF_VLAN_TCI in nicira-ext.h). On the other hand, there is never a
480 * reason to set the low bit of MFF_IP_DSCP to 1, so we reject that. */
482 mf_is_value_valid(const struct mf_field *mf, const union mf_value *value)
494 case MFF_TUN_GBP_FLAGS:
495 CASE_MFF_TUN_METADATA:
498 case MFF_SKB_PRIORITY:
522 case MFF_ICMPV4_TYPE:
523 case MFF_ICMPV4_CODE:
524 case MFF_ICMPV6_TYPE:
525 case MFF_ICMPV6_CODE:
531 case MFF_IN_PORT_OXM:
532 case MFF_ACTSET_OUTPUT: {
534 return !ofputil_port_from_ofp11(value->be32, &port);
538 return !(value->u8 & ~IP_DSCP_MASK);
539 case MFF_IP_DSCP_SHIFTED:
540 return !(value->u8 & (~IP_DSCP_MASK >> 2));
542 return !(value->u8 & ~IP_ECN_MASK);
544 return !(value->u8 & ~FLOW_NW_FRAG_MASK);
546 return !(value->be16 & ~htons(0x0fff));
549 return !(value->be16 & htons(0xff00));
552 return !(value->be16 & htons(VLAN_CFI | VLAN_PCP_MASK));
554 return !(value->be16 & htons(VLAN_PCP_MASK));
556 case MFF_DL_VLAN_PCP:
558 return !(value->u8 & ~(VLAN_PCP_MASK >> VLAN_PCP_SHIFT));
561 return !(value->be32 & ~htonl(IPV6_LABEL_MASK));
564 return !(value->be32 & ~htonl(MPLS_LABEL_MASK >> MPLS_LABEL_SHIFT));
567 return !(value->u8 & ~(MPLS_TC_MASK >> MPLS_TC_SHIFT));
570 return !(value->u8 & ~(MPLS_BOS_MASK >> MPLS_BOS_SHIFT));
573 return !(value->be16 & ~htons(FLOW_TNL_PUB_F_MASK));
581 /* Copies the value of field 'mf' from 'flow' into 'value'. The caller is
582 * responsible for ensuring that 'flow' meets 'mf''s prerequisites. */
584 mf_get_value(const struct mf_field *mf, const struct flow *flow,
585 union mf_value *value)
589 value->be32 = htonl(flow->dp_hash);
592 value->be32 = htonl(flow->recirc_id);
595 value->be32 = htonl(flow->conj_id);
598 value->be64 = flow->tunnel.tun_id;
601 value->be32 = flow->tunnel.ip_src;
604 value->be32 = flow->tunnel.ip_dst;
607 value->be16 = htons(flow->tunnel.flags & FLOW_TNL_PUB_F_MASK);
610 value->be16 = flow->tunnel.gbp_id;
612 case MFF_TUN_GBP_FLAGS:
613 value->u8 = flow->tunnel.gbp_flags;
616 value->u8 = flow->tunnel.ip_ttl;
619 value->u8 = flow->tunnel.ip_tos;
621 CASE_MFF_TUN_METADATA:
622 tun_metadata_read(&flow->tunnel, mf, value);
626 value->be64 = flow->metadata;
630 value->be16 = htons(ofp_to_u16(flow->in_port.ofp_port));
632 case MFF_IN_PORT_OXM:
633 value->be32 = ofputil_port_to_ofp11(flow->in_port.ofp_port);
635 case MFF_ACTSET_OUTPUT:
636 value->be32 = ofputil_port_to_ofp11(flow->actset_output);
639 case MFF_SKB_PRIORITY:
640 value->be32 = htonl(flow->skb_priority);
644 value->be32 = htonl(flow->pkt_mark);
648 value->be32 = htonl(flow->regs[mf->id - MFF_REG0]);
652 value->be64 = htonll(flow_get_xreg(flow, mf->id - MFF_XREG0));
656 value->mac = flow->dl_src;
660 value->mac = flow->dl_dst;
664 value->be16 = flow->dl_type;
668 value->be16 = flow->vlan_tci;
672 value->be16 = flow->vlan_tci & htons(VLAN_VID_MASK);
675 value->be16 = flow->vlan_tci & htons(VLAN_VID_MASK | VLAN_CFI);
678 case MFF_DL_VLAN_PCP:
680 value->u8 = vlan_tci_to_pcp(flow->vlan_tci);
684 value->be32 = htonl(mpls_lse_to_label(flow->mpls_lse[0]));
688 value->u8 = mpls_lse_to_tc(flow->mpls_lse[0]);
692 value->u8 = mpls_lse_to_bos(flow->mpls_lse[0]);
696 value->be32 = flow->nw_src;
700 value->be32 = flow->nw_dst;
704 value->ipv6 = flow->ipv6_src;
708 value->ipv6 = flow->ipv6_dst;
712 value->be32 = flow->ipv6_label;
716 value->u8 = flow->nw_proto;
720 value->u8 = flow->nw_tos & IP_DSCP_MASK;
723 case MFF_IP_DSCP_SHIFTED:
724 value->u8 = flow->nw_tos >> 2;
728 value->u8 = flow->nw_tos & IP_ECN_MASK;
732 value->u8 = flow->nw_ttl;
736 value->u8 = flow->nw_frag;
740 value->be16 = htons(flow->nw_proto);
744 value->be32 = flow->nw_src;
748 value->be32 = flow->nw_dst;
753 value->mac = flow->arp_sha;
758 value->mac = flow->arp_tha;
764 value->be16 = flow->tp_src;
770 value->be16 = flow->tp_dst;
774 value->be16 = flow->tcp_flags;
777 case MFF_ICMPV4_TYPE:
778 case MFF_ICMPV6_TYPE:
779 value->u8 = ntohs(flow->tp_src);
782 case MFF_ICMPV4_CODE:
783 case MFF_ICMPV6_CODE:
784 value->u8 = ntohs(flow->tp_dst);
788 value->ipv6 = flow->nd_target;
797 /* Makes 'match' match field 'mf' exactly, with the value matched taken from
798 * 'value'. The caller is responsible for ensuring that 'match' meets 'mf''s
801 mf_set_value(const struct mf_field *mf,
802 const union mf_value *value, struct match *match)
806 match_set_dp_hash(match, ntohl(value->be32));
809 match_set_recirc_id(match, ntohl(value->be32));
812 match_set_conj_id(match, ntohl(value->be32));
815 match_set_tun_id(match, value->be64);
818 match_set_tun_src(match, value->be32);
821 match_set_tun_dst(match, value->be32);
824 match_set_tun_flags(match, ntohs(value->be16));
827 match_set_tun_gbp_id(match, value->be16);
829 case MFF_TUN_GBP_FLAGS:
830 match_set_tun_gbp_flags(match, value->u8);
833 match_set_tun_tos(match, value->u8);
836 match_set_tun_ttl(match, value->u8);
838 CASE_MFF_TUN_METADATA:
839 tun_metadata_set_match(mf, value, NULL, match);
843 match_set_metadata(match, value->be64);
847 match_set_in_port(match, u16_to_ofp(ntohs(value->be16)));
850 case MFF_IN_PORT_OXM: {
852 ofputil_port_from_ofp11(value->be32, &port);
853 match_set_in_port(match, port);
856 case MFF_ACTSET_OUTPUT: {
858 ofputil_port_from_ofp11(value->be32, &port);
859 match_set_actset_output(match, port);
863 case MFF_SKB_PRIORITY:
864 match_set_skb_priority(match, ntohl(value->be32));
868 match_set_pkt_mark(match, ntohl(value->be32));
872 match_set_reg(match, mf->id - MFF_REG0, ntohl(value->be32));
876 match_set_xreg(match, mf->id - MFF_XREG0, ntohll(value->be64));
880 match_set_dl_src(match, value->mac);
884 match_set_dl_dst(match, value->mac);
888 match_set_dl_type(match, value->be16);
892 match_set_dl_tci(match, value->be16);
896 match_set_dl_vlan(match, value->be16);
899 match_set_vlan_vid(match, value->be16);
902 case MFF_DL_VLAN_PCP:
904 match_set_dl_vlan_pcp(match, value->u8);
908 match_set_mpls_label(match, 0, value->be32);
912 match_set_mpls_tc(match, 0, value->u8);
916 match_set_mpls_bos(match, 0, value->u8);
920 match_set_nw_src(match, value->be32);
924 match_set_nw_dst(match, value->be32);
928 match_set_ipv6_src(match, &value->ipv6);
932 match_set_ipv6_dst(match, &value->ipv6);
936 match_set_ipv6_label(match, value->be32);
940 match_set_nw_proto(match, value->u8);
944 match_set_nw_dscp(match, value->u8);
947 case MFF_IP_DSCP_SHIFTED:
948 match_set_nw_dscp(match, value->u8 << 2);
952 match_set_nw_ecn(match, value->u8);
956 match_set_nw_ttl(match, value->u8);
960 match_set_nw_frag(match, value->u8);
964 match_set_nw_proto(match, ntohs(value->be16));
968 match_set_nw_src(match, value->be32);
972 match_set_nw_dst(match, value->be32);
977 match_set_arp_sha(match, value->mac);
982 match_set_arp_tha(match, value->mac);
988 match_set_tp_src(match, value->be16);
994 match_set_tp_dst(match, value->be16);
998 match_set_tcp_flags(match, value->be16);
1001 case MFF_ICMPV4_TYPE:
1002 case MFF_ICMPV6_TYPE:
1003 match_set_icmp_type(match, value->u8);
1006 case MFF_ICMPV4_CODE:
1007 case MFF_ICMPV6_CODE:
1008 match_set_icmp_code(match, value->u8);
1012 match_set_nd_target(match, &value->ipv6);
1021 /* Unwildcard 'mask' member field described by 'mf'. The caller is
1022 * responsible for ensuring that 'mask' meets 'mf''s prerequisites. */
1024 mf_mask_field(const struct mf_field *mf, struct flow *mask)
1026 /* For MFF_DL_VLAN, we cannot send a all 1's to flow_set_dl_vlan()
1027 * as that will be considered as OFP10_VLAN_NONE. So consider it as a
1028 * special case. For the rest, calling mf_set_flow_value() is good
1030 if (mf->id == MFF_DL_VLAN) {
1031 flow_set_dl_vlan(mask, htons(VLAN_VID_MASK));
1033 mf_set_flow_value(mf, &exact_match_mask, mask);
1038 field_len(const struct mf_field *mf, const union mf_value *value_)
1040 const uint8_t *value = &value_->u8;
1043 if (!mf->variable_len) {
1051 for (i = 0; i < mf->n_bytes; i++) {
1052 if (value[i] != 0) {
1057 return mf->n_bytes - i;
1060 /* Returns the effective length of the field. For fixed length fields,
1061 * this is just the defined length. For variable length fields, it is
1062 * the minimum size encoding that retains the same meaning (i.e.
1063 * discarding leading zeros).
1065 * 'is_masked' returns (if non-NULL) whether the original contained
1066 * a mask. Otherwise, a mask that is the same length as the value
1067 * might be misinterpreted as an exact match. */
1069 mf_field_len(const struct mf_field *mf, const union mf_value *value,
1070 const union mf_value *mask, bool *is_masked_)
1073 bool is_masked = mask && !is_all_ones(mask, mf->n_bytes);
1075 len = field_len(mf, value);
1077 mask_len = field_len(mf, mask);
1078 len = MAX(len, mask_len);
1082 *is_masked_ = is_masked;
1088 /* Sets 'flow' member field described by 'mf' to 'value'. The caller is
1089 * responsible for ensuring that 'flow' meets 'mf''s prerequisites.*/
1091 mf_set_flow_value(const struct mf_field *mf,
1092 const union mf_value *value, struct flow *flow)
1096 flow->dp_hash = ntohl(value->be32);
1099 flow->recirc_id = ntohl(value->be32);
1102 flow->conj_id = ntohl(value->be32);
1105 flow->tunnel.tun_id = value->be64;
1108 flow->tunnel.ip_src = value->be32;
1111 flow->tunnel.ip_dst = value->be32;
1114 flow->tunnel.flags = (flow->tunnel.flags & ~FLOW_TNL_PUB_F_MASK) |
1117 case MFF_TUN_GBP_ID:
1118 flow->tunnel.gbp_id = value->be16;
1120 case MFF_TUN_GBP_FLAGS:
1121 flow->tunnel.gbp_flags = value->u8;
1124 flow->tunnel.ip_tos = value->u8;
1127 flow->tunnel.ip_ttl = value->u8;
1129 CASE_MFF_TUN_METADATA:
1130 tun_metadata_write(&flow->tunnel, mf, value);
1133 flow->metadata = value->be64;
1137 flow->in_port.ofp_port = u16_to_ofp(ntohs(value->be16));
1140 case MFF_IN_PORT_OXM:
1141 ofputil_port_from_ofp11(value->be32, &flow->in_port.ofp_port);
1143 case MFF_ACTSET_OUTPUT:
1144 ofputil_port_from_ofp11(value->be32, &flow->actset_output);
1147 case MFF_SKB_PRIORITY:
1148 flow->skb_priority = ntohl(value->be32);
1152 flow->pkt_mark = ntohl(value->be32);
1156 flow->regs[mf->id - MFF_REG0] = ntohl(value->be32);
1160 flow_set_xreg(flow, mf->id - MFF_XREG0, ntohll(value->be64));
1164 flow->dl_src = value->mac;
1168 flow->dl_dst = value->mac;
1172 flow->dl_type = value->be16;
1176 flow->vlan_tci = value->be16;
1180 flow_set_dl_vlan(flow, value->be16);
1183 flow_set_vlan_vid(flow, value->be16);
1186 case MFF_DL_VLAN_PCP:
1188 flow_set_vlan_pcp(flow, value->u8);
1191 case MFF_MPLS_LABEL:
1192 flow_set_mpls_label(flow, 0, value->be32);
1196 flow_set_mpls_tc(flow, 0, value->u8);
1200 flow_set_mpls_bos(flow, 0, value->u8);
1204 flow->nw_src = value->be32;
1208 flow->nw_dst = value->be32;
1212 flow->ipv6_src = value->ipv6;
1216 flow->ipv6_dst = value->ipv6;
1219 case MFF_IPV6_LABEL:
1220 flow->ipv6_label = value->be32 & htonl(IPV6_LABEL_MASK);
1224 flow->nw_proto = value->u8;
1228 flow->nw_tos &= ~IP_DSCP_MASK;
1229 flow->nw_tos |= value->u8 & IP_DSCP_MASK;
1232 case MFF_IP_DSCP_SHIFTED:
1233 flow->nw_tos &= ~IP_DSCP_MASK;
1234 flow->nw_tos |= value->u8 << 2;
1238 flow->nw_tos &= ~IP_ECN_MASK;
1239 flow->nw_tos |= value->u8 & IP_ECN_MASK;
1243 flow->nw_ttl = value->u8;
1247 flow->nw_frag = value->u8 & FLOW_NW_FRAG_MASK;
1251 flow->nw_proto = ntohs(value->be16);
1255 flow->nw_src = value->be32;
1259 flow->nw_dst = value->be32;
1264 flow->arp_sha = value->mac;
1269 flow->arp_tha = value->mac;
1275 flow->tp_src = value->be16;
1281 flow->tp_dst = value->be16;
1285 flow->tcp_flags = value->be16;
1288 case MFF_ICMPV4_TYPE:
1289 case MFF_ICMPV6_TYPE:
1290 flow->tp_src = htons(value->u8);
1293 case MFF_ICMPV4_CODE:
1294 case MFF_ICMPV6_CODE:
1295 flow->tp_dst = htons(value->u8);
1299 flow->nd_target = value->ipv6;
1308 /* Consider each of 'src', 'mask', and 'dst' as if they were arrays of 8*n
1309 * bits. Then, for each 0 <= i < 8 * n such that mask[i] == 1, sets dst[i] =
1312 apply_mask(const uint8_t *src, const uint8_t *mask, uint8_t *dst, size_t n)
1316 for (i = 0; i < n; i++) {
1317 dst[i] = (src[i] & mask[i]) | (dst[i] & ~mask[i]);
1321 /* Sets 'flow' member field described by 'field' to 'value', except that bits
1322 * for which 'mask' has a 0-bit keep their existing values. The caller is
1323 * responsible for ensuring that 'flow' meets 'field''s prerequisites.*/
1325 mf_set_flow_value_masked(const struct mf_field *field,
1326 const union mf_value *value,
1327 const union mf_value *mask,
1332 mf_get_value(field, flow, &tmp);
1333 apply_mask((const uint8_t *) value, (const uint8_t *) mask,
1334 (uint8_t *) &tmp, field->n_bytes);
1335 mf_set_flow_value(field, &tmp, flow);
1339 mf_is_tun_metadata(const struct mf_field *mf)
1341 return mf->id >= MFF_TUN_METADATA0 &&
1342 mf->id < MFF_TUN_METADATA0 + TUN_METADATA_NUM_OPTS;
1345 /* Returns true if 'mf' has previously been set in 'flow', false if
1346 * it contains a non-default value.
1348 * The caller is responsible for ensuring that 'flow' meets 'mf''s
1351 mf_is_set(const struct mf_field *mf, const struct flow *flow)
1353 if (!mf_is_tun_metadata(mf)) {
1354 union mf_value value;
1356 mf_get_value(mf, flow, &value);
1357 return !is_all_zeros(&value, mf->n_bytes);
1359 return ULLONG_GET(flow->tunnel.metadata.present.map,
1360 mf->id - MFF_TUN_METADATA0);
1364 /* Makes 'match' wildcard field 'mf'.
1366 * The caller is responsible for ensuring that 'match' meets 'mf''s
1369 mf_set_wild(const struct mf_field *mf, struct match *match)
1373 match->flow.dp_hash = 0;
1374 match->wc.masks.dp_hash = 0;
1377 match->flow.recirc_id = 0;
1378 match->wc.masks.recirc_id = 0;
1381 match->flow.conj_id = 0;
1382 match->wc.masks.conj_id = 0;
1385 match_set_tun_id_masked(match, htonll(0), htonll(0));
1388 match_set_tun_src_masked(match, htonl(0), htonl(0));
1391 match_set_tun_dst_masked(match, htonl(0), htonl(0));
1394 match_set_tun_flags_masked(match, 0, 0);
1396 case MFF_TUN_GBP_ID:
1397 match_set_tun_gbp_id_masked(match, 0, 0);
1399 case MFF_TUN_GBP_FLAGS:
1400 match_set_tun_gbp_flags_masked(match, 0, 0);
1403 match_set_tun_tos_masked(match, 0, 0);
1406 match_set_tun_ttl_masked(match, 0, 0);
1408 CASE_MFF_TUN_METADATA:
1409 tun_metadata_set_match(mf, NULL, NULL, match);
1413 match_set_metadata_masked(match, htonll(0), htonll(0));
1417 case MFF_IN_PORT_OXM:
1418 match->flow.in_port.ofp_port = 0;
1419 match->wc.masks.in_port.ofp_port = 0;
1421 case MFF_ACTSET_OUTPUT:
1422 match->flow.actset_output = 0;
1423 match->wc.masks.actset_output = 0;
1426 case MFF_SKB_PRIORITY:
1427 match->flow.skb_priority = 0;
1428 match->wc.masks.skb_priority = 0;
1432 match->flow.pkt_mark = 0;
1433 match->wc.masks.pkt_mark = 0;
1437 match_set_reg_masked(match, mf->id - MFF_REG0, 0, 0);
1441 match_set_xreg_masked(match, mf->id - MFF_XREG0, 0, 0);
1445 match->flow.dl_src = eth_addr_zero;
1446 match->wc.masks.dl_src = eth_addr_zero;
1450 match->flow.dl_dst = eth_addr_zero;
1451 match->wc.masks.dl_dst = eth_addr_zero;
1455 match->flow.dl_type = htons(0);
1456 match->wc.masks.dl_type = htons(0);
1460 match_set_dl_tci_masked(match, htons(0), htons(0));
1465 match_set_any_vid(match);
1468 case MFF_DL_VLAN_PCP:
1470 match_set_any_pcp(match);
1473 case MFF_MPLS_LABEL:
1474 match_set_any_mpls_label(match, 0);
1478 match_set_any_mpls_tc(match, 0);
1482 match_set_any_mpls_bos(match, 0);
1487 match_set_nw_src_masked(match, htonl(0), htonl(0));
1492 match_set_nw_dst_masked(match, htonl(0), htonl(0));
1496 memset(&match->wc.masks.ipv6_src, 0, sizeof match->wc.masks.ipv6_src);
1497 memset(&match->flow.ipv6_src, 0, sizeof match->flow.ipv6_src);
1501 memset(&match->wc.masks.ipv6_dst, 0, sizeof match->wc.masks.ipv6_dst);
1502 memset(&match->flow.ipv6_dst, 0, sizeof match->flow.ipv6_dst);
1505 case MFF_IPV6_LABEL:
1506 match->wc.masks.ipv6_label = htonl(0);
1507 match->flow.ipv6_label = htonl(0);
1511 match->wc.masks.nw_proto = 0;
1512 match->flow.nw_proto = 0;
1516 case MFF_IP_DSCP_SHIFTED:
1517 match->wc.masks.nw_tos &= ~IP_DSCP_MASK;
1518 match->flow.nw_tos &= ~IP_DSCP_MASK;
1522 match->wc.masks.nw_tos &= ~IP_ECN_MASK;
1523 match->flow.nw_tos &= ~IP_ECN_MASK;
1527 match->wc.masks.nw_ttl = 0;
1528 match->flow.nw_ttl = 0;
1532 match->wc.masks.nw_frag &= ~FLOW_NW_FRAG_MASK;
1533 match->flow.nw_frag &= ~FLOW_NW_FRAG_MASK;
1537 match->wc.masks.nw_proto = 0;
1538 match->flow.nw_proto = 0;
1543 match->flow.arp_sha = eth_addr_zero;
1544 match->wc.masks.arp_sha = eth_addr_zero;
1549 match->flow.arp_tha = eth_addr_zero;
1550 match->wc.masks.arp_tha = eth_addr_zero;
1556 case MFF_ICMPV4_TYPE:
1557 case MFF_ICMPV6_TYPE:
1558 match->wc.masks.tp_src = htons(0);
1559 match->flow.tp_src = htons(0);
1565 case MFF_ICMPV4_CODE:
1566 case MFF_ICMPV6_CODE:
1567 match->wc.masks.tp_dst = htons(0);
1568 match->flow.tp_dst = htons(0);
1572 match->wc.masks.tcp_flags = htons(0);
1573 match->flow.tcp_flags = htons(0);
1577 memset(&match->wc.masks.nd_target, 0,
1578 sizeof match->wc.masks.nd_target);
1579 memset(&match->flow.nd_target, 0, sizeof match->flow.nd_target);
1588 /* Makes 'match' match field 'mf' with the specified 'value' and 'mask'.
1589 * 'value' specifies a value to match and 'mask' specifies a wildcard pattern,
1590 * with a 1-bit indicating that the corresponding value bit must match and a
1591 * 0-bit indicating a don't-care.
1593 * If 'mask' is NULL or points to all-1-bits, then this call is equivalent to
1594 * mf_set_value(mf, value, match). If 'mask' points to all-0-bits, then this
1595 * call is equivalent to mf_set_wild(mf, match).
1597 * 'mask' must be a valid mask for 'mf' (see mf_is_mask_valid()). The caller
1598 * is responsible for ensuring that 'match' meets 'mf''s prerequisites. */
1599 enum ofputil_protocol
1600 mf_set(const struct mf_field *mf,
1601 const union mf_value *value, const union mf_value *mask,
1602 struct match *match)
1604 if (!mask || is_all_ones(mask, mf->n_bytes)) {
1605 mf_set_value(mf, value, match);
1606 return mf->usable_protocols_exact;
1607 } else if (is_all_zeros(mask, mf->n_bytes) && !mf_is_tun_metadata(mf)) {
1608 /* Tunnel metadata matches on the existence of the field itself, so
1609 * it still needs to be encoded even if the value is wildcarded. */
1610 mf_set_wild(mf, match);
1611 return OFPUTIL_P_ANY;
1618 case MFF_IN_PORT_OXM:
1619 case MFF_ACTSET_OUTPUT:
1620 case MFF_SKB_PRIORITY:
1623 case MFF_DL_VLAN_PCP:
1625 case MFF_MPLS_LABEL:
1631 case MFF_IP_DSCP_SHIFTED:
1634 case MFF_ICMPV4_TYPE:
1635 case MFF_ICMPV4_CODE:
1636 case MFF_ICMPV6_TYPE:
1637 case MFF_ICMPV6_CODE:
1638 return OFPUTIL_P_NONE;
1641 match_set_dp_hash_masked(match, ntohl(value->be32), ntohl(mask->be32));
1644 match_set_tun_id_masked(match, value->be64, mask->be64);
1647 match_set_tun_src_masked(match, value->be32, mask->be32);
1650 match_set_tun_dst_masked(match, value->be32, mask->be32);
1653 match_set_tun_flags_masked(match, ntohs(value->be16), ntohs(mask->be16));
1655 case MFF_TUN_GBP_ID:
1656 match_set_tun_gbp_id_masked(match, value->be16, mask->be16);
1658 case MFF_TUN_GBP_FLAGS:
1659 match_set_tun_gbp_flags_masked(match, value->u8, mask->u8);
1662 match_set_tun_ttl_masked(match, value->u8, mask->u8);
1665 match_set_tun_tos_masked(match, value->u8, mask->u8);
1667 CASE_MFF_TUN_METADATA:
1668 tun_metadata_set_match(mf, value, mask, match);
1672 match_set_metadata_masked(match, value->be64, mask->be64);
1676 match_set_reg_masked(match, mf->id - MFF_REG0,
1677 ntohl(value->be32), ntohl(mask->be32));
1681 match_set_xreg_masked(match, mf->id - MFF_XREG0,
1682 ntohll(value->be64), ntohll(mask->be64));
1686 match_set_pkt_mark_masked(match, ntohl(value->be32),
1691 match_set_dl_dst_masked(match, value->mac, mask->mac);
1695 match_set_dl_src_masked(match, value->mac, mask->mac);
1700 match_set_arp_sha_masked(match, value->mac, mask->mac);
1705 match_set_arp_tha_masked(match, value->mac, mask->mac);
1709 match_set_dl_tci_masked(match, value->be16, mask->be16);
1713 match_set_vlan_vid_masked(match, value->be16, mask->be16);
1717 match_set_nw_src_masked(match, value->be32, mask->be32);
1721 match_set_nw_dst_masked(match, value->be32, mask->be32);
1725 match_set_ipv6_src_masked(match, &value->ipv6, &mask->ipv6);
1729 match_set_ipv6_dst_masked(match, &value->ipv6, &mask->ipv6);
1732 case MFF_IPV6_LABEL:
1733 if ((mask->be32 & htonl(IPV6_LABEL_MASK)) == htonl(IPV6_LABEL_MASK)) {
1734 mf_set_value(mf, value, match);
1736 match_set_ipv6_label_masked(match, value->be32, mask->be32);
1741 match_set_nd_target_masked(match, &value->ipv6, &mask->ipv6);
1745 match_set_nw_frag_masked(match, value->u8, mask->u8);
1749 match_set_nw_src_masked(match, value->be32, mask->be32);
1753 match_set_nw_dst_masked(match, value->be32, mask->be32);
1759 match_set_tp_src_masked(match, value->be16, mask->be16);
1765 match_set_tp_dst_masked(match, value->be16, mask->be16);
1769 match_set_tcp_flags_masked(match, value->be16, mask->be16);
1777 return ((mf->usable_protocols_bitwise == mf->usable_protocols_cidr
1778 || ip_is_cidr(mask->be32))
1779 ? mf->usable_protocols_cidr
1780 : mf->usable_protocols_bitwise);
1784 mf_check__(const struct mf_subfield *sf, const struct flow *flow,
1788 VLOG_WARN_RL(&rl, "unknown %s field", type);
1789 return OFPERR_OFPBAC_BAD_SET_TYPE;
1790 } else if (!sf->n_bits) {
1791 VLOG_WARN_RL(&rl, "zero bit %s field %s", type, sf->field->name);
1792 return OFPERR_OFPBAC_BAD_SET_LEN;
1793 } else if (sf->ofs >= sf->field->n_bits) {
1794 VLOG_WARN_RL(&rl, "bit offset %d exceeds %d-bit width of %s field %s",
1795 sf->ofs, sf->field->n_bits, type, sf->field->name);
1796 return OFPERR_OFPBAC_BAD_SET_LEN;
1797 } else if (sf->ofs + sf->n_bits > sf->field->n_bits) {
1798 VLOG_WARN_RL(&rl, "bit offset %d and width %d exceeds %d-bit width "
1799 "of %s field %s", sf->ofs, sf->n_bits,
1800 sf->field->n_bits, type, sf->field->name);
1801 return OFPERR_OFPBAC_BAD_SET_LEN;
1802 } else if (flow && !mf_are_prereqs_ok(sf->field, flow)) {
1803 VLOG_WARN_RL(&rl, "%s field %s lacks correct prerequisites",
1804 type, sf->field->name);
1805 return OFPERR_OFPBAC_MATCH_INCONSISTENT;
1811 /* Checks whether 'sf' is valid for reading a subfield out of 'flow'. Returns
1812 * 0 if so, otherwise an OpenFlow error code (e.g. as returned by
1815 mf_check_src(const struct mf_subfield *sf, const struct flow *flow)
1817 return mf_check__(sf, flow, "source");
1820 /* Checks whether 'sf' is valid for writing a subfield into 'flow'. Returns 0
1821 * if so, otherwise an OpenFlow error code (e.g. as returned by
1824 mf_check_dst(const struct mf_subfield *sf, const struct flow *flow)
1826 int error = mf_check__(sf, flow, "destination");
1827 if (!error && !sf->field->writable) {
1828 VLOG_WARN_RL(&rl, "destination field %s is not writable",
1830 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
1835 /* Copies the value and wildcard bit pattern for 'mf' from 'match' into the
1836 * 'value' and 'mask', respectively. */
1838 mf_get(const struct mf_field *mf, const struct match *match,
1839 union mf_value *value, union mf_value *mask)
1841 mf_get_value(mf, &match->flow, value);
1842 mf_get_mask(mf, &match->wc, mask);
1846 mf_from_integer_string(const struct mf_field *mf, const char *s,
1847 uint8_t *valuep, uint8_t *maskp)
1850 const char *err_str = "";
1853 err = parse_int_string(s, valuep, mf->n_bytes, &tail);
1854 if (err || (*tail != '\0' && *tail != '/')) {
1860 err = parse_int_string(tail + 1, maskp, mf->n_bytes, &tail);
1861 if (err || *tail != '\0') {
1866 memset(maskp, 0xff, mf->n_bytes);
1872 if (err == ERANGE) {
1873 return xasprintf("%s: %s too large for %u-byte field %s",
1874 s, err_str, mf->n_bytes, mf->name);
1876 return xasprintf("%s: bad syntax for %s %s", s, mf->name, err_str);
1881 mf_from_ethernet_string(const struct mf_field *mf, const char *s,
1882 struct eth_addr *mac, struct eth_addr *mask)
1886 ovs_assert(mf->n_bytes == ETH_ADDR_LEN);
1889 if (ovs_scan(s, ETH_ADDR_SCAN_FMT"%n", ETH_ADDR_SCAN_ARGS(*mac), &n)
1890 && n == strlen(s)) {
1891 *mask = eth_addr_exact;
1896 if (ovs_scan(s, ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT"%n",
1897 ETH_ADDR_SCAN_ARGS(*mac), ETH_ADDR_SCAN_ARGS(*mask), &n)
1898 && n == strlen(s)) {
1902 return xasprintf("%s: invalid Ethernet address", s);
1906 mf_from_ipv4_string(const struct mf_field *mf, const char *s,
1907 ovs_be32 *ip, ovs_be32 *mask)
1911 ovs_assert(mf->n_bytes == sizeof *ip);
1913 if (ovs_scan(s, IP_SCAN_FMT"/"IP_SCAN_FMT,
1914 IP_SCAN_ARGS(ip), IP_SCAN_ARGS(mask))) {
1916 } else if (ovs_scan(s, IP_SCAN_FMT"/%d", IP_SCAN_ARGS(ip), &prefix)) {
1917 if (prefix <= 0 || prefix > 32) {
1918 return xasprintf("%s: network prefix bits not between 0 and "
1921 *mask = be32_prefix_mask(prefix);
1922 } else if (ovs_scan(s, IP_SCAN_FMT, IP_SCAN_ARGS(ip))) {
1923 *mask = OVS_BE32_MAX;
1925 return xasprintf("%s: invalid IP address", s);
1931 mf_from_ipv6_string(const struct mf_field *mf, const char *s,
1932 struct in6_addr *value, struct in6_addr *mask)
1934 char *str = xstrdup(s);
1935 char *save_ptr = NULL;
1936 const char *name, *netmask;
1939 ovs_assert(mf->n_bytes == sizeof *value);
1941 name = strtok_r(str, "/", &save_ptr);
1942 retval = name ? lookup_ipv6(name, value) : EINVAL;
1946 err = xasprintf("%s: could not convert to IPv6 address", str);
1952 netmask = strtok_r(NULL, "/", &save_ptr);
1954 if (inet_pton(AF_INET6, netmask, mask) != 1) {
1955 int prefix = atoi(netmask);
1956 if (prefix <= 0 || prefix > 128) {
1958 return xasprintf("%s: prefix bits not between 1 and 128", s);
1960 *mask = ipv6_create_mask(prefix);
1964 *mask = in6addr_exact;
1972 mf_from_ofp_port_string(const struct mf_field *mf, const char *s,
1973 ovs_be16 *valuep, ovs_be16 *maskp)
1977 ovs_assert(mf->n_bytes == sizeof(ovs_be16));
1979 if (ofputil_port_from_string(s, &port)) {
1980 *valuep = htons(ofp_to_u16(port));
1981 *maskp = OVS_BE16_MAX;
1984 return xasprintf("%s: port value out of range for %s", s, mf->name);
1988 mf_from_ofp_port_string32(const struct mf_field *mf, const char *s,
1989 ovs_be32 *valuep, ovs_be32 *maskp)
1993 ovs_assert(mf->n_bytes == sizeof(ovs_be32));
1994 if (ofputil_port_from_string(s, &port)) {
1995 *valuep = ofputil_port_to_ofp11(port);
1996 *maskp = OVS_BE32_MAX;
1999 return xasprintf("%s: port value out of range for %s", s, mf->name);
2002 struct frag_handling {
2008 static const struct frag_handling all_frags[] = {
2009 #define A FLOW_NW_FRAG_ANY
2010 #define L FLOW_NW_FRAG_LATER
2011 /* name mask value */
2014 { "first", A|L, A },
2015 { "later", A|L, A|L },
2020 { "not_later", L, 0 },
2027 mf_from_frag_string(const char *s, uint8_t *valuep, uint8_t *maskp)
2029 const struct frag_handling *h;
2031 for (h = all_frags; h < &all_frags[ARRAY_SIZE(all_frags)]; h++) {
2032 if (!strcasecmp(s, h->name)) {
2033 /* We force the upper bits of the mask on to make mf_parse_value()
2034 * happy (otherwise it will never think it's an exact match.) */
2035 *maskp = h->mask | ~FLOW_NW_FRAG_MASK;
2041 return xasprintf("%s: unknown fragment type (valid types are \"no\", "
2042 "\"yes\", \"first\", \"later\", \"not_first\"", s);
2046 parse_mf_flags(const char *s, const char *(*bit_to_string)(uint32_t),
2047 const char *field_name, ovs_be16 *flagsp, ovs_be16 allowed,
2052 uint32_t flags, mask;
2054 err = parse_flags(s, bit_to_string, '\0', field_name, &err_str,
2055 &flags, ntohs(allowed), maskp ? &mask : NULL);
2060 *flagsp = htons(flags);
2062 *maskp = htons(mask);
2069 mf_from_tcp_flags_string(const char *s, ovs_be16 *flagsp, ovs_be16 *maskp)
2071 return parse_mf_flags(s, packet_tcp_flag_to_string, "TCP", flagsp,
2072 TCP_FLAGS_BE16(OVS_BE16_MAX), maskp);
2076 mf_from_tun_flags_string(const char *s, ovs_be16 *flagsp, ovs_be16 *maskp)
2078 return parse_mf_flags(s, flow_tun_flag_to_string, "tunnel", flagsp,
2079 htons(FLOW_TNL_PUB_F_MASK), maskp);
2082 /* Parses 's', a string value for field 'mf', into 'value' and 'mask'. Returns
2083 * NULL if successful, otherwise a malloc()'d string describing the error. */
2085 mf_parse(const struct mf_field *mf, const char *s,
2086 union mf_value *value, union mf_value *mask)
2090 if (!strcmp(s, "*")) {
2091 memset(value, 0, mf->n_bytes);
2092 memset(mask, 0, mf->n_bytes);
2096 switch (mf->string) {
2098 case MFS_HEXADECIMAL:
2099 error = mf_from_integer_string(mf, s,
2100 (uint8_t *) value, (uint8_t *) mask);
2104 error = mf_from_ethernet_string(mf, s, &value->mac, &mask->mac);
2108 error = mf_from_ipv4_string(mf, s, &value->be32, &mask->be32);
2112 error = mf_from_ipv6_string(mf, s, &value->ipv6, &mask->ipv6);
2116 error = mf_from_ofp_port_string(mf, s, &value->be16, &mask->be16);
2119 case MFS_OFP_PORT_OXM:
2120 error = mf_from_ofp_port_string32(mf, s, &value->be32, &mask->be32);
2124 error = mf_from_frag_string(s, &value->u8, &mask->u8);
2128 ovs_assert(mf->n_bytes == sizeof(ovs_be16));
2129 error = mf_from_tun_flags_string(s, &value->be16, &mask->be16);
2133 ovs_assert(mf->n_bytes == sizeof(ovs_be16));
2134 error = mf_from_tcp_flags_string(s, &value->be16, &mask->be16);
2141 if (!error && !mf_is_mask_valid(mf, mask)) {
2142 error = xasprintf("%s: invalid mask for field %s", s, mf->name);
2147 /* Parses 's', a string value for field 'mf', into 'value'. Returns NULL if
2148 * successful, otherwise a malloc()'d string describing the error. */
2150 mf_parse_value(const struct mf_field *mf, const char *s, union mf_value *value)
2152 union mf_value mask;
2155 error = mf_parse(mf, s, value, &mask);
2160 if (!is_all_ones((const uint8_t *) &mask, mf->n_bytes)) {
2161 return xasprintf("%s: wildcards not allowed here", s);
2167 mf_format_integer_string(const struct mf_field *mf, const uint8_t *valuep,
2168 const uint8_t *maskp, struct ds *s)
2170 if (mf->string == MFS_HEXADECIMAL) {
2171 ds_put_hex(s, valuep, mf->n_bytes);
2173 unsigned long long int integer = 0;
2176 ovs_assert(mf->n_bytes <= 8);
2177 for (i = 0; i < mf->n_bytes; i++) {
2178 integer = (integer << 8) | valuep[i];
2180 ds_put_format(s, "%lld", integer);
2184 /* I guess we could write the mask in decimal for MFS_DECIMAL but I'm
2185 * not sure that that a bit-mask written in decimal is ever easier to
2186 * understand than the same bit-mask written in hexadecimal. */
2187 ds_put_char(s, '/');
2188 ds_put_hex(s, maskp, mf->n_bytes);
2193 mf_format_frag_string(uint8_t value, uint8_t mask, struct ds *s)
2195 const struct frag_handling *h;
2197 mask &= FLOW_NW_FRAG_MASK;
2200 for (h = all_frags; h < &all_frags[ARRAY_SIZE(all_frags)]; h++) {
2201 if (value == h->value && mask == h->mask) {
2202 ds_put_cstr(s, h->name);
2206 ds_put_cstr(s, "<error>");
2210 mf_format_tnl_flags_string(ovs_be16 value, ovs_be16 mask, struct ds *s)
2212 format_flags_masked(s, NULL, flow_tun_flag_to_string, ntohs(value),
2213 ntohs(mask) & FLOW_TNL_PUB_F_MASK, FLOW_TNL_PUB_F_MASK);
2217 mf_format_tcp_flags_string(ovs_be16 value, ovs_be16 mask, struct ds *s)
2219 format_flags_masked(s, NULL, packet_tcp_flag_to_string, ntohs(value),
2220 TCP_FLAGS(mask), TCP_FLAGS(OVS_BE16_MAX));
2223 /* Appends to 's' a string representation of field 'mf' whose value is in
2224 * 'value' and 'mask'. 'mask' may be NULL to indicate an exact match. */
2226 mf_format(const struct mf_field *mf,
2227 const union mf_value *value, const union mf_value *mask,
2231 if (is_all_zeros(mask, mf->n_bytes)) {
2232 ds_put_cstr(s, "ANY");
2234 } else if (is_all_ones(mask, mf->n_bytes)) {
2239 switch (mf->string) {
2240 case MFS_OFP_PORT_OXM:
2243 ofputil_port_from_ofp11(value->be32, &port);
2244 ofputil_format_port(port, s);
2250 ofputil_format_port(u16_to_ofp(ntohs(value->be16)), s);
2255 case MFS_HEXADECIMAL:
2256 mf_format_integer_string(mf, (uint8_t *) value, (uint8_t *) mask, s);
2260 eth_format_masked(value->mac, mask ? &mask->mac : NULL, s);
2264 ip_format_masked(value->be32, mask ? mask->be32 : OVS_BE32_MAX, s);
2268 print_ipv6_masked(s, &value->ipv6, mask ? &mask->ipv6 : NULL);
2272 mf_format_frag_string(value->u8, mask ? mask->u8 : UINT8_MAX, s);
2276 mf_format_tnl_flags_string(value->be16,
2277 mask ? mask->be16 : OVS_BE16_MAX, s);
2281 mf_format_tcp_flags_string(value->be16,
2282 mask ? mask->be16 : OVS_BE16_MAX, s);
2290 /* Makes subfield 'sf' within 'flow' exactly match the 'sf->n_bits'
2291 * least-significant bits in 'x'.
2294 mf_write_subfield_flow(const struct mf_subfield *sf,
2295 const union mf_subvalue *x, struct flow *flow)
2297 const struct mf_field *field = sf->field;
2298 union mf_value value;
2300 mf_get_value(field, flow, &value);
2301 bitwise_copy(x, sizeof *x, 0, &value, field->n_bytes,
2302 sf->ofs, sf->n_bits);
2303 mf_set_flow_value(field, &value, flow);
2306 /* Makes subfield 'sf' within 'match' exactly match the 'sf->n_bits'
2307 * least-significant bits in 'x'.
2310 mf_write_subfield(const struct mf_subfield *sf, const union mf_subvalue *x,
2311 struct match *match)
2313 const struct mf_field *field = sf->field;
2314 union mf_value value, mask;
2316 mf_get(field, match, &value, &mask);
2317 bitwise_copy(x, sizeof *x, 0, &value, field->n_bytes, sf->ofs, sf->n_bits);
2318 bitwise_one ( &mask, field->n_bytes, sf->ofs, sf->n_bits);
2319 mf_set(field, &value, &mask, match);
2322 /* 'v' and 'm' correspond to values of 'field'. This function copies them into
2323 * 'match' in the correspond positions. */
2325 mf_mask_subfield(const struct mf_field *field,
2326 const union mf_subvalue *v,
2327 const union mf_subvalue *m,
2328 struct match *match)
2330 union mf_value value, mask;
2332 mf_get(field, match, &value, &mask);
2333 bitwise_copy(v, sizeof *v, 0, &value, field->n_bytes, 0, field->n_bits);
2334 bitwise_copy(m, sizeof *m, 0, &mask, field->n_bytes, 0, field->n_bits);
2335 mf_set(field, &value, &mask, match);
2338 /* Initializes 'x' to the value of 'sf' within 'flow'. 'sf' must be valid for
2339 * reading 'flow', e.g. as checked by mf_check_src(). */
2341 mf_read_subfield(const struct mf_subfield *sf, const struct flow *flow,
2342 union mf_subvalue *x)
2344 union mf_value value;
2346 mf_get_value(sf->field, flow, &value);
2348 memset(x, 0, sizeof *x);
2349 bitwise_copy(&value, sf->field->n_bytes, sf->ofs,
2354 /* Returns the value of 'sf' within 'flow'. 'sf' must be valid for reading
2355 * 'flow', e.g. as checked by mf_check_src() and sf->n_bits must be 64 or
2358 mf_get_subfield(const struct mf_subfield *sf, const struct flow *flow)
2360 union mf_value value;
2362 mf_get_value(sf->field, flow, &value);
2363 return bitwise_get(&value, sf->field->n_bytes, sf->ofs, sf->n_bits);
2367 mf_format_subvalue(const union mf_subvalue *subvalue, struct ds *s)
2369 ds_put_hex(s, subvalue->u8, sizeof subvalue->u8);
2373 field_array_set(enum mf_field_id id, const union mf_value *value,
2374 struct field_array *fa)
2376 ovs_assert(id < MFF_N_IDS);
2377 bitmap_set1(fa->used.bm, id);
2378 fa->value[id] = *value;