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));
275 return !(wc->masks.mpls_lse[0] & htonl(MPLS_TTL_MASK));
278 return !wc->masks.nw_src;
280 return !wc->masks.nw_dst;
283 return ipv6_mask_is_any(&wc->masks.ipv6_src);
285 return ipv6_mask_is_any(&wc->masks.ipv6_dst);
288 return !wc->masks.ipv6_label;
291 return !wc->masks.nw_proto;
293 case MFF_IP_DSCP_SHIFTED:
294 return !(wc->masks.nw_tos & IP_DSCP_MASK);
296 return !(wc->masks.nw_tos & IP_ECN_MASK);
298 return !wc->masks.nw_ttl;
301 return ipv6_mask_is_any(&wc->masks.nd_target);
304 return !(wc->masks.nw_frag & FLOW_NW_FRAG_MASK);
307 return !wc->masks.nw_proto;
309 return !wc->masks.nw_src;
311 return !wc->masks.nw_dst;
316 case MFF_ICMPV4_TYPE:
317 case MFF_ICMPV6_TYPE:
318 return !wc->masks.tp_src;
322 case MFF_ICMPV4_CODE:
323 case MFF_ICMPV6_CODE:
324 return !wc->masks.tp_dst;
326 return !wc->masks.tcp_flags;
334 /* Initializes 'mask' with the wildcard bit pattern for field 'mf' within 'wc'.
335 * Each bit in 'mask' will be set to 1 if the bit is significant for matching
336 * purposes, or to 0 if it is wildcarded.
338 * The caller is responsible for ensuring that 'wc' corresponds to a flow that
339 * meets 'mf''s prerequisites. */
341 mf_get_mask(const struct mf_field *mf, const struct flow_wildcards *wc,
342 union mf_value *mask)
344 mf_get_value(mf, &wc->masks, mask);
347 /* Tests whether 'mask' is a valid wildcard bit pattern for 'mf'. Returns true
348 * if the mask is valid, false otherwise. */
350 mf_is_mask_valid(const struct mf_field *mf, const union mf_value *mask)
352 switch (mf->maskable) {
354 return (is_all_zeros(mask, mf->n_bytes) ||
355 is_all_ones(mask, mf->n_bytes));
364 /* Returns true if 'flow' meets the prerequisites for 'mf', false otherwise. */
366 mf_are_prereqs_ok(const struct mf_field *mf, const struct flow *flow)
368 switch (mf->prereqs) {
373 return (flow->dl_type == htons(ETH_TYPE_ARP) ||
374 flow->dl_type == htons(ETH_TYPE_RARP));
376 return flow->dl_type == htons(ETH_TYPE_IP);
378 return flow->dl_type == htons(ETH_TYPE_IPV6);
380 return (flow->vlan_tci & htons(VLAN_CFI)) != 0;
382 return eth_type_mpls(flow->dl_type);
384 return is_ip_any(flow);
387 return is_ip_any(flow) && flow->nw_proto == IPPROTO_TCP
388 && !(flow->nw_frag & FLOW_NW_FRAG_LATER);
390 return is_ip_any(flow) && flow->nw_proto == IPPROTO_UDP
391 && !(flow->nw_frag & FLOW_NW_FRAG_LATER);
393 return is_ip_any(flow) && flow->nw_proto == IPPROTO_SCTP
394 && !(flow->nw_frag & FLOW_NW_FRAG_LATER);
396 return is_icmpv4(flow);
398 return is_icmpv6(flow);
401 return (is_icmpv6(flow)
402 && flow->tp_dst == htons(0)
403 && (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT) ||
404 flow->tp_src == htons(ND_NEIGHBOR_ADVERT)));
406 return (is_icmpv6(flow)
407 && flow->tp_dst == htons(0)
408 && (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT)));
410 return (is_icmpv6(flow)
411 && flow->tp_dst == htons(0)
412 && (flow->tp_src == htons(ND_NEIGHBOR_ADVERT)));
418 /* Set field and it's prerequisities in the mask.
419 * This is only ever called for writeable 'mf's, but we do not make the
420 * distinction here. */
422 mf_mask_field_and_prereqs(const struct mf_field *mf, struct flow_wildcards *wc)
424 mf_set_flow_value(mf, &exact_match_mask, &wc->masks);
426 switch (mf->prereqs) {
430 WC_MASK_FIELD(wc, tp_src);
431 WC_MASK_FIELD(wc, tp_dst);
438 /* nw_frag always unwildcarded. */
439 WC_MASK_FIELD(wc, nw_proto);
446 /* dl_type always unwildcarded. */
449 WC_MASK_FIELD_MASK(wc, vlan_tci, htons(VLAN_CFI));
456 /* Set bits of 'bm' corresponding to the field 'mf' and it's prerequisities. */
458 mf_bitmap_set_field_and_prereqs(const struct mf_field *mf, struct mf_bitmap *bm)
460 bitmap_set1(bm->bm, mf->id);
462 switch (mf->prereqs) {
466 bitmap_set1(bm->bm, MFF_TCP_SRC);
467 bitmap_set1(bm->bm, MFF_TCP_DST);
474 /* nw_frag always unwildcarded. */
475 bitmap_set1(bm->bm, MFF_IP_PROTO);
482 bitmap_set1(bm->bm, MFF_ETH_TYPE);
485 bitmap_set1(bm->bm, MFF_VLAN_TCI);
492 /* Returns true if 'value' may be a valid value *as part of a masked match*,
495 * A value is not rejected just because it is not valid for the field in
496 * question, but only if it doesn't make sense to test the bits in question at
497 * all. For example, the MFF_VLAN_TCI field will never have a nonzero value
498 * without the VLAN_CFI bit being set, but we can't reject those values because
499 * it is still legitimate to test just for those bits (see the documentation
500 * for NXM_OF_VLAN_TCI in nicira-ext.h). On the other hand, there is never a
501 * reason to set the low bit of MFF_IP_DSCP to 1, so we reject that. */
503 mf_is_value_valid(const struct mf_field *mf, const union mf_value *value)
512 case MFF_TUN_IPV6_SRC:
513 case MFF_TUN_IPV6_DST:
517 case MFF_TUN_GBP_FLAGS:
518 CASE_MFF_TUN_METADATA:
521 case MFF_SKB_PRIORITY:
549 case MFF_ICMPV4_TYPE:
550 case MFF_ICMPV4_CODE:
551 case MFF_ICMPV6_TYPE:
552 case MFF_ICMPV6_CODE:
558 case MFF_IN_PORT_OXM:
559 case MFF_ACTSET_OUTPUT: {
561 return !ofputil_port_from_ofp11(value->be32, &port);
565 return !(value->u8 & ~IP_DSCP_MASK);
566 case MFF_IP_DSCP_SHIFTED:
567 return !(value->u8 & (~IP_DSCP_MASK >> 2));
569 return !(value->u8 & ~IP_ECN_MASK);
571 return !(value->u8 & ~FLOW_NW_FRAG_MASK);
573 return !(value->be16 & ~htons(0x0fff));
576 return !(value->be16 & htons(0xff00));
579 return !(value->be16 & htons(VLAN_CFI | VLAN_PCP_MASK));
581 return !(value->be16 & htons(VLAN_PCP_MASK));
583 case MFF_DL_VLAN_PCP:
585 return !(value->u8 & ~(VLAN_PCP_MASK >> VLAN_PCP_SHIFT));
588 return !(value->be32 & ~htonl(IPV6_LABEL_MASK));
591 return !(value->be32 & ~htonl(MPLS_LABEL_MASK >> MPLS_LABEL_SHIFT));
594 return !(value->u8 & ~(MPLS_TC_MASK >> MPLS_TC_SHIFT));
597 return !(value->u8 & ~(MPLS_BOS_MASK >> MPLS_BOS_SHIFT));
600 return !(value->be16 & ~htons(FLOW_TNL_PUB_F_MASK));
603 return !(value->be32 & ~htonl(CS_SUPPORTED_MASK));
611 /* Copies the value of field 'mf' from 'flow' into 'value'. The caller is
612 * responsible for ensuring that 'flow' meets 'mf''s prerequisites. */
614 mf_get_value(const struct mf_field *mf, const struct flow *flow,
615 union mf_value *value)
619 value->be32 = htonl(flow->dp_hash);
622 value->be32 = htonl(flow->recirc_id);
625 value->be32 = htonl(flow->conj_id);
628 value->be64 = flow->tunnel.tun_id;
631 value->be32 = flow->tunnel.ip_src;
634 value->be32 = flow->tunnel.ip_dst;
636 case MFF_TUN_IPV6_SRC:
637 value->ipv6 = flow->tunnel.ipv6_src;
639 case MFF_TUN_IPV6_DST:
640 value->ipv6 = flow->tunnel.ipv6_dst;
643 value->be16 = htons(flow->tunnel.flags & FLOW_TNL_PUB_F_MASK);
646 value->be16 = flow->tunnel.gbp_id;
648 case MFF_TUN_GBP_FLAGS:
649 value->u8 = flow->tunnel.gbp_flags;
652 value->u8 = flow->tunnel.ip_ttl;
655 value->u8 = flow->tunnel.ip_tos;
657 CASE_MFF_TUN_METADATA:
658 tun_metadata_read(&flow->tunnel, mf, value);
662 value->be64 = flow->metadata;
666 value->be16 = htons(ofp_to_u16(flow->in_port.ofp_port));
668 case MFF_IN_PORT_OXM:
669 value->be32 = ofputil_port_to_ofp11(flow->in_port.ofp_port);
671 case MFF_ACTSET_OUTPUT:
672 value->be32 = ofputil_port_to_ofp11(flow->actset_output);
675 case MFF_SKB_PRIORITY:
676 value->be32 = htonl(flow->skb_priority);
680 value->be32 = htonl(flow->pkt_mark);
684 value->be32 = htonl(flow->ct_state);
688 value->be16 = htons(flow->ct_zone);
692 value->be32 = htonl(flow->ct_mark);
696 value->be128 = hton128(flow->ct_label);
700 value->be32 = htonl(flow->regs[mf->id - MFF_REG0]);
704 value->be64 = htonll(flow_get_xreg(flow, mf->id - MFF_XREG0));
708 value->mac = flow->dl_src;
712 value->mac = flow->dl_dst;
716 value->be16 = flow->dl_type;
720 value->be16 = flow->vlan_tci;
724 value->be16 = flow->vlan_tci & htons(VLAN_VID_MASK);
727 value->be16 = flow->vlan_tci & htons(VLAN_VID_MASK | VLAN_CFI);
730 case MFF_DL_VLAN_PCP:
732 value->u8 = vlan_tci_to_pcp(flow->vlan_tci);
736 value->be32 = htonl(mpls_lse_to_label(flow->mpls_lse[0]));
740 value->u8 = mpls_lse_to_tc(flow->mpls_lse[0]);
744 value->u8 = mpls_lse_to_bos(flow->mpls_lse[0]);
748 value->u8 = mpls_lse_to_ttl(flow->mpls_lse[0]);
752 value->be32 = flow->nw_src;
756 value->be32 = flow->nw_dst;
760 value->ipv6 = flow->ipv6_src;
764 value->ipv6 = flow->ipv6_dst;
768 value->be32 = flow->ipv6_label;
772 value->u8 = flow->nw_proto;
776 value->u8 = flow->nw_tos & IP_DSCP_MASK;
779 case MFF_IP_DSCP_SHIFTED:
780 value->u8 = flow->nw_tos >> 2;
784 value->u8 = flow->nw_tos & IP_ECN_MASK;
788 value->u8 = flow->nw_ttl;
792 value->u8 = flow->nw_frag;
796 value->be16 = htons(flow->nw_proto);
800 value->be32 = flow->nw_src;
804 value->be32 = flow->nw_dst;
809 value->mac = flow->arp_sha;
814 value->mac = flow->arp_tha;
820 value->be16 = flow->tp_src;
826 value->be16 = flow->tp_dst;
830 value->be16 = flow->tcp_flags;
833 case MFF_ICMPV4_TYPE:
834 case MFF_ICMPV6_TYPE:
835 value->u8 = ntohs(flow->tp_src);
838 case MFF_ICMPV4_CODE:
839 case MFF_ICMPV6_CODE:
840 value->u8 = ntohs(flow->tp_dst);
844 value->ipv6 = flow->nd_target;
853 /* Makes 'match' match field 'mf' exactly, with the value matched taken from
854 * 'value'. The caller is responsible for ensuring that 'match' meets 'mf''s
857 * If non-NULL, 'err_str' returns a malloc'ed string describing any errors
858 * with the request or NULL if there is no error. The caller is reponsible
859 * for freeing the string. */
861 mf_set_value(const struct mf_field *mf,
862 const union mf_value *value, struct match *match, char **err_str)
870 match_set_dp_hash(match, ntohl(value->be32));
873 match_set_recirc_id(match, ntohl(value->be32));
876 match_set_conj_id(match, ntohl(value->be32));
879 match_set_tun_id(match, value->be64);
882 match_set_tun_src(match, value->be32);
885 match_set_tun_dst(match, value->be32);
887 case MFF_TUN_IPV6_SRC:
888 match_set_tun_ipv6_src(match, &value->ipv6);
890 case MFF_TUN_IPV6_DST:
891 match_set_tun_ipv6_dst(match, &value->ipv6);
894 match_set_tun_flags(match, ntohs(value->be16));
897 match_set_tun_gbp_id(match, value->be16);
899 case MFF_TUN_GBP_FLAGS:
900 match_set_tun_gbp_flags(match, value->u8);
903 match_set_tun_tos(match, value->u8);
906 match_set_tun_ttl(match, value->u8);
908 CASE_MFF_TUN_METADATA:
909 tun_metadata_set_match(mf, value, NULL, match, err_str);
913 match_set_metadata(match, value->be64);
917 match_set_in_port(match, u16_to_ofp(ntohs(value->be16)));
920 case MFF_IN_PORT_OXM: {
922 ofputil_port_from_ofp11(value->be32, &port);
923 match_set_in_port(match, port);
926 case MFF_ACTSET_OUTPUT: {
928 ofputil_port_from_ofp11(value->be32, &port);
929 match_set_actset_output(match, port);
933 case MFF_SKB_PRIORITY:
934 match_set_skb_priority(match, ntohl(value->be32));
938 match_set_pkt_mark(match, ntohl(value->be32));
942 match_set_ct_state(match, ntohl(value->be32));
946 match_set_ct_zone(match, ntohs(value->be16));
950 match_set_ct_mark(match, ntohl(value->be32));
954 match_set_ct_label(match, ntoh128(value->be128));
958 match_set_reg(match, mf->id - MFF_REG0, ntohl(value->be32));
962 match_set_xreg(match, mf->id - MFF_XREG0, ntohll(value->be64));
966 match_set_dl_src(match, value->mac);
970 match_set_dl_dst(match, value->mac);
974 match_set_dl_type(match, value->be16);
978 match_set_dl_tci(match, value->be16);
982 match_set_dl_vlan(match, value->be16);
985 match_set_vlan_vid(match, value->be16);
988 case MFF_DL_VLAN_PCP:
990 match_set_dl_vlan_pcp(match, value->u8);
994 match_set_mpls_label(match, 0, value->be32);
998 match_set_mpls_tc(match, 0, value->u8);
1002 match_set_mpls_bos(match, 0, value->u8);
1006 match_set_mpls_ttl(match, 0, value->u8);
1010 match_set_nw_src(match, value->be32);
1014 match_set_nw_dst(match, value->be32);
1018 match_set_ipv6_src(match, &value->ipv6);
1022 match_set_ipv6_dst(match, &value->ipv6);
1025 case MFF_IPV6_LABEL:
1026 match_set_ipv6_label(match, value->be32);
1030 match_set_nw_proto(match, value->u8);
1034 match_set_nw_dscp(match, value->u8);
1037 case MFF_IP_DSCP_SHIFTED:
1038 match_set_nw_dscp(match, value->u8 << 2);
1042 match_set_nw_ecn(match, value->u8);
1046 match_set_nw_ttl(match, value->u8);
1050 match_set_nw_frag(match, value->u8);
1054 match_set_nw_proto(match, ntohs(value->be16));
1058 match_set_nw_src(match, value->be32);
1062 match_set_nw_dst(match, value->be32);
1067 match_set_arp_sha(match, value->mac);
1072 match_set_arp_tha(match, value->mac);
1078 match_set_tp_src(match, value->be16);
1084 match_set_tp_dst(match, value->be16);
1088 match_set_tcp_flags(match, value->be16);
1091 case MFF_ICMPV4_TYPE:
1092 case MFF_ICMPV6_TYPE:
1093 match_set_icmp_type(match, value->u8);
1096 case MFF_ICMPV4_CODE:
1097 case MFF_ICMPV6_CODE:
1098 match_set_icmp_code(match, value->u8);
1102 match_set_nd_target(match, &value->ipv6);
1111 /* Unwildcard 'mask' member field described by 'mf'. The caller is
1112 * responsible for ensuring that 'mask' meets 'mf''s prerequisites. */
1114 mf_mask_field(const struct mf_field *mf, struct flow *mask)
1116 /* For MFF_DL_VLAN, we cannot send a all 1's to flow_set_dl_vlan()
1117 * as that will be considered as OFP10_VLAN_NONE. So consider it as a
1118 * special case. For the rest, calling mf_set_flow_value() is good
1120 if (mf->id == MFF_DL_VLAN) {
1121 flow_set_dl_vlan(mask, htons(VLAN_VID_MASK));
1123 mf_set_flow_value(mf, &exact_match_mask, mask);
1128 field_len(const struct mf_field *mf, const union mf_value *value_)
1130 const uint8_t *value = &value_->u8;
1133 if (!mf->variable_len) {
1141 for (i = 0; i < mf->n_bytes; i++) {
1142 if (value[i] != 0) {
1147 return mf->n_bytes - i;
1150 /* Returns the effective length of the field. For fixed length fields,
1151 * this is just the defined length. For variable length fields, it is
1152 * the minimum size encoding that retains the same meaning (i.e.
1153 * discarding leading zeros).
1155 * 'is_masked' returns (if non-NULL) whether the original contained
1156 * a mask. Otherwise, a mask that is the same length as the value
1157 * might be misinterpreted as an exact match. */
1159 mf_field_len(const struct mf_field *mf, const union mf_value *value,
1160 const union mf_value *mask, bool *is_masked_)
1163 bool is_masked = mask && !is_all_ones(mask, mf->n_bytes);
1165 len = field_len(mf, value);
1167 mask_len = field_len(mf, mask);
1168 len = MAX(len, mask_len);
1172 *is_masked_ = is_masked;
1178 /* Sets 'flow' member field described by 'mf' to 'value'. The caller is
1179 * responsible for ensuring that 'flow' meets 'mf''s prerequisites.*/
1181 mf_set_flow_value(const struct mf_field *mf,
1182 const union mf_value *value, struct flow *flow)
1186 flow->dp_hash = ntohl(value->be32);
1189 flow->recirc_id = ntohl(value->be32);
1192 flow->conj_id = ntohl(value->be32);
1195 flow->tunnel.tun_id = value->be64;
1198 flow->tunnel.ip_src = value->be32;
1201 flow->tunnel.ip_dst = value->be32;
1203 case MFF_TUN_IPV6_SRC:
1204 flow->tunnel.ipv6_src = value->ipv6;
1206 case MFF_TUN_IPV6_DST:
1207 flow->tunnel.ipv6_dst = value->ipv6;
1210 flow->tunnel.flags = (flow->tunnel.flags & ~FLOW_TNL_PUB_F_MASK) |
1213 case MFF_TUN_GBP_ID:
1214 flow->tunnel.gbp_id = value->be16;
1216 case MFF_TUN_GBP_FLAGS:
1217 flow->tunnel.gbp_flags = value->u8;
1220 flow->tunnel.ip_tos = value->u8;
1223 flow->tunnel.ip_ttl = value->u8;
1225 CASE_MFF_TUN_METADATA:
1226 tun_metadata_write(&flow->tunnel, mf, value);
1229 flow->metadata = value->be64;
1233 flow->in_port.ofp_port = u16_to_ofp(ntohs(value->be16));
1236 case MFF_IN_PORT_OXM:
1237 ofputil_port_from_ofp11(value->be32, &flow->in_port.ofp_port);
1239 case MFF_ACTSET_OUTPUT:
1240 ofputil_port_from_ofp11(value->be32, &flow->actset_output);
1243 case MFF_SKB_PRIORITY:
1244 flow->skb_priority = ntohl(value->be32);
1248 flow->pkt_mark = ntohl(value->be32);
1252 flow->ct_state = ntohl(value->be32);
1256 flow->ct_zone = ntohs(value->be16);
1260 flow->ct_mark = ntohl(value->be32);
1264 flow->ct_label = ntoh128(value->be128);
1268 flow->regs[mf->id - MFF_REG0] = ntohl(value->be32);
1272 flow_set_xreg(flow, mf->id - MFF_XREG0, ntohll(value->be64));
1276 flow->dl_src = value->mac;
1280 flow->dl_dst = value->mac;
1284 flow->dl_type = value->be16;
1288 flow->vlan_tci = value->be16;
1292 flow_set_dl_vlan(flow, value->be16);
1295 flow_set_vlan_vid(flow, value->be16);
1298 case MFF_DL_VLAN_PCP:
1300 flow_set_vlan_pcp(flow, value->u8);
1303 case MFF_MPLS_LABEL:
1304 flow_set_mpls_label(flow, 0, value->be32);
1308 flow_set_mpls_tc(flow, 0, value->u8);
1312 flow_set_mpls_bos(flow, 0, value->u8);
1316 flow_set_mpls_ttl(flow, 0, value->u8);
1320 flow->nw_src = value->be32;
1324 flow->nw_dst = value->be32;
1328 flow->ipv6_src = value->ipv6;
1332 flow->ipv6_dst = value->ipv6;
1335 case MFF_IPV6_LABEL:
1336 flow->ipv6_label = value->be32 & htonl(IPV6_LABEL_MASK);
1340 flow->nw_proto = value->u8;
1344 flow->nw_tos &= ~IP_DSCP_MASK;
1345 flow->nw_tos |= value->u8 & IP_DSCP_MASK;
1348 case MFF_IP_DSCP_SHIFTED:
1349 flow->nw_tos &= ~IP_DSCP_MASK;
1350 flow->nw_tos |= value->u8 << 2;
1354 flow->nw_tos &= ~IP_ECN_MASK;
1355 flow->nw_tos |= value->u8 & IP_ECN_MASK;
1359 flow->nw_ttl = value->u8;
1363 flow->nw_frag = value->u8 & FLOW_NW_FRAG_MASK;
1367 flow->nw_proto = ntohs(value->be16);
1371 flow->nw_src = value->be32;
1375 flow->nw_dst = value->be32;
1380 flow->arp_sha = value->mac;
1385 flow->arp_tha = value->mac;
1391 flow->tp_src = value->be16;
1397 flow->tp_dst = value->be16;
1401 flow->tcp_flags = value->be16;
1404 case MFF_ICMPV4_TYPE:
1405 case MFF_ICMPV6_TYPE:
1406 flow->tp_src = htons(value->u8);
1409 case MFF_ICMPV4_CODE:
1410 case MFF_ICMPV6_CODE:
1411 flow->tp_dst = htons(value->u8);
1415 flow->nd_target = value->ipv6;
1424 /* Consider each of 'src', 'mask', and 'dst' as if they were arrays of 8*n
1425 * bits. Then, for each 0 <= i < 8 * n such that mask[i] == 1, sets dst[i] =
1428 apply_mask(const uint8_t *src, const uint8_t *mask, uint8_t *dst, size_t n)
1432 for (i = 0; i < n; i++) {
1433 dst[i] = (src[i] & mask[i]) | (dst[i] & ~mask[i]);
1437 /* Sets 'flow' member field described by 'field' to 'value', except that bits
1438 * for which 'mask' has a 0-bit keep their existing values. The caller is
1439 * responsible for ensuring that 'flow' meets 'field''s prerequisites.*/
1441 mf_set_flow_value_masked(const struct mf_field *field,
1442 const union mf_value *value,
1443 const union mf_value *mask,
1448 mf_get_value(field, flow, &tmp);
1449 apply_mask((const uint8_t *) value, (const uint8_t *) mask,
1450 (uint8_t *) &tmp, field->n_bytes);
1451 mf_set_flow_value(field, &tmp, flow);
1455 mf_is_tun_metadata(const struct mf_field *mf)
1457 return mf->id >= MFF_TUN_METADATA0 &&
1458 mf->id < MFF_TUN_METADATA0 + TUN_METADATA_NUM_OPTS;
1461 /* Returns true if 'mf' has previously been set in 'flow', false if
1462 * it contains a non-default value.
1464 * The caller is responsible for ensuring that 'flow' meets 'mf''s
1467 mf_is_set(const struct mf_field *mf, const struct flow *flow)
1469 if (!mf_is_tun_metadata(mf)) {
1470 union mf_value value;
1472 mf_get_value(mf, flow, &value);
1473 return !is_all_zeros(&value, mf->n_bytes);
1475 return ULLONG_GET(flow->tunnel.metadata.present.map,
1476 mf->id - MFF_TUN_METADATA0);
1480 /* Makes 'match' wildcard field 'mf'.
1482 * The caller is responsible for ensuring that 'match' meets 'mf''s
1485 * If non-NULL, 'err_str' returns a malloc'ed string describing any errors
1486 * with the request or NULL if there is no error. The caller is reponsible
1487 * for freeing the string. */
1489 mf_set_wild(const struct mf_field *mf, struct match *match, char **err_str)
1497 match->flow.dp_hash = 0;
1498 match->wc.masks.dp_hash = 0;
1501 match->flow.recirc_id = 0;
1502 match->wc.masks.recirc_id = 0;
1505 match->flow.conj_id = 0;
1506 match->wc.masks.conj_id = 0;
1509 match_set_tun_id_masked(match, htonll(0), htonll(0));
1512 match_set_tun_src_masked(match, htonl(0), htonl(0));
1515 match_set_tun_dst_masked(match, htonl(0), htonl(0));
1517 case MFF_TUN_IPV6_SRC:
1518 memset(&match->wc.masks.tunnel.ipv6_src, 0,
1519 sizeof match->wc.masks.tunnel.ipv6_src);
1520 memset(&match->flow.tunnel.ipv6_src, 0,
1521 sizeof match->flow.tunnel.ipv6_src);
1523 case MFF_TUN_IPV6_DST:
1524 memset(&match->wc.masks.tunnel.ipv6_dst, 0,
1525 sizeof match->wc.masks.tunnel.ipv6_dst);
1526 memset(&match->flow.tunnel.ipv6_dst, 0,
1527 sizeof match->flow.tunnel.ipv6_dst);
1530 match_set_tun_flags_masked(match, 0, 0);
1532 case MFF_TUN_GBP_ID:
1533 match_set_tun_gbp_id_masked(match, 0, 0);
1535 case MFF_TUN_GBP_FLAGS:
1536 match_set_tun_gbp_flags_masked(match, 0, 0);
1539 match_set_tun_tos_masked(match, 0, 0);
1542 match_set_tun_ttl_masked(match, 0, 0);
1544 CASE_MFF_TUN_METADATA:
1545 tun_metadata_set_match(mf, NULL, NULL, match, err_str);
1549 match_set_metadata_masked(match, htonll(0), htonll(0));
1553 case MFF_IN_PORT_OXM:
1554 match->flow.in_port.ofp_port = 0;
1555 match->wc.masks.in_port.ofp_port = 0;
1557 case MFF_ACTSET_OUTPUT:
1558 match->flow.actset_output = 0;
1559 match->wc.masks.actset_output = 0;
1562 case MFF_SKB_PRIORITY:
1563 match->flow.skb_priority = 0;
1564 match->wc.masks.skb_priority = 0;
1568 match->flow.pkt_mark = 0;
1569 match->wc.masks.pkt_mark = 0;
1573 match->flow.ct_state = 0;
1574 match->wc.masks.ct_state = 0;
1578 match->flow.ct_zone = 0;
1579 match->wc.masks.ct_zone = 0;
1583 match->flow.ct_mark = 0;
1584 match->wc.masks.ct_mark = 0;
1588 memset(&match->flow.ct_label, 0, sizeof(match->flow.ct_label));
1589 memset(&match->wc.masks.ct_label, 0, sizeof(match->wc.masks.ct_label));
1593 match_set_reg_masked(match, mf->id - MFF_REG0, 0, 0);
1597 match_set_xreg_masked(match, mf->id - MFF_XREG0, 0, 0);
1601 match->flow.dl_src = eth_addr_zero;
1602 match->wc.masks.dl_src = eth_addr_zero;
1606 match->flow.dl_dst = eth_addr_zero;
1607 match->wc.masks.dl_dst = eth_addr_zero;
1611 match->flow.dl_type = htons(0);
1612 match->wc.masks.dl_type = htons(0);
1616 match_set_dl_tci_masked(match, htons(0), htons(0));
1621 match_set_any_vid(match);
1624 case MFF_DL_VLAN_PCP:
1626 match_set_any_pcp(match);
1629 case MFF_MPLS_LABEL:
1630 match_set_any_mpls_label(match, 0);
1634 match_set_any_mpls_tc(match, 0);
1638 match_set_any_mpls_bos(match, 0);
1642 match_set_any_mpls_ttl(match, 0);
1647 match_set_nw_src_masked(match, htonl(0), htonl(0));
1652 match_set_nw_dst_masked(match, htonl(0), htonl(0));
1656 memset(&match->wc.masks.ipv6_src, 0, sizeof match->wc.masks.ipv6_src);
1657 memset(&match->flow.ipv6_src, 0, sizeof match->flow.ipv6_src);
1661 memset(&match->wc.masks.ipv6_dst, 0, sizeof match->wc.masks.ipv6_dst);
1662 memset(&match->flow.ipv6_dst, 0, sizeof match->flow.ipv6_dst);
1665 case MFF_IPV6_LABEL:
1666 match->wc.masks.ipv6_label = htonl(0);
1667 match->flow.ipv6_label = htonl(0);
1671 match->wc.masks.nw_proto = 0;
1672 match->flow.nw_proto = 0;
1676 case MFF_IP_DSCP_SHIFTED:
1677 match->wc.masks.nw_tos &= ~IP_DSCP_MASK;
1678 match->flow.nw_tos &= ~IP_DSCP_MASK;
1682 match->wc.masks.nw_tos &= ~IP_ECN_MASK;
1683 match->flow.nw_tos &= ~IP_ECN_MASK;
1687 match->wc.masks.nw_ttl = 0;
1688 match->flow.nw_ttl = 0;
1692 match->wc.masks.nw_frag &= ~FLOW_NW_FRAG_MASK;
1693 match->flow.nw_frag &= ~FLOW_NW_FRAG_MASK;
1697 match->wc.masks.nw_proto = 0;
1698 match->flow.nw_proto = 0;
1703 match->flow.arp_sha = eth_addr_zero;
1704 match->wc.masks.arp_sha = eth_addr_zero;
1709 match->flow.arp_tha = eth_addr_zero;
1710 match->wc.masks.arp_tha = eth_addr_zero;
1716 case MFF_ICMPV4_TYPE:
1717 case MFF_ICMPV6_TYPE:
1718 match->wc.masks.tp_src = htons(0);
1719 match->flow.tp_src = htons(0);
1725 case MFF_ICMPV4_CODE:
1726 case MFF_ICMPV6_CODE:
1727 match->wc.masks.tp_dst = htons(0);
1728 match->flow.tp_dst = htons(0);
1732 match->wc.masks.tcp_flags = htons(0);
1733 match->flow.tcp_flags = htons(0);
1737 memset(&match->wc.masks.nd_target, 0,
1738 sizeof match->wc.masks.nd_target);
1739 memset(&match->flow.nd_target, 0, sizeof match->flow.nd_target);
1748 /* Makes 'match' match field 'mf' with the specified 'value' and 'mask'.
1749 * 'value' specifies a value to match and 'mask' specifies a wildcard pattern,
1750 * with a 1-bit indicating that the corresponding value bit must match and a
1751 * 0-bit indicating a don't-care.
1753 * If 'mask' is NULL or points to all-1-bits, then this call is equivalent to
1754 * mf_set_value(mf, value, match). If 'mask' points to all-0-bits, then this
1755 * call is equivalent to mf_set_wild(mf, match).
1757 * 'mask' must be a valid mask for 'mf' (see mf_is_mask_valid()). The caller
1758 * is responsible for ensuring that 'match' meets 'mf''s prerequisites.
1760 * If non-NULL, 'err_str' returns a malloc'ed string describing any errors
1761 * with the request or NULL if there is no error. The caller is reponsible
1762 * for freeing the string.
1764 * Return a set of enum ofputil_protocol bits (as an uint32_t to avoid circular
1765 * dependency on enum ofputil_protocol definition) indicating which OpenFlow
1766 * protocol versions can support this functionality. */
1768 mf_set(const struct mf_field *mf,
1769 const union mf_value *value, const union mf_value *mask,
1770 struct match *match, char **err_str)
1772 if (!mask || is_all_ones(mask, mf->n_bytes)) {
1773 mf_set_value(mf, value, match, err_str);
1774 return mf->usable_protocols_exact;
1775 } else if (is_all_zeros(mask, mf->n_bytes) && !mf_is_tun_metadata(mf)) {
1776 /* Tunnel metadata matches on the existence of the field itself, so
1777 * it still needs to be encoded even if the value is wildcarded. */
1778 mf_set_wild(mf, match, err_str);
1779 return OFPUTIL_P_ANY;
1791 case MFF_IN_PORT_OXM:
1792 case MFF_ACTSET_OUTPUT:
1793 case MFF_SKB_PRIORITY:
1796 case MFF_DL_VLAN_PCP:
1798 case MFF_MPLS_LABEL:
1805 case MFF_IP_DSCP_SHIFTED:
1808 case MFF_ICMPV4_TYPE:
1809 case MFF_ICMPV4_CODE:
1810 case MFF_ICMPV6_TYPE:
1811 case MFF_ICMPV6_CODE:
1812 return OFPUTIL_P_NONE;
1815 match_set_dp_hash_masked(match, ntohl(value->be32), ntohl(mask->be32));
1818 match_set_tun_id_masked(match, value->be64, mask->be64);
1821 match_set_tun_src_masked(match, value->be32, mask->be32);
1824 match_set_tun_dst_masked(match, value->be32, mask->be32);
1826 case MFF_TUN_IPV6_SRC:
1827 match_set_tun_ipv6_src_masked(match, &value->ipv6, &mask->ipv6);
1829 case MFF_TUN_IPV6_DST:
1830 match_set_tun_ipv6_dst_masked(match, &value->ipv6, &mask->ipv6);
1833 match_set_tun_flags_masked(match, ntohs(value->be16), ntohs(mask->be16));
1835 case MFF_TUN_GBP_ID:
1836 match_set_tun_gbp_id_masked(match, value->be16, mask->be16);
1838 case MFF_TUN_GBP_FLAGS:
1839 match_set_tun_gbp_flags_masked(match, value->u8, mask->u8);
1842 match_set_tun_ttl_masked(match, value->u8, mask->u8);
1845 match_set_tun_tos_masked(match, value->u8, mask->u8);
1847 CASE_MFF_TUN_METADATA:
1848 tun_metadata_set_match(mf, value, mask, match, err_str);
1852 match_set_metadata_masked(match, value->be64, mask->be64);
1856 match_set_reg_masked(match, mf->id - MFF_REG0,
1857 ntohl(value->be32), ntohl(mask->be32));
1861 match_set_xreg_masked(match, mf->id - MFF_XREG0,
1862 ntohll(value->be64), ntohll(mask->be64));
1866 match_set_pkt_mark_masked(match, ntohl(value->be32),
1871 match_set_ct_state_masked(match, ntohl(value->be32), ntohl(mask->be32));
1875 match_set_ct_mark_masked(match, ntohl(value->be32), ntohl(mask->be32));
1879 match_set_ct_label_masked(match, ntoh128(value->be128),
1880 mask ? ntoh128(mask->be128) : OVS_U128_MAX);
1884 match_set_dl_dst_masked(match, value->mac, mask->mac);
1888 match_set_dl_src_masked(match, value->mac, mask->mac);
1893 match_set_arp_sha_masked(match, value->mac, mask->mac);
1898 match_set_arp_tha_masked(match, value->mac, mask->mac);
1902 match_set_dl_tci_masked(match, value->be16, mask->be16);
1906 match_set_vlan_vid_masked(match, value->be16, mask->be16);
1910 match_set_nw_src_masked(match, value->be32, mask->be32);
1914 match_set_nw_dst_masked(match, value->be32, mask->be32);
1918 match_set_ipv6_src_masked(match, &value->ipv6, &mask->ipv6);
1922 match_set_ipv6_dst_masked(match, &value->ipv6, &mask->ipv6);
1925 case MFF_IPV6_LABEL:
1926 if ((mask->be32 & htonl(IPV6_LABEL_MASK)) == htonl(IPV6_LABEL_MASK)) {
1927 mf_set_value(mf, value, match, err_str);
1929 match_set_ipv6_label_masked(match, value->be32, mask->be32);
1934 match_set_nd_target_masked(match, &value->ipv6, &mask->ipv6);
1938 match_set_nw_frag_masked(match, value->u8, mask->u8);
1942 match_set_nw_src_masked(match, value->be32, mask->be32);
1946 match_set_nw_dst_masked(match, value->be32, mask->be32);
1952 match_set_tp_src_masked(match, value->be16, mask->be16);
1958 match_set_tp_dst_masked(match, value->be16, mask->be16);
1962 match_set_tcp_flags_masked(match, value->be16, mask->be16);
1970 return ((mf->usable_protocols_bitwise == mf->usable_protocols_cidr
1971 || ip_is_cidr(mask->be32))
1972 ? mf->usable_protocols_cidr
1973 : mf->usable_protocols_bitwise);
1977 mf_check__(const struct mf_subfield *sf, const struct flow *flow,
1981 VLOG_WARN_RL(&rl, "unknown %s field", type);
1982 return OFPERR_OFPBAC_BAD_SET_TYPE;
1983 } else if (!sf->n_bits) {
1984 VLOG_WARN_RL(&rl, "zero bit %s field %s", type, sf->field->name);
1985 return OFPERR_OFPBAC_BAD_SET_LEN;
1986 } else if (sf->ofs >= sf->field->n_bits) {
1987 VLOG_WARN_RL(&rl, "bit offset %d exceeds %d-bit width of %s field %s",
1988 sf->ofs, sf->field->n_bits, type, sf->field->name);
1989 return OFPERR_OFPBAC_BAD_SET_LEN;
1990 } else if (sf->ofs + sf->n_bits > sf->field->n_bits) {
1991 VLOG_WARN_RL(&rl, "bit offset %d and width %d exceeds %d-bit width "
1992 "of %s field %s", sf->ofs, sf->n_bits,
1993 sf->field->n_bits, type, sf->field->name);
1994 return OFPERR_OFPBAC_BAD_SET_LEN;
1995 } else if (flow && !mf_are_prereqs_ok(sf->field, flow)) {
1996 VLOG_WARN_RL(&rl, "%s field %s lacks correct prerequisites",
1997 type, sf->field->name);
1998 return OFPERR_OFPBAC_MATCH_INCONSISTENT;
2004 /* Checks whether 'sf' is valid for reading a subfield out of 'flow'. Returns
2005 * 0 if so, otherwise an OpenFlow error code (e.g. as returned by
2008 mf_check_src(const struct mf_subfield *sf, const struct flow *flow)
2010 return mf_check__(sf, flow, "source");
2013 /* Checks whether 'sf' is valid for writing a subfield into 'flow'. Returns 0
2014 * if so, otherwise an OpenFlow error code (e.g. as returned by
2017 mf_check_dst(const struct mf_subfield *sf, const struct flow *flow)
2019 int error = mf_check__(sf, flow, "destination");
2020 if (!error && !sf->field->writable) {
2021 VLOG_WARN_RL(&rl, "destination field %s is not writable",
2023 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
2028 /* Copies the value and wildcard bit pattern for 'mf' from 'match' into the
2029 * 'value' and 'mask', respectively. */
2031 mf_get(const struct mf_field *mf, const struct match *match,
2032 union mf_value *value, union mf_value *mask)
2034 mf_get_value(mf, &match->flow, value);
2035 mf_get_mask(mf, &match->wc, mask);
2039 mf_from_integer_string(const struct mf_field *mf, const char *s,
2040 uint8_t *valuep, uint8_t *maskp)
2043 const char *err_str = "";
2046 err = parse_int_string(s, valuep, mf->n_bytes, &tail);
2047 if (err || (*tail != '\0' && *tail != '/')) {
2053 err = parse_int_string(tail + 1, maskp, mf->n_bytes, &tail);
2054 if (err || *tail != '\0') {
2059 memset(maskp, 0xff, mf->n_bytes);
2065 if (err == ERANGE) {
2066 return xasprintf("%s: %s too large for %u-byte field %s",
2067 s, err_str, mf->n_bytes, mf->name);
2069 return xasprintf("%s: bad syntax for %s %s", s, mf->name, err_str);
2074 mf_from_ethernet_string(const struct mf_field *mf, const char *s,
2075 struct eth_addr *mac, struct eth_addr *mask)
2079 ovs_assert(mf->n_bytes == ETH_ADDR_LEN);
2082 if (ovs_scan(s, ETH_ADDR_SCAN_FMT"%n", ETH_ADDR_SCAN_ARGS(*mac), &n)
2083 && n == strlen(s)) {
2084 *mask = eth_addr_exact;
2089 if (ovs_scan(s, ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT"%n",
2090 ETH_ADDR_SCAN_ARGS(*mac), ETH_ADDR_SCAN_ARGS(*mask), &n)
2091 && n == strlen(s)) {
2095 return xasprintf("%s: invalid Ethernet address", s);
2099 mf_from_ipv4_string(const struct mf_field *mf, const char *s,
2100 ovs_be32 *ip, ovs_be32 *mask)
2102 ovs_assert(mf->n_bytes == sizeof *ip);
2103 return ip_parse_masked(s, ip, mask);
2107 mf_from_ipv6_string(const struct mf_field *mf, const char *s,
2108 struct in6_addr *ipv6, struct in6_addr *mask)
2110 ovs_assert(mf->n_bytes == sizeof *ipv6);
2111 return ipv6_parse_masked(s, ipv6, mask);
2115 mf_from_ofp_port_string(const struct mf_field *mf, const char *s,
2116 ovs_be16 *valuep, ovs_be16 *maskp)
2120 ovs_assert(mf->n_bytes == sizeof(ovs_be16));
2122 if (ofputil_port_from_string(s, &port)) {
2123 *valuep = htons(ofp_to_u16(port));
2124 *maskp = OVS_BE16_MAX;
2127 return xasprintf("%s: port value out of range for %s", s, mf->name);
2131 mf_from_ofp_port_string32(const struct mf_field *mf, const char *s,
2132 ovs_be32 *valuep, ovs_be32 *maskp)
2136 ovs_assert(mf->n_bytes == sizeof(ovs_be32));
2137 if (ofputil_port_from_string(s, &port)) {
2138 *valuep = ofputil_port_to_ofp11(port);
2139 *maskp = OVS_BE32_MAX;
2142 return xasprintf("%s: port value out of range for %s", s, mf->name);
2145 struct frag_handling {
2151 static const struct frag_handling all_frags[] = {
2152 #define A FLOW_NW_FRAG_ANY
2153 #define L FLOW_NW_FRAG_LATER
2154 /* name mask value */
2157 { "first", A|L, A },
2158 { "later", A|L, A|L },
2163 { "not_later", L, 0 },
2170 mf_from_frag_string(const char *s, uint8_t *valuep, uint8_t *maskp)
2172 const struct frag_handling *h;
2174 for (h = all_frags; h < &all_frags[ARRAY_SIZE(all_frags)]; h++) {
2175 if (!strcasecmp(s, h->name)) {
2176 /* We force the upper bits of the mask on to make mf_parse_value()
2177 * happy (otherwise it will never think it's an exact match.) */
2178 *maskp = h->mask | ~FLOW_NW_FRAG_MASK;
2184 return xasprintf("%s: unknown fragment type (valid types are \"no\", "
2185 "\"yes\", \"first\", \"later\", \"not_first\"", s);
2189 parse_mf_flags(const char *s, const char *(*bit_to_string)(uint32_t),
2190 const char *field_name, ovs_be16 *flagsp, ovs_be16 allowed,
2195 uint32_t flags, mask;
2197 err = parse_flags(s, bit_to_string, '\0', field_name, &err_str,
2198 &flags, ntohs(allowed), maskp ? &mask : NULL);
2203 *flagsp = htons(flags);
2205 *maskp = htons(mask);
2212 mf_from_tcp_flags_string(const char *s, ovs_be16 *flagsp, ovs_be16 *maskp)
2214 return parse_mf_flags(s, packet_tcp_flag_to_string, "TCP", flagsp,
2215 TCP_FLAGS_BE16(OVS_BE16_MAX), maskp);
2219 mf_from_tun_flags_string(const char *s, ovs_be16 *flagsp, ovs_be16 *maskp)
2221 return parse_mf_flags(s, flow_tun_flag_to_string, "tunnel", flagsp,
2222 htons(FLOW_TNL_PUB_F_MASK), maskp);
2226 mf_from_ct_state_string(const char *s, ovs_be32 *flagsp, ovs_be32 *maskp)
2230 uint32_t flags, mask;
2232 err = parse_flags(s, ct_state_to_string, '\0', "ct_state", &err_str,
2233 &flags, CS_SUPPORTED_MASK, maskp ? &mask : NULL);
2238 *flagsp = htonl(flags);
2240 *maskp = htonl(mask);
2246 /* Parses 's', a string value for field 'mf', into 'value' and 'mask'. Returns
2247 * NULL if successful, otherwise a malloc()'d string describing the error. */
2249 mf_parse(const struct mf_field *mf, const char *s,
2250 union mf_value *value, union mf_value *mask)
2254 if (!strcmp(s, "*")) {
2255 memset(value, 0, mf->n_bytes);
2256 memset(mask, 0, mf->n_bytes);
2260 switch (mf->string) {
2262 case MFS_HEXADECIMAL:
2263 error = mf_from_integer_string(mf, s,
2264 (uint8_t *) value, (uint8_t *) mask);
2268 ovs_assert(mf->n_bytes == sizeof(ovs_be32));
2269 error = mf_from_ct_state_string(s, &value->be32, &mask->be32);
2273 error = mf_from_ethernet_string(mf, s, &value->mac, &mask->mac);
2277 error = mf_from_ipv4_string(mf, s, &value->be32, &mask->be32);
2281 error = mf_from_ipv6_string(mf, s, &value->ipv6, &mask->ipv6);
2285 error = mf_from_ofp_port_string(mf, s, &value->be16, &mask->be16);
2288 case MFS_OFP_PORT_OXM:
2289 error = mf_from_ofp_port_string32(mf, s, &value->be32, &mask->be32);
2293 error = mf_from_frag_string(s, &value->u8, &mask->u8);
2297 ovs_assert(mf->n_bytes == sizeof(ovs_be16));
2298 error = mf_from_tun_flags_string(s, &value->be16, &mask->be16);
2302 ovs_assert(mf->n_bytes == sizeof(ovs_be16));
2303 error = mf_from_tcp_flags_string(s, &value->be16, &mask->be16);
2310 if (!error && !mf_is_mask_valid(mf, mask)) {
2311 error = xasprintf("%s: invalid mask for field %s", s, mf->name);
2316 /* Parses 's', a string value for field 'mf', into 'value'. Returns NULL if
2317 * successful, otherwise a malloc()'d string describing the error. */
2319 mf_parse_value(const struct mf_field *mf, const char *s, union mf_value *value)
2321 union mf_value mask;
2324 error = mf_parse(mf, s, value, &mask);
2329 if (!is_all_ones((const uint8_t *) &mask, mf->n_bytes)) {
2330 return xasprintf("%s: wildcards not allowed here", s);
2336 mf_format_integer_string(const struct mf_field *mf, const uint8_t *valuep,
2337 const uint8_t *maskp, struct ds *s)
2339 if (mf->string == MFS_HEXADECIMAL) {
2340 ds_put_hex(s, valuep, mf->n_bytes);
2342 unsigned long long int integer = 0;
2345 ovs_assert(mf->n_bytes <= 8);
2346 for (i = 0; i < mf->n_bytes; i++) {
2347 integer = (integer << 8) | valuep[i];
2349 ds_put_format(s, "%lld", integer);
2353 /* I guess we could write the mask in decimal for MFS_DECIMAL but I'm
2354 * not sure that that a bit-mask written in decimal is ever easier to
2355 * understand than the same bit-mask written in hexadecimal. */
2356 ds_put_char(s, '/');
2357 ds_put_hex(s, maskp, mf->n_bytes);
2362 mf_format_frag_string(uint8_t value, uint8_t mask, struct ds *s)
2364 const struct frag_handling *h;
2366 mask &= FLOW_NW_FRAG_MASK;
2369 for (h = all_frags; h < &all_frags[ARRAY_SIZE(all_frags)]; h++) {
2370 if (value == h->value && mask == h->mask) {
2371 ds_put_cstr(s, h->name);
2375 ds_put_cstr(s, "<error>");
2379 mf_format_tnl_flags_string(ovs_be16 value, ovs_be16 mask, struct ds *s)
2381 format_flags_masked(s, NULL, flow_tun_flag_to_string, ntohs(value),
2382 ntohs(mask) & FLOW_TNL_PUB_F_MASK, FLOW_TNL_PUB_F_MASK);
2386 mf_format_tcp_flags_string(ovs_be16 value, ovs_be16 mask, struct ds *s)
2388 format_flags_masked(s, NULL, packet_tcp_flag_to_string, ntohs(value),
2389 TCP_FLAGS(mask), TCP_FLAGS(OVS_BE16_MAX));
2393 mf_format_ct_state_string(ovs_be32 value, ovs_be32 mask, struct ds *s)
2395 format_flags_masked(s, NULL, ct_state_to_string, ntohl(value),
2396 ntohl(mask), UINT16_MAX);
2399 /* Appends to 's' a string representation of field 'mf' whose value is in
2400 * 'value' and 'mask'. 'mask' may be NULL to indicate an exact match. */
2402 mf_format(const struct mf_field *mf,
2403 const union mf_value *value, const union mf_value *mask,
2407 if (is_all_zeros(mask, mf->n_bytes)) {
2408 ds_put_cstr(s, "ANY");
2410 } else if (is_all_ones(mask, mf->n_bytes)) {
2415 switch (mf->string) {
2416 case MFS_OFP_PORT_OXM:
2419 ofputil_port_from_ofp11(value->be32, &port);
2420 ofputil_format_port(port, s);
2426 ofputil_format_port(u16_to_ofp(ntohs(value->be16)), s);
2431 case MFS_HEXADECIMAL:
2432 mf_format_integer_string(mf, (uint8_t *) value, (uint8_t *) mask, s);
2436 mf_format_ct_state_string(value->be32,
2437 mask ? mask->be32 : OVS_BE32_MAX, s);
2441 eth_format_masked(value->mac, mask ? &mask->mac : NULL, s);
2445 ip_format_masked(value->be32, mask ? mask->be32 : OVS_BE32_MAX, s);
2449 ipv6_format_masked(&value->ipv6, mask ? &mask->ipv6 : NULL, s);
2453 mf_format_frag_string(value->u8, mask ? mask->u8 : UINT8_MAX, s);
2457 mf_format_tnl_flags_string(value->be16,
2458 mask ? mask->be16 : OVS_BE16_MAX, s);
2462 mf_format_tcp_flags_string(value->be16,
2463 mask ? mask->be16 : OVS_BE16_MAX, s);
2471 /* Makes subfield 'sf' within 'flow' exactly match the 'sf->n_bits'
2472 * least-significant bits in 'x'.
2475 mf_write_subfield_flow(const struct mf_subfield *sf,
2476 const union mf_subvalue *x, struct flow *flow)
2478 const struct mf_field *field = sf->field;
2479 union mf_value value;
2481 mf_get_value(field, flow, &value);
2482 bitwise_copy(x, sizeof *x, 0, &value, field->n_bytes,
2483 sf->ofs, sf->n_bits);
2484 mf_set_flow_value(field, &value, flow);
2487 /* Makes subfield 'sf' within 'match' exactly match the 'sf->n_bits'
2488 * least-significant bits in 'x'.
2491 mf_write_subfield(const struct mf_subfield *sf, const union mf_subvalue *x,
2492 struct match *match)
2494 const struct mf_field *field = sf->field;
2495 union mf_value value, mask;
2497 mf_get(field, match, &value, &mask);
2498 bitwise_copy(x, sizeof *x, 0, &value, field->n_bytes, sf->ofs, sf->n_bits);
2499 bitwise_one ( &mask, field->n_bytes, sf->ofs, sf->n_bits);
2500 mf_set(field, &value, &mask, match, NULL);
2503 /* 'v' and 'm' correspond to values of 'field'. This function copies them into
2504 * 'match' in the correspond positions. */
2506 mf_mask_subfield(const struct mf_field *field,
2507 const union mf_subvalue *v,
2508 const union mf_subvalue *m,
2509 struct match *match)
2511 union mf_value value, mask;
2513 mf_get(field, match, &value, &mask);
2514 bitwise_copy(v, sizeof *v, 0, &value, field->n_bytes, 0, field->n_bits);
2515 bitwise_copy(m, sizeof *m, 0, &mask, field->n_bytes, 0, field->n_bits);
2516 mf_set(field, &value, &mask, match, NULL);
2519 /* Initializes 'x' to the value of 'sf' within 'flow'. 'sf' must be valid for
2520 * reading 'flow', e.g. as checked by mf_check_src(). */
2522 mf_read_subfield(const struct mf_subfield *sf, const struct flow *flow,
2523 union mf_subvalue *x)
2525 union mf_value value;
2527 mf_get_value(sf->field, flow, &value);
2529 memset(x, 0, sizeof *x);
2530 bitwise_copy(&value, sf->field->n_bytes, sf->ofs,
2535 /* Returns the value of 'sf' within 'flow'. 'sf' must be valid for reading
2536 * 'flow', e.g. as checked by mf_check_src() and sf->n_bits must be 64 or
2539 mf_get_subfield(const struct mf_subfield *sf, const struct flow *flow)
2541 union mf_value value;
2543 mf_get_value(sf->field, flow, &value);
2544 return bitwise_get(&value, sf->field->n_bytes, sf->ofs, sf->n_bits);
2548 mf_format_subvalue(const union mf_subvalue *subvalue, struct ds *s)
2550 ds_put_hex(s, subvalue->u8, sizeof subvalue->u8);
2554 field_array_set(enum mf_field_id id, const union mf_value *value,
2555 struct field_array *fa)
2557 ovs_assert(id < MFF_N_IDS);
2558 bitmap_set1(fa->used.bm, id);
2559 fa->value[id] = *value;