2 * Copyright (c) 2011, 2012, 2013, 2014, 2015, 2016 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 /* Appends a formatted representation of 'sv' to 's'. */
176 mf_subvalue_format(const union mf_subvalue *sv, struct ds *s)
178 ds_put_hex(s, sv, sizeof *sv);
181 /* Returns true if 'wc' wildcards all the bits in field 'mf', false if 'wc'
182 * specifies at least one bit in the field.
184 * The caller is responsible for ensuring that 'wc' corresponds to a flow that
185 * meets 'mf''s prerequisites. */
187 mf_is_all_wild(const struct mf_field *mf, const struct flow_wildcards *wc)
191 return !wc->masks.dp_hash;
193 return !wc->masks.recirc_id;
195 return !wc->masks.conj_id;
197 return !wc->masks.tunnel.ip_src;
199 return !wc->masks.tunnel.ip_dst;
200 case MFF_TUN_IPV6_SRC:
201 return ipv6_mask_is_any(&wc->masks.tunnel.ipv6_src);
202 case MFF_TUN_IPV6_DST:
203 return ipv6_mask_is_any(&wc->masks.tunnel.ipv6_dst);
205 return !wc->masks.tunnel.tun_id;
207 return !wc->masks.tunnel.ip_tos;
209 return !wc->masks.tunnel.ip_ttl;
211 return !(wc->masks.tunnel.flags & FLOW_TNL_PUB_F_MASK);
213 return !wc->masks.tunnel.gbp_id;
214 case MFF_TUN_GBP_FLAGS:
215 return !wc->masks.tunnel.gbp_flags;
216 CASE_MFF_TUN_METADATA:
217 return !ULLONG_GET(wc->masks.tunnel.metadata.present.map,
218 mf->id - MFF_TUN_METADATA0);
220 return !wc->masks.metadata;
222 case MFF_IN_PORT_OXM:
223 return !wc->masks.in_port.ofp_port;
224 case MFF_SKB_PRIORITY:
225 return !wc->masks.skb_priority;
227 return !wc->masks.pkt_mark;
229 return !wc->masks.ct_state;
231 return !wc->masks.ct_zone;
233 return !wc->masks.ct_mark;
235 return ovs_u128_is_zero(&wc->masks.ct_label);
237 return !wc->masks.regs[mf->id - MFF_REG0];
239 return !flow_get_xreg(&wc->masks, mf->id - MFF_XREG0);
240 case MFF_ACTSET_OUTPUT:
241 return !wc->masks.actset_output;
244 return eth_addr_is_zero(wc->masks.dl_src);
246 return eth_addr_is_zero(wc->masks.dl_dst);
248 return !wc->masks.dl_type;
252 return eth_addr_is_zero(wc->masks.arp_sha);
256 return eth_addr_is_zero(wc->masks.arp_tha);
259 return !wc->masks.vlan_tci;
261 return !(wc->masks.vlan_tci & htons(VLAN_VID_MASK));
263 return !(wc->masks.vlan_tci & htons(VLAN_VID_MASK | VLAN_CFI));
264 case MFF_DL_VLAN_PCP:
266 return !(wc->masks.vlan_tci & htons(VLAN_PCP_MASK));
269 return !(wc->masks.mpls_lse[0] & htonl(MPLS_LABEL_MASK));
271 return !(wc->masks.mpls_lse[0] & htonl(MPLS_TC_MASK));
273 return !(wc->masks.mpls_lse[0] & htonl(MPLS_BOS_MASK));
276 return !wc->masks.nw_src;
278 return !wc->masks.nw_dst;
281 return ipv6_mask_is_any(&wc->masks.ipv6_src);
283 return ipv6_mask_is_any(&wc->masks.ipv6_dst);
286 return !wc->masks.ipv6_label;
289 return !wc->masks.nw_proto;
291 case MFF_IP_DSCP_SHIFTED:
292 return !(wc->masks.nw_tos & IP_DSCP_MASK);
294 return !(wc->masks.nw_tos & IP_ECN_MASK);
296 return !wc->masks.nw_ttl;
299 return ipv6_mask_is_any(&wc->masks.nd_target);
302 return !(wc->masks.nw_frag & FLOW_NW_FRAG_MASK);
305 return !wc->masks.nw_proto;
307 return !wc->masks.nw_src;
309 return !wc->masks.nw_dst;
314 case MFF_ICMPV4_TYPE:
315 case MFF_ICMPV6_TYPE:
316 return !wc->masks.tp_src;
320 case MFF_ICMPV4_CODE:
321 case MFF_ICMPV6_CODE:
322 return !wc->masks.tp_dst;
324 return !wc->masks.tcp_flags;
332 /* Initializes 'mask' with the wildcard bit pattern for field 'mf' within 'wc'.
333 * Each bit in 'mask' will be set to 1 if the bit is significant for matching
334 * purposes, or to 0 if it is wildcarded.
336 * The caller is responsible for ensuring that 'wc' corresponds to a flow that
337 * meets 'mf''s prerequisites. */
339 mf_get_mask(const struct mf_field *mf, const struct flow_wildcards *wc,
340 union mf_value *mask)
342 mf_get_value(mf, &wc->masks, mask);
345 /* Tests whether 'mask' is a valid wildcard bit pattern for 'mf'. Returns true
346 * if the mask is valid, false otherwise. */
348 mf_is_mask_valid(const struct mf_field *mf, const union mf_value *mask)
350 switch (mf->maskable) {
352 return (is_all_zeros(mask, mf->n_bytes) ||
353 is_all_ones(mask, mf->n_bytes));
362 /* Returns true if 'flow' meets the prerequisites for 'mf', false otherwise. */
364 mf_are_prereqs_ok(const struct mf_field *mf, const struct flow *flow)
366 switch (mf->prereqs) {
371 return (flow->dl_type == htons(ETH_TYPE_ARP) ||
372 flow->dl_type == htons(ETH_TYPE_RARP));
374 return flow->dl_type == htons(ETH_TYPE_IP);
376 return flow->dl_type == htons(ETH_TYPE_IPV6);
378 return (flow->vlan_tci & htons(VLAN_CFI)) != 0;
380 return eth_type_mpls(flow->dl_type);
382 return is_ip_any(flow);
385 return is_ip_any(flow) && flow->nw_proto == IPPROTO_TCP
386 && !(flow->nw_frag & FLOW_NW_FRAG_LATER);
388 return is_ip_any(flow) && flow->nw_proto == IPPROTO_UDP
389 && !(flow->nw_frag & FLOW_NW_FRAG_LATER);
391 return is_ip_any(flow) && flow->nw_proto == IPPROTO_SCTP
392 && !(flow->nw_frag & FLOW_NW_FRAG_LATER);
394 return is_icmpv4(flow);
396 return is_icmpv6(flow);
399 return (is_icmpv6(flow)
400 && flow->tp_dst == htons(0)
401 && (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT) ||
402 flow->tp_src == htons(ND_NEIGHBOR_ADVERT)));
404 return (is_icmpv6(flow)
405 && flow->tp_dst == htons(0)
406 && (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT)));
408 return (is_icmpv6(flow)
409 && flow->tp_dst == htons(0)
410 && (flow->tp_src == htons(ND_NEIGHBOR_ADVERT)));
416 /* Set field and it's prerequisities in the mask.
417 * This is only ever called for writeable 'mf's, but we do not make the
418 * distinction here. */
420 mf_mask_field_and_prereqs(const struct mf_field *mf, struct flow_wildcards *wc)
422 mf_set_flow_value(mf, &exact_match_mask, &wc->masks);
424 switch (mf->prereqs) {
428 WC_MASK_FIELD(wc, tp_src);
429 WC_MASK_FIELD(wc, tp_dst);
436 /* nw_frag always unwildcarded. */
437 WC_MASK_FIELD(wc, nw_proto);
444 /* dl_type always unwildcarded. */
447 WC_MASK_FIELD_MASK(wc, vlan_tci, htons(VLAN_CFI));
454 /* Set bits of 'bm' corresponding to the field 'mf' and it's prerequisities. */
456 mf_bitmap_set_field_and_prereqs(const struct mf_field *mf, struct mf_bitmap *bm)
458 bitmap_set1(bm->bm, mf->id);
460 switch (mf->prereqs) {
464 bitmap_set1(bm->bm, MFF_TCP_SRC);
465 bitmap_set1(bm->bm, MFF_TCP_DST);
472 /* nw_frag always unwildcarded. */
473 bitmap_set1(bm->bm, MFF_IP_PROTO);
480 bitmap_set1(bm->bm, MFF_ETH_TYPE);
483 bitmap_set1(bm->bm, MFF_VLAN_TCI);
490 /* Returns true if 'value' may be a valid value *as part of a masked match*,
493 * A value is not rejected just because it is not valid for the field in
494 * question, but only if it doesn't make sense to test the bits in question at
495 * all. For example, the MFF_VLAN_TCI field will never have a nonzero value
496 * without the VLAN_CFI bit being set, but we can't reject those values because
497 * it is still legitimate to test just for those bits (see the documentation
498 * for NXM_OF_VLAN_TCI in nicira-ext.h). On the other hand, there is never a
499 * reason to set the low bit of MFF_IP_DSCP to 1, so we reject that. */
501 mf_is_value_valid(const struct mf_field *mf, const union mf_value *value)
510 case MFF_TUN_IPV6_SRC:
511 case MFF_TUN_IPV6_DST:
515 case MFF_TUN_GBP_FLAGS:
516 CASE_MFF_TUN_METADATA:
519 case MFF_SKB_PRIORITY:
546 case MFF_ICMPV4_TYPE:
547 case MFF_ICMPV4_CODE:
548 case MFF_ICMPV6_TYPE:
549 case MFF_ICMPV6_CODE:
555 case MFF_IN_PORT_OXM:
556 case MFF_ACTSET_OUTPUT: {
558 return !ofputil_port_from_ofp11(value->be32, &port);
562 return !(value->u8 & ~IP_DSCP_MASK);
563 case MFF_IP_DSCP_SHIFTED:
564 return !(value->u8 & (~IP_DSCP_MASK >> 2));
566 return !(value->u8 & ~IP_ECN_MASK);
568 return !(value->u8 & ~FLOW_NW_FRAG_MASK);
570 return !(value->be16 & ~htons(0x0fff));
573 return !(value->be16 & htons(0xff00));
576 return !(value->be16 & htons(VLAN_CFI | VLAN_PCP_MASK));
578 return !(value->be16 & htons(VLAN_PCP_MASK));
580 case MFF_DL_VLAN_PCP:
582 return !(value->u8 & ~(VLAN_PCP_MASK >> VLAN_PCP_SHIFT));
585 return !(value->be32 & ~htonl(IPV6_LABEL_MASK));
588 return !(value->be32 & ~htonl(MPLS_LABEL_MASK >> MPLS_LABEL_SHIFT));
591 return !(value->u8 & ~(MPLS_TC_MASK >> MPLS_TC_SHIFT));
594 return !(value->u8 & ~(MPLS_BOS_MASK >> MPLS_BOS_SHIFT));
597 return !(value->be16 & ~htons(FLOW_TNL_PUB_F_MASK));
600 return !(value->be32 & ~htonl(CS_SUPPORTED_MASK));
608 /* Copies the value of field 'mf' from 'flow' into 'value'. The caller is
609 * responsible for ensuring that 'flow' meets 'mf''s prerequisites. */
611 mf_get_value(const struct mf_field *mf, const struct flow *flow,
612 union mf_value *value)
616 value->be32 = htonl(flow->dp_hash);
619 value->be32 = htonl(flow->recirc_id);
622 value->be32 = htonl(flow->conj_id);
625 value->be64 = flow->tunnel.tun_id;
628 value->be32 = flow->tunnel.ip_src;
631 value->be32 = flow->tunnel.ip_dst;
633 case MFF_TUN_IPV6_SRC:
634 value->ipv6 = flow->tunnel.ipv6_src;
636 case MFF_TUN_IPV6_DST:
637 value->ipv6 = flow->tunnel.ipv6_dst;
640 value->be16 = htons(flow->tunnel.flags & FLOW_TNL_PUB_F_MASK);
643 value->be16 = flow->tunnel.gbp_id;
645 case MFF_TUN_GBP_FLAGS:
646 value->u8 = flow->tunnel.gbp_flags;
649 value->u8 = flow->tunnel.ip_ttl;
652 value->u8 = flow->tunnel.ip_tos;
654 CASE_MFF_TUN_METADATA:
655 tun_metadata_read(&flow->tunnel, mf, value);
659 value->be64 = flow->metadata;
663 value->be16 = htons(ofp_to_u16(flow->in_port.ofp_port));
665 case MFF_IN_PORT_OXM:
666 value->be32 = ofputil_port_to_ofp11(flow->in_port.ofp_port);
668 case MFF_ACTSET_OUTPUT:
669 value->be32 = ofputil_port_to_ofp11(flow->actset_output);
672 case MFF_SKB_PRIORITY:
673 value->be32 = htonl(flow->skb_priority);
677 value->be32 = htonl(flow->pkt_mark);
681 value->be32 = htonl(flow->ct_state);
685 value->be16 = htons(flow->ct_zone);
689 value->be32 = htonl(flow->ct_mark);
693 value->be128 = hton128(flow->ct_label);
697 value->be32 = htonl(flow->regs[mf->id - MFF_REG0]);
701 value->be64 = htonll(flow_get_xreg(flow, mf->id - MFF_XREG0));
705 value->mac = flow->dl_src;
709 value->mac = flow->dl_dst;
713 value->be16 = flow->dl_type;
717 value->be16 = flow->vlan_tci;
721 value->be16 = flow->vlan_tci & htons(VLAN_VID_MASK);
724 value->be16 = flow->vlan_tci & htons(VLAN_VID_MASK | VLAN_CFI);
727 case MFF_DL_VLAN_PCP:
729 value->u8 = vlan_tci_to_pcp(flow->vlan_tci);
733 value->be32 = htonl(mpls_lse_to_label(flow->mpls_lse[0]));
737 value->u8 = mpls_lse_to_tc(flow->mpls_lse[0]);
741 value->u8 = mpls_lse_to_bos(flow->mpls_lse[0]);
745 value->be32 = flow->nw_src;
749 value->be32 = flow->nw_dst;
753 value->ipv6 = flow->ipv6_src;
757 value->ipv6 = flow->ipv6_dst;
761 value->be32 = flow->ipv6_label;
765 value->u8 = flow->nw_proto;
769 value->u8 = flow->nw_tos & IP_DSCP_MASK;
772 case MFF_IP_DSCP_SHIFTED:
773 value->u8 = flow->nw_tos >> 2;
777 value->u8 = flow->nw_tos & IP_ECN_MASK;
781 value->u8 = flow->nw_ttl;
785 value->u8 = flow->nw_frag;
789 value->be16 = htons(flow->nw_proto);
793 value->be32 = flow->nw_src;
797 value->be32 = flow->nw_dst;
802 value->mac = flow->arp_sha;
807 value->mac = flow->arp_tha;
813 value->be16 = flow->tp_src;
819 value->be16 = flow->tp_dst;
823 value->be16 = flow->tcp_flags;
826 case MFF_ICMPV4_TYPE:
827 case MFF_ICMPV6_TYPE:
828 value->u8 = ntohs(flow->tp_src);
831 case MFF_ICMPV4_CODE:
832 case MFF_ICMPV6_CODE:
833 value->u8 = ntohs(flow->tp_dst);
837 value->ipv6 = flow->nd_target;
846 /* Makes 'match' match field 'mf' exactly, with the value matched taken from
847 * 'value'. The caller is responsible for ensuring that 'match' meets 'mf''s
850 * If non-NULL, 'err_str' returns a malloc'ed string describing any errors
851 * with the request or NULL if there is no error. The caller is reponsible
852 * for freeing the string. */
854 mf_set_value(const struct mf_field *mf,
855 const union mf_value *value, struct match *match, char **err_str)
863 match_set_dp_hash(match, ntohl(value->be32));
866 match_set_recirc_id(match, ntohl(value->be32));
869 match_set_conj_id(match, ntohl(value->be32));
872 match_set_tun_id(match, value->be64);
875 match_set_tun_src(match, value->be32);
878 match_set_tun_dst(match, value->be32);
880 case MFF_TUN_IPV6_SRC:
881 match_set_tun_ipv6_src(match, &value->ipv6);
883 case MFF_TUN_IPV6_DST:
884 match_set_tun_ipv6_dst(match, &value->ipv6);
887 match_set_tun_flags(match, ntohs(value->be16));
890 match_set_tun_gbp_id(match, value->be16);
892 case MFF_TUN_GBP_FLAGS:
893 match_set_tun_gbp_flags(match, value->u8);
896 match_set_tun_tos(match, value->u8);
899 match_set_tun_ttl(match, value->u8);
901 CASE_MFF_TUN_METADATA:
902 tun_metadata_set_match(mf, value, NULL, match, err_str);
906 match_set_metadata(match, value->be64);
910 match_set_in_port(match, u16_to_ofp(ntohs(value->be16)));
913 case MFF_IN_PORT_OXM: {
915 ofputil_port_from_ofp11(value->be32, &port);
916 match_set_in_port(match, port);
919 case MFF_ACTSET_OUTPUT: {
921 ofputil_port_from_ofp11(value->be32, &port);
922 match_set_actset_output(match, port);
926 case MFF_SKB_PRIORITY:
927 match_set_skb_priority(match, ntohl(value->be32));
931 match_set_pkt_mark(match, ntohl(value->be32));
935 match_set_ct_state(match, ntohl(value->be32));
939 match_set_ct_zone(match, ntohs(value->be16));
943 match_set_ct_mark(match, ntohl(value->be32));
947 match_set_ct_label(match, ntoh128(value->be128));
951 match_set_reg(match, mf->id - MFF_REG0, ntohl(value->be32));
955 match_set_xreg(match, mf->id - MFF_XREG0, ntohll(value->be64));
959 match_set_dl_src(match, value->mac);
963 match_set_dl_dst(match, value->mac);
967 match_set_dl_type(match, value->be16);
971 match_set_dl_tci(match, value->be16);
975 match_set_dl_vlan(match, value->be16);
978 match_set_vlan_vid(match, value->be16);
981 case MFF_DL_VLAN_PCP:
983 match_set_dl_vlan_pcp(match, value->u8);
987 match_set_mpls_label(match, 0, value->be32);
991 match_set_mpls_tc(match, 0, value->u8);
995 match_set_mpls_bos(match, 0, value->u8);
999 match_set_nw_src(match, value->be32);
1003 match_set_nw_dst(match, value->be32);
1007 match_set_ipv6_src(match, &value->ipv6);
1011 match_set_ipv6_dst(match, &value->ipv6);
1014 case MFF_IPV6_LABEL:
1015 match_set_ipv6_label(match, value->be32);
1019 match_set_nw_proto(match, value->u8);
1023 match_set_nw_dscp(match, value->u8);
1026 case MFF_IP_DSCP_SHIFTED:
1027 match_set_nw_dscp(match, value->u8 << 2);
1031 match_set_nw_ecn(match, value->u8);
1035 match_set_nw_ttl(match, value->u8);
1039 match_set_nw_frag(match, value->u8);
1043 match_set_nw_proto(match, ntohs(value->be16));
1047 match_set_nw_src(match, value->be32);
1051 match_set_nw_dst(match, value->be32);
1056 match_set_arp_sha(match, value->mac);
1061 match_set_arp_tha(match, value->mac);
1067 match_set_tp_src(match, value->be16);
1073 match_set_tp_dst(match, value->be16);
1077 match_set_tcp_flags(match, value->be16);
1080 case MFF_ICMPV4_TYPE:
1081 case MFF_ICMPV6_TYPE:
1082 match_set_icmp_type(match, value->u8);
1085 case MFF_ICMPV4_CODE:
1086 case MFF_ICMPV6_CODE:
1087 match_set_icmp_code(match, value->u8);
1091 match_set_nd_target(match, &value->ipv6);
1100 /* Unwildcard 'mask' member field described by 'mf'. The caller is
1101 * responsible for ensuring that 'mask' meets 'mf''s prerequisites. */
1103 mf_mask_field(const struct mf_field *mf, struct flow *mask)
1105 /* For MFF_DL_VLAN, we cannot send a all 1's to flow_set_dl_vlan()
1106 * as that will be considered as OFP10_VLAN_NONE. So consider it as a
1107 * special case. For the rest, calling mf_set_flow_value() is good
1109 if (mf->id == MFF_DL_VLAN) {
1110 flow_set_dl_vlan(mask, htons(VLAN_VID_MASK));
1112 mf_set_flow_value(mf, &exact_match_mask, mask);
1117 field_len(const struct mf_field *mf, const union mf_value *value_)
1119 const uint8_t *value = &value_->u8;
1122 if (!mf->variable_len) {
1130 for (i = 0; i < mf->n_bytes; i++) {
1131 if (value[i] != 0) {
1136 return mf->n_bytes - i;
1139 /* Returns the effective length of the field. For fixed length fields,
1140 * this is just the defined length. For variable length fields, it is
1141 * the minimum size encoding that retains the same meaning (i.e.
1142 * discarding leading zeros).
1144 * 'is_masked' returns (if non-NULL) whether the original contained
1145 * a mask. Otherwise, a mask that is the same length as the value
1146 * might be misinterpreted as an exact match. */
1148 mf_field_len(const struct mf_field *mf, const union mf_value *value,
1149 const union mf_value *mask, bool *is_masked_)
1152 bool is_masked = mask && !is_all_ones(mask, mf->n_bytes);
1154 len = field_len(mf, value);
1156 mask_len = field_len(mf, mask);
1157 len = MAX(len, mask_len);
1161 *is_masked_ = is_masked;
1167 /* Sets 'flow' member field described by 'mf' to 'value'. The caller is
1168 * responsible for ensuring that 'flow' meets 'mf''s prerequisites.*/
1170 mf_set_flow_value(const struct mf_field *mf,
1171 const union mf_value *value, struct flow *flow)
1175 flow->dp_hash = ntohl(value->be32);
1178 flow->recirc_id = ntohl(value->be32);
1181 flow->conj_id = ntohl(value->be32);
1184 flow->tunnel.tun_id = value->be64;
1187 flow->tunnel.ip_src = value->be32;
1190 flow->tunnel.ip_dst = value->be32;
1192 case MFF_TUN_IPV6_SRC:
1193 flow->tunnel.ipv6_src = value->ipv6;
1195 case MFF_TUN_IPV6_DST:
1196 flow->tunnel.ipv6_dst = value->ipv6;
1199 flow->tunnel.flags = (flow->tunnel.flags & ~FLOW_TNL_PUB_F_MASK) |
1202 case MFF_TUN_GBP_ID:
1203 flow->tunnel.gbp_id = value->be16;
1205 case MFF_TUN_GBP_FLAGS:
1206 flow->tunnel.gbp_flags = value->u8;
1209 flow->tunnel.ip_tos = value->u8;
1212 flow->tunnel.ip_ttl = value->u8;
1214 CASE_MFF_TUN_METADATA:
1215 tun_metadata_write(&flow->tunnel, mf, value);
1218 flow->metadata = value->be64;
1222 flow->in_port.ofp_port = u16_to_ofp(ntohs(value->be16));
1225 case MFF_IN_PORT_OXM:
1226 ofputil_port_from_ofp11(value->be32, &flow->in_port.ofp_port);
1228 case MFF_ACTSET_OUTPUT:
1229 ofputil_port_from_ofp11(value->be32, &flow->actset_output);
1232 case MFF_SKB_PRIORITY:
1233 flow->skb_priority = ntohl(value->be32);
1237 flow->pkt_mark = ntohl(value->be32);
1241 flow->ct_state = ntohl(value->be32);
1245 flow->ct_zone = ntohs(value->be16);
1249 flow->ct_mark = ntohl(value->be32);
1253 flow->ct_label = ntoh128(value->be128);
1257 flow->regs[mf->id - MFF_REG0] = ntohl(value->be32);
1261 flow_set_xreg(flow, mf->id - MFF_XREG0, ntohll(value->be64));
1265 flow->dl_src = value->mac;
1269 flow->dl_dst = value->mac;
1273 flow->dl_type = value->be16;
1277 flow->vlan_tci = value->be16;
1281 flow_set_dl_vlan(flow, value->be16);
1284 flow_set_vlan_vid(flow, value->be16);
1287 case MFF_DL_VLAN_PCP:
1289 flow_set_vlan_pcp(flow, value->u8);
1292 case MFF_MPLS_LABEL:
1293 flow_set_mpls_label(flow, 0, value->be32);
1297 flow_set_mpls_tc(flow, 0, value->u8);
1301 flow_set_mpls_bos(flow, 0, value->u8);
1305 flow->nw_src = value->be32;
1309 flow->nw_dst = value->be32;
1313 flow->ipv6_src = value->ipv6;
1317 flow->ipv6_dst = value->ipv6;
1320 case MFF_IPV6_LABEL:
1321 flow->ipv6_label = value->be32 & htonl(IPV6_LABEL_MASK);
1325 flow->nw_proto = value->u8;
1329 flow->nw_tos &= ~IP_DSCP_MASK;
1330 flow->nw_tos |= value->u8 & IP_DSCP_MASK;
1333 case MFF_IP_DSCP_SHIFTED:
1334 flow->nw_tos &= ~IP_DSCP_MASK;
1335 flow->nw_tos |= value->u8 << 2;
1339 flow->nw_tos &= ~IP_ECN_MASK;
1340 flow->nw_tos |= value->u8 & IP_ECN_MASK;
1344 flow->nw_ttl = value->u8;
1348 flow->nw_frag = value->u8 & FLOW_NW_FRAG_MASK;
1352 flow->nw_proto = ntohs(value->be16);
1356 flow->nw_src = value->be32;
1360 flow->nw_dst = value->be32;
1365 flow->arp_sha = value->mac;
1370 flow->arp_tha = value->mac;
1376 flow->tp_src = value->be16;
1382 flow->tp_dst = value->be16;
1386 flow->tcp_flags = value->be16;
1389 case MFF_ICMPV4_TYPE:
1390 case MFF_ICMPV6_TYPE:
1391 flow->tp_src = htons(value->u8);
1394 case MFF_ICMPV4_CODE:
1395 case MFF_ICMPV6_CODE:
1396 flow->tp_dst = htons(value->u8);
1400 flow->nd_target = value->ipv6;
1409 /* Consider each of 'src', 'mask', and 'dst' as if they were arrays of 8*n
1410 * bits. Then, for each 0 <= i < 8 * n such that mask[i] == 1, sets dst[i] =
1413 apply_mask(const uint8_t *src, const uint8_t *mask, uint8_t *dst, size_t n)
1417 for (i = 0; i < n; i++) {
1418 dst[i] = (src[i] & mask[i]) | (dst[i] & ~mask[i]);
1422 /* Sets 'flow' member field described by 'field' to 'value', except that bits
1423 * for which 'mask' has a 0-bit keep their existing values. The caller is
1424 * responsible for ensuring that 'flow' meets 'field''s prerequisites.*/
1426 mf_set_flow_value_masked(const struct mf_field *field,
1427 const union mf_value *value,
1428 const union mf_value *mask,
1433 mf_get_value(field, flow, &tmp);
1434 apply_mask((const uint8_t *) value, (const uint8_t *) mask,
1435 (uint8_t *) &tmp, field->n_bytes);
1436 mf_set_flow_value(field, &tmp, flow);
1440 mf_is_tun_metadata(const struct mf_field *mf)
1442 return mf->id >= MFF_TUN_METADATA0 &&
1443 mf->id < MFF_TUN_METADATA0 + TUN_METADATA_NUM_OPTS;
1446 /* Returns true if 'mf' has previously been set in 'flow', false if
1447 * it contains a non-default value.
1449 * The caller is responsible for ensuring that 'flow' meets 'mf''s
1452 mf_is_set(const struct mf_field *mf, const struct flow *flow)
1454 if (!mf_is_tun_metadata(mf)) {
1455 union mf_value value;
1457 mf_get_value(mf, flow, &value);
1458 return !is_all_zeros(&value, mf->n_bytes);
1460 return ULLONG_GET(flow->tunnel.metadata.present.map,
1461 mf->id - MFF_TUN_METADATA0);
1465 /* Makes 'match' wildcard field 'mf'.
1467 * The caller is responsible for ensuring that 'match' meets 'mf''s
1470 * If non-NULL, 'err_str' returns a malloc'ed string describing any errors
1471 * with the request or NULL if there is no error. The caller is reponsible
1472 * for freeing the string. */
1474 mf_set_wild(const struct mf_field *mf, struct match *match, char **err_str)
1482 match->flow.dp_hash = 0;
1483 match->wc.masks.dp_hash = 0;
1486 match->flow.recirc_id = 0;
1487 match->wc.masks.recirc_id = 0;
1490 match->flow.conj_id = 0;
1491 match->wc.masks.conj_id = 0;
1494 match_set_tun_id_masked(match, htonll(0), htonll(0));
1497 match_set_tun_src_masked(match, htonl(0), htonl(0));
1500 match_set_tun_dst_masked(match, htonl(0), htonl(0));
1502 case MFF_TUN_IPV6_SRC:
1503 memset(&match->wc.masks.tunnel.ipv6_src, 0,
1504 sizeof match->wc.masks.tunnel.ipv6_src);
1505 memset(&match->flow.tunnel.ipv6_src, 0,
1506 sizeof match->flow.tunnel.ipv6_src);
1508 case MFF_TUN_IPV6_DST:
1509 memset(&match->wc.masks.tunnel.ipv6_dst, 0,
1510 sizeof match->wc.masks.tunnel.ipv6_dst);
1511 memset(&match->flow.tunnel.ipv6_dst, 0,
1512 sizeof match->flow.tunnel.ipv6_dst);
1515 match_set_tun_flags_masked(match, 0, 0);
1517 case MFF_TUN_GBP_ID:
1518 match_set_tun_gbp_id_masked(match, 0, 0);
1520 case MFF_TUN_GBP_FLAGS:
1521 match_set_tun_gbp_flags_masked(match, 0, 0);
1524 match_set_tun_tos_masked(match, 0, 0);
1527 match_set_tun_ttl_masked(match, 0, 0);
1529 CASE_MFF_TUN_METADATA:
1530 tun_metadata_set_match(mf, NULL, NULL, match, err_str);
1534 match_set_metadata_masked(match, htonll(0), htonll(0));
1538 case MFF_IN_PORT_OXM:
1539 match->flow.in_port.ofp_port = 0;
1540 match->wc.masks.in_port.ofp_port = 0;
1542 case MFF_ACTSET_OUTPUT:
1543 match->flow.actset_output = 0;
1544 match->wc.masks.actset_output = 0;
1547 case MFF_SKB_PRIORITY:
1548 match->flow.skb_priority = 0;
1549 match->wc.masks.skb_priority = 0;
1553 match->flow.pkt_mark = 0;
1554 match->wc.masks.pkt_mark = 0;
1558 match->flow.ct_state = 0;
1559 match->wc.masks.ct_state = 0;
1563 match->flow.ct_zone = 0;
1564 match->wc.masks.ct_zone = 0;
1568 match->flow.ct_mark = 0;
1569 match->wc.masks.ct_mark = 0;
1573 memset(&match->flow.ct_label, 0, sizeof(match->flow.ct_label));
1574 memset(&match->wc.masks.ct_label, 0, sizeof(match->wc.masks.ct_label));
1578 match_set_reg_masked(match, mf->id - MFF_REG0, 0, 0);
1582 match_set_xreg_masked(match, mf->id - MFF_XREG0, 0, 0);
1586 match->flow.dl_src = eth_addr_zero;
1587 match->wc.masks.dl_src = eth_addr_zero;
1591 match->flow.dl_dst = eth_addr_zero;
1592 match->wc.masks.dl_dst = eth_addr_zero;
1596 match->flow.dl_type = htons(0);
1597 match->wc.masks.dl_type = htons(0);
1601 match_set_dl_tci_masked(match, htons(0), htons(0));
1606 match_set_any_vid(match);
1609 case MFF_DL_VLAN_PCP:
1611 match_set_any_pcp(match);
1614 case MFF_MPLS_LABEL:
1615 match_set_any_mpls_label(match, 0);
1619 match_set_any_mpls_tc(match, 0);
1623 match_set_any_mpls_bos(match, 0);
1628 match_set_nw_src_masked(match, htonl(0), htonl(0));
1633 match_set_nw_dst_masked(match, htonl(0), htonl(0));
1637 memset(&match->wc.masks.ipv6_src, 0, sizeof match->wc.masks.ipv6_src);
1638 memset(&match->flow.ipv6_src, 0, sizeof match->flow.ipv6_src);
1642 memset(&match->wc.masks.ipv6_dst, 0, sizeof match->wc.masks.ipv6_dst);
1643 memset(&match->flow.ipv6_dst, 0, sizeof match->flow.ipv6_dst);
1646 case MFF_IPV6_LABEL:
1647 match->wc.masks.ipv6_label = htonl(0);
1648 match->flow.ipv6_label = htonl(0);
1652 match->wc.masks.nw_proto = 0;
1653 match->flow.nw_proto = 0;
1657 case MFF_IP_DSCP_SHIFTED:
1658 match->wc.masks.nw_tos &= ~IP_DSCP_MASK;
1659 match->flow.nw_tos &= ~IP_DSCP_MASK;
1663 match->wc.masks.nw_tos &= ~IP_ECN_MASK;
1664 match->flow.nw_tos &= ~IP_ECN_MASK;
1668 match->wc.masks.nw_ttl = 0;
1669 match->flow.nw_ttl = 0;
1673 match->wc.masks.nw_frag &= ~FLOW_NW_FRAG_MASK;
1674 match->flow.nw_frag &= ~FLOW_NW_FRAG_MASK;
1678 match->wc.masks.nw_proto = 0;
1679 match->flow.nw_proto = 0;
1684 match->flow.arp_sha = eth_addr_zero;
1685 match->wc.masks.arp_sha = eth_addr_zero;
1690 match->flow.arp_tha = eth_addr_zero;
1691 match->wc.masks.arp_tha = eth_addr_zero;
1697 case MFF_ICMPV4_TYPE:
1698 case MFF_ICMPV6_TYPE:
1699 match->wc.masks.tp_src = htons(0);
1700 match->flow.tp_src = htons(0);
1706 case MFF_ICMPV4_CODE:
1707 case MFF_ICMPV6_CODE:
1708 match->wc.masks.tp_dst = htons(0);
1709 match->flow.tp_dst = htons(0);
1713 match->wc.masks.tcp_flags = htons(0);
1714 match->flow.tcp_flags = htons(0);
1718 memset(&match->wc.masks.nd_target, 0,
1719 sizeof match->wc.masks.nd_target);
1720 memset(&match->flow.nd_target, 0, sizeof match->flow.nd_target);
1729 /* Makes 'match' match field 'mf' with the specified 'value' and 'mask'.
1730 * 'value' specifies a value to match and 'mask' specifies a wildcard pattern,
1731 * with a 1-bit indicating that the corresponding value bit must match and a
1732 * 0-bit indicating a don't-care.
1734 * If 'mask' is NULL or points to all-1-bits, then this call is equivalent to
1735 * mf_set_value(mf, value, match). If 'mask' points to all-0-bits, then this
1736 * call is equivalent to mf_set_wild(mf, match).
1738 * 'mask' must be a valid mask for 'mf' (see mf_is_mask_valid()). The caller
1739 * is responsible for ensuring that 'match' meets 'mf''s prerequisites.
1741 * If non-NULL, 'err_str' returns a malloc'ed string describing any errors
1742 * with the request or NULL if there is no error. The caller is reponsible
1743 * for freeing the string.
1745 * Return a set of enum ofputil_protocol bits (as an uint32_t to avoid circular
1746 * dependency on enum ofputil_protocol definition) indicating which OpenFlow
1747 * protocol versions can support this functionality. */
1749 mf_set(const struct mf_field *mf,
1750 const union mf_value *value, const union mf_value *mask,
1751 struct match *match, char **err_str)
1753 if (!mask || is_all_ones(mask, mf->n_bytes)) {
1754 mf_set_value(mf, value, match, err_str);
1755 return mf->usable_protocols_exact;
1756 } else if (is_all_zeros(mask, mf->n_bytes) && !mf_is_tun_metadata(mf)) {
1757 /* Tunnel metadata matches on the existence of the field itself, so
1758 * it still needs to be encoded even if the value is wildcarded. */
1759 mf_set_wild(mf, match, err_str);
1760 return OFPUTIL_P_ANY;
1772 case MFF_IN_PORT_OXM:
1773 case MFF_ACTSET_OUTPUT:
1774 case MFF_SKB_PRIORITY:
1777 case MFF_DL_VLAN_PCP:
1779 case MFF_MPLS_LABEL:
1785 case MFF_IP_DSCP_SHIFTED:
1788 case MFF_ICMPV4_TYPE:
1789 case MFF_ICMPV4_CODE:
1790 case MFF_ICMPV6_TYPE:
1791 case MFF_ICMPV6_CODE:
1792 return OFPUTIL_P_NONE;
1795 match_set_dp_hash_masked(match, ntohl(value->be32), ntohl(mask->be32));
1798 match_set_tun_id_masked(match, value->be64, mask->be64);
1801 match_set_tun_src_masked(match, value->be32, mask->be32);
1804 match_set_tun_dst_masked(match, value->be32, mask->be32);
1806 case MFF_TUN_IPV6_SRC:
1807 match_set_tun_ipv6_src_masked(match, &value->ipv6, &mask->ipv6);
1809 case MFF_TUN_IPV6_DST:
1810 match_set_tun_ipv6_dst_masked(match, &value->ipv6, &mask->ipv6);
1813 match_set_tun_flags_masked(match, ntohs(value->be16), ntohs(mask->be16));
1815 case MFF_TUN_GBP_ID:
1816 match_set_tun_gbp_id_masked(match, value->be16, mask->be16);
1818 case MFF_TUN_GBP_FLAGS:
1819 match_set_tun_gbp_flags_masked(match, value->u8, mask->u8);
1822 match_set_tun_ttl_masked(match, value->u8, mask->u8);
1825 match_set_tun_tos_masked(match, value->u8, mask->u8);
1827 CASE_MFF_TUN_METADATA:
1828 tun_metadata_set_match(mf, value, mask, match, err_str);
1832 match_set_metadata_masked(match, value->be64, mask->be64);
1836 match_set_reg_masked(match, mf->id - MFF_REG0,
1837 ntohl(value->be32), ntohl(mask->be32));
1841 match_set_xreg_masked(match, mf->id - MFF_XREG0,
1842 ntohll(value->be64), ntohll(mask->be64));
1846 match_set_pkt_mark_masked(match, ntohl(value->be32),
1851 match_set_ct_state_masked(match, ntohl(value->be32), ntohl(mask->be32));
1855 match_set_ct_mark_masked(match, ntohl(value->be32), ntohl(mask->be32));
1859 match_set_ct_label_masked(match, ntoh128(value->be128),
1860 mask ? ntoh128(mask->be128) : OVS_U128_MAX);
1864 match_set_dl_dst_masked(match, value->mac, mask->mac);
1868 match_set_dl_src_masked(match, value->mac, mask->mac);
1873 match_set_arp_sha_masked(match, value->mac, mask->mac);
1878 match_set_arp_tha_masked(match, value->mac, mask->mac);
1882 match_set_dl_tci_masked(match, value->be16, mask->be16);
1886 match_set_vlan_vid_masked(match, value->be16, mask->be16);
1890 match_set_nw_src_masked(match, value->be32, mask->be32);
1894 match_set_nw_dst_masked(match, value->be32, mask->be32);
1898 match_set_ipv6_src_masked(match, &value->ipv6, &mask->ipv6);
1902 match_set_ipv6_dst_masked(match, &value->ipv6, &mask->ipv6);
1905 case MFF_IPV6_LABEL:
1906 if ((mask->be32 & htonl(IPV6_LABEL_MASK)) == htonl(IPV6_LABEL_MASK)) {
1907 mf_set_value(mf, value, match, err_str);
1909 match_set_ipv6_label_masked(match, value->be32, mask->be32);
1914 match_set_nd_target_masked(match, &value->ipv6, &mask->ipv6);
1918 match_set_nw_frag_masked(match, value->u8, mask->u8);
1922 match_set_nw_src_masked(match, value->be32, mask->be32);
1926 match_set_nw_dst_masked(match, value->be32, mask->be32);
1932 match_set_tp_src_masked(match, value->be16, mask->be16);
1938 match_set_tp_dst_masked(match, value->be16, mask->be16);
1942 match_set_tcp_flags_masked(match, value->be16, mask->be16);
1950 return ((mf->usable_protocols_bitwise == mf->usable_protocols_cidr
1951 || ip_is_cidr(mask->be32))
1952 ? mf->usable_protocols_cidr
1953 : mf->usable_protocols_bitwise);
1957 mf_check__(const struct mf_subfield *sf, const struct flow *flow,
1961 VLOG_WARN_RL(&rl, "unknown %s field", type);
1962 return OFPERR_OFPBAC_BAD_SET_TYPE;
1963 } else if (!sf->n_bits) {
1964 VLOG_WARN_RL(&rl, "zero bit %s field %s", type, sf->field->name);
1965 return OFPERR_OFPBAC_BAD_SET_LEN;
1966 } else if (sf->ofs >= sf->field->n_bits) {
1967 VLOG_WARN_RL(&rl, "bit offset %d exceeds %d-bit width of %s field %s",
1968 sf->ofs, sf->field->n_bits, type, sf->field->name);
1969 return OFPERR_OFPBAC_BAD_SET_LEN;
1970 } else if (sf->ofs + sf->n_bits > sf->field->n_bits) {
1971 VLOG_WARN_RL(&rl, "bit offset %d and width %d exceeds %d-bit width "
1972 "of %s field %s", sf->ofs, sf->n_bits,
1973 sf->field->n_bits, type, sf->field->name);
1974 return OFPERR_OFPBAC_BAD_SET_LEN;
1975 } else if (flow && !mf_are_prereqs_ok(sf->field, flow)) {
1976 VLOG_WARN_RL(&rl, "%s field %s lacks correct prerequisites",
1977 type, sf->field->name);
1978 return OFPERR_OFPBAC_MATCH_INCONSISTENT;
1984 /* Checks whether 'sf' is valid for reading a subfield out of 'flow'. Returns
1985 * 0 if so, otherwise an OpenFlow error code (e.g. as returned by
1988 mf_check_src(const struct mf_subfield *sf, const struct flow *flow)
1990 return mf_check__(sf, flow, "source");
1993 /* Checks whether 'sf' is valid for writing a subfield into 'flow'. Returns 0
1994 * if so, otherwise an OpenFlow error code (e.g. as returned by
1997 mf_check_dst(const struct mf_subfield *sf, const struct flow *flow)
1999 int error = mf_check__(sf, flow, "destination");
2000 if (!error && !sf->field->writable) {
2001 VLOG_WARN_RL(&rl, "destination field %s is not writable",
2003 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
2008 /* Copies the value and wildcard bit pattern for 'mf' from 'match' into the
2009 * 'value' and 'mask', respectively. */
2011 mf_get(const struct mf_field *mf, const struct match *match,
2012 union mf_value *value, union mf_value *mask)
2014 mf_get_value(mf, &match->flow, value);
2015 mf_get_mask(mf, &match->wc, mask);
2019 mf_from_integer_string(const struct mf_field *mf, const char *s,
2020 uint8_t *valuep, uint8_t *maskp)
2023 const char *err_str = "";
2026 err = parse_int_string(s, valuep, mf->n_bytes, &tail);
2027 if (err || (*tail != '\0' && *tail != '/')) {
2033 err = parse_int_string(tail + 1, maskp, mf->n_bytes, &tail);
2034 if (err || *tail != '\0') {
2039 memset(maskp, 0xff, mf->n_bytes);
2045 if (err == ERANGE) {
2046 return xasprintf("%s: %s too large for %u-byte field %s",
2047 s, err_str, mf->n_bytes, mf->name);
2049 return xasprintf("%s: bad syntax for %s %s", s, mf->name, err_str);
2054 mf_from_ethernet_string(const struct mf_field *mf, const char *s,
2055 struct eth_addr *mac, struct eth_addr *mask)
2059 ovs_assert(mf->n_bytes == ETH_ADDR_LEN);
2062 if (ovs_scan(s, ETH_ADDR_SCAN_FMT"%n", ETH_ADDR_SCAN_ARGS(*mac), &n)
2063 && n == strlen(s)) {
2064 *mask = eth_addr_exact;
2069 if (ovs_scan(s, ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT"%n",
2070 ETH_ADDR_SCAN_ARGS(*mac), ETH_ADDR_SCAN_ARGS(*mask), &n)
2071 && n == strlen(s)) {
2075 return xasprintf("%s: invalid Ethernet address", s);
2079 mf_from_ipv4_string(const struct mf_field *mf, const char *s,
2080 ovs_be32 *ip, ovs_be32 *mask)
2082 ovs_assert(mf->n_bytes == sizeof *ip);
2083 return ip_parse_masked(s, ip, mask);
2087 mf_from_ipv6_string(const struct mf_field *mf, const char *s,
2088 struct in6_addr *ipv6, struct in6_addr *mask)
2090 ovs_assert(mf->n_bytes == sizeof *ipv6);
2091 return ipv6_parse_masked(s, ipv6, mask);
2095 mf_from_ofp_port_string(const struct mf_field *mf, const char *s,
2096 ovs_be16 *valuep, ovs_be16 *maskp)
2100 ovs_assert(mf->n_bytes == sizeof(ovs_be16));
2102 if (ofputil_port_from_string(s, &port)) {
2103 *valuep = htons(ofp_to_u16(port));
2104 *maskp = OVS_BE16_MAX;
2107 return xasprintf("%s: port value out of range for %s", s, mf->name);
2111 mf_from_ofp_port_string32(const struct mf_field *mf, const char *s,
2112 ovs_be32 *valuep, ovs_be32 *maskp)
2116 ovs_assert(mf->n_bytes == sizeof(ovs_be32));
2117 if (ofputil_port_from_string(s, &port)) {
2118 *valuep = ofputil_port_to_ofp11(port);
2119 *maskp = OVS_BE32_MAX;
2122 return xasprintf("%s: port value out of range for %s", s, mf->name);
2125 struct frag_handling {
2131 static const struct frag_handling all_frags[] = {
2132 #define A FLOW_NW_FRAG_ANY
2133 #define L FLOW_NW_FRAG_LATER
2134 /* name mask value */
2137 { "first", A|L, A },
2138 { "later", A|L, A|L },
2143 { "not_later", L, 0 },
2150 mf_from_frag_string(const char *s, uint8_t *valuep, uint8_t *maskp)
2152 const struct frag_handling *h;
2154 for (h = all_frags; h < &all_frags[ARRAY_SIZE(all_frags)]; h++) {
2155 if (!strcasecmp(s, h->name)) {
2156 /* We force the upper bits of the mask on to make mf_parse_value()
2157 * happy (otherwise it will never think it's an exact match.) */
2158 *maskp = h->mask | ~FLOW_NW_FRAG_MASK;
2164 return xasprintf("%s: unknown fragment type (valid types are \"no\", "
2165 "\"yes\", \"first\", \"later\", \"not_first\"", s);
2169 parse_mf_flags(const char *s, const char *(*bit_to_string)(uint32_t),
2170 const char *field_name, ovs_be16 *flagsp, ovs_be16 allowed,
2175 uint32_t flags, mask;
2177 err = parse_flags(s, bit_to_string, '\0', field_name, &err_str,
2178 &flags, ntohs(allowed), maskp ? &mask : NULL);
2183 *flagsp = htons(flags);
2185 *maskp = htons(mask);
2192 mf_from_tcp_flags_string(const char *s, ovs_be16 *flagsp, ovs_be16 *maskp)
2194 return parse_mf_flags(s, packet_tcp_flag_to_string, "TCP", flagsp,
2195 TCP_FLAGS_BE16(OVS_BE16_MAX), maskp);
2199 mf_from_tun_flags_string(const char *s, ovs_be16 *flagsp, ovs_be16 *maskp)
2201 return parse_mf_flags(s, flow_tun_flag_to_string, "tunnel", flagsp,
2202 htons(FLOW_TNL_PUB_F_MASK), maskp);
2206 mf_from_ct_state_string(const char *s, ovs_be32 *flagsp, ovs_be32 *maskp)
2210 uint32_t flags, mask;
2212 err = parse_flags(s, ct_state_to_string, '\0', "ct_state", &err_str,
2213 &flags, CS_SUPPORTED_MASK, maskp ? &mask : NULL);
2218 *flagsp = htonl(flags);
2220 *maskp = htonl(mask);
2226 /* Parses 's', a string value for field 'mf', into 'value' and 'mask'. Returns
2227 * NULL if successful, otherwise a malloc()'d string describing the error. */
2229 mf_parse(const struct mf_field *mf, const char *s,
2230 union mf_value *value, union mf_value *mask)
2234 if (!strcmp(s, "*")) {
2235 memset(value, 0, mf->n_bytes);
2236 memset(mask, 0, mf->n_bytes);
2240 switch (mf->string) {
2242 case MFS_HEXADECIMAL:
2243 error = mf_from_integer_string(mf, s,
2244 (uint8_t *) value, (uint8_t *) mask);
2248 ovs_assert(mf->n_bytes == sizeof(ovs_be32));
2249 error = mf_from_ct_state_string(s, &value->be32, &mask->be32);
2253 error = mf_from_ethernet_string(mf, s, &value->mac, &mask->mac);
2257 error = mf_from_ipv4_string(mf, s, &value->be32, &mask->be32);
2261 error = mf_from_ipv6_string(mf, s, &value->ipv6, &mask->ipv6);
2265 error = mf_from_ofp_port_string(mf, s, &value->be16, &mask->be16);
2268 case MFS_OFP_PORT_OXM:
2269 error = mf_from_ofp_port_string32(mf, s, &value->be32, &mask->be32);
2273 error = mf_from_frag_string(s, &value->u8, &mask->u8);
2277 ovs_assert(mf->n_bytes == sizeof(ovs_be16));
2278 error = mf_from_tun_flags_string(s, &value->be16, &mask->be16);
2282 ovs_assert(mf->n_bytes == sizeof(ovs_be16));
2283 error = mf_from_tcp_flags_string(s, &value->be16, &mask->be16);
2290 if (!error && !mf_is_mask_valid(mf, mask)) {
2291 error = xasprintf("%s: invalid mask for field %s", s, mf->name);
2296 /* Parses 's', a string value for field 'mf', into 'value'. Returns NULL if
2297 * successful, otherwise a malloc()'d string describing the error. */
2299 mf_parse_value(const struct mf_field *mf, const char *s, union mf_value *value)
2301 union mf_value mask;
2304 error = mf_parse(mf, s, value, &mask);
2309 if (!is_all_ones((const uint8_t *) &mask, mf->n_bytes)) {
2310 return xasprintf("%s: wildcards not allowed here", s);
2316 mf_format_integer_string(const struct mf_field *mf, const uint8_t *valuep,
2317 const uint8_t *maskp, struct ds *s)
2319 if (mf->string == MFS_HEXADECIMAL) {
2320 ds_put_hex(s, valuep, mf->n_bytes);
2322 unsigned long long int integer = 0;
2325 ovs_assert(mf->n_bytes <= 8);
2326 for (i = 0; i < mf->n_bytes; i++) {
2327 integer = (integer << 8) | valuep[i];
2329 ds_put_format(s, "%lld", integer);
2333 /* I guess we could write the mask in decimal for MFS_DECIMAL but I'm
2334 * not sure that that a bit-mask written in decimal is ever easier to
2335 * understand than the same bit-mask written in hexadecimal. */
2336 ds_put_char(s, '/');
2337 ds_put_hex(s, maskp, mf->n_bytes);
2342 mf_format_frag_string(uint8_t value, uint8_t mask, struct ds *s)
2344 const struct frag_handling *h;
2346 mask &= FLOW_NW_FRAG_MASK;
2349 for (h = all_frags; h < &all_frags[ARRAY_SIZE(all_frags)]; h++) {
2350 if (value == h->value && mask == h->mask) {
2351 ds_put_cstr(s, h->name);
2355 ds_put_cstr(s, "<error>");
2359 mf_format_tnl_flags_string(ovs_be16 value, ovs_be16 mask, struct ds *s)
2361 format_flags_masked(s, NULL, flow_tun_flag_to_string, ntohs(value),
2362 ntohs(mask) & FLOW_TNL_PUB_F_MASK, FLOW_TNL_PUB_F_MASK);
2366 mf_format_tcp_flags_string(ovs_be16 value, ovs_be16 mask, struct ds *s)
2368 format_flags_masked(s, NULL, packet_tcp_flag_to_string, ntohs(value),
2369 TCP_FLAGS(mask), TCP_FLAGS(OVS_BE16_MAX));
2373 mf_format_ct_state_string(ovs_be32 value, ovs_be32 mask, struct ds *s)
2375 format_flags_masked(s, NULL, ct_state_to_string, ntohl(value),
2376 ntohl(mask), UINT16_MAX);
2379 /* Appends to 's' a string representation of field 'mf' whose value is in
2380 * 'value' and 'mask'. 'mask' may be NULL to indicate an exact match. */
2382 mf_format(const struct mf_field *mf,
2383 const union mf_value *value, const union mf_value *mask,
2387 if (is_all_zeros(mask, mf->n_bytes)) {
2388 ds_put_cstr(s, "ANY");
2390 } else if (is_all_ones(mask, mf->n_bytes)) {
2395 switch (mf->string) {
2396 case MFS_OFP_PORT_OXM:
2399 ofputil_port_from_ofp11(value->be32, &port);
2400 ofputil_format_port(port, s);
2406 ofputil_format_port(u16_to_ofp(ntohs(value->be16)), s);
2411 case MFS_HEXADECIMAL:
2412 mf_format_integer_string(mf, (uint8_t *) value, (uint8_t *) mask, s);
2416 mf_format_ct_state_string(value->be32,
2417 mask ? mask->be32 : OVS_BE32_MAX, s);
2421 eth_format_masked(value->mac, mask ? &mask->mac : NULL, s);
2425 ip_format_masked(value->be32, mask ? mask->be32 : OVS_BE32_MAX, s);
2429 ipv6_format_masked(&value->ipv6, mask ? &mask->ipv6 : NULL, s);
2433 mf_format_frag_string(value->u8, mask ? mask->u8 : UINT8_MAX, s);
2437 mf_format_tnl_flags_string(value->be16,
2438 mask ? mask->be16 : OVS_BE16_MAX, s);
2442 mf_format_tcp_flags_string(value->be16,
2443 mask ? mask->be16 : OVS_BE16_MAX, s);
2451 /* Makes subfield 'sf' within 'flow' exactly match the 'sf->n_bits'
2452 * least-significant bits in 'x'.
2455 mf_write_subfield_flow(const struct mf_subfield *sf,
2456 const union mf_subvalue *x, struct flow *flow)
2458 const struct mf_field *field = sf->field;
2459 union mf_value value;
2461 mf_get_value(field, flow, &value);
2462 bitwise_copy(x, sizeof *x, 0, &value, field->n_bytes,
2463 sf->ofs, sf->n_bits);
2464 mf_set_flow_value(field, &value, flow);
2467 /* Makes subfield 'sf' within 'match' exactly match the 'sf->n_bits'
2468 * least-significant bits in 'x'.
2471 mf_write_subfield(const struct mf_subfield *sf, const union mf_subvalue *x,
2472 struct match *match)
2474 const struct mf_field *field = sf->field;
2475 union mf_value value, mask;
2477 mf_get(field, match, &value, &mask);
2478 bitwise_copy(x, sizeof *x, 0, &value, field->n_bytes, sf->ofs, sf->n_bits);
2479 bitwise_one ( &mask, field->n_bytes, sf->ofs, sf->n_bits);
2480 mf_set(field, &value, &mask, match, NULL);
2483 /* 'v' and 'm' correspond to values of 'field'. This function copies them into
2484 * 'match' in the correspond positions. */
2486 mf_mask_subfield(const struct mf_field *field,
2487 const union mf_subvalue *v,
2488 const union mf_subvalue *m,
2489 struct match *match)
2491 union mf_value value, mask;
2493 mf_get(field, match, &value, &mask);
2494 bitwise_copy(v, sizeof *v, 0, &value, field->n_bytes, 0, field->n_bits);
2495 bitwise_copy(m, sizeof *m, 0, &mask, field->n_bytes, 0, field->n_bits);
2496 mf_set(field, &value, &mask, match, NULL);
2499 /* Initializes 'x' to the value of 'sf' within 'flow'. 'sf' must be valid for
2500 * reading 'flow', e.g. as checked by mf_check_src(). */
2502 mf_read_subfield(const struct mf_subfield *sf, const struct flow *flow,
2503 union mf_subvalue *x)
2505 union mf_value value;
2507 mf_get_value(sf->field, flow, &value);
2509 memset(x, 0, sizeof *x);
2510 bitwise_copy(&value, sf->field->n_bytes, sf->ofs,
2515 /* Returns the value of 'sf' within 'flow'. 'sf' must be valid for reading
2516 * 'flow', e.g. as checked by mf_check_src() and sf->n_bits must be 64 or
2519 mf_get_subfield(const struct mf_subfield *sf, const struct flow *flow)
2521 union mf_value value;
2523 mf_get_value(sf->field, flow, &value);
2524 return bitwise_get(&value, sf->field->n_bytes, sf->ofs, sf->n_bits);
2528 mf_format_subvalue(const union mf_subvalue *subvalue, struct ds *s)
2530 ds_put_hex(s, subvalue->u8, sizeof subvalue->u8);
2534 field_array_set(enum mf_field_id id, const union mf_value *value,
2535 struct field_array *fa)
2537 ovs_assert(id < MFF_N_IDS);
2538 bitmap_set1(fa->used.bm, id);
2539 fa->value[id] = *value;