2 * Copyright (c) 2011, 2012, 2013, 2014, 2015 Nicira, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include "meta-flow.h"
23 #include <netinet/icmp6.h>
24 #include <netinet/ip6.h>
26 #include "classifier.h"
27 #include "dynamic-string.h"
29 #include "ofp-errors.h"
31 #include "ovs-thread.h"
35 #include "socket-util.h"
36 #include "tun-metadata.h"
37 #include "unaligned.h"
39 #include "openvswitch/vlog.h"
41 VLOG_DEFINE_THIS_MODULE(meta_flow);
43 #define FLOW_U32OFS(FIELD) \
44 offsetof(struct flow, FIELD) % 4 ? -1 : offsetof(struct flow, FIELD) / 4
46 #define MF_FIELD_SIZES(MEMBER) \
47 sizeof ((union mf_value *)0)->MEMBER, \
48 8 * sizeof ((union mf_value *)0)->MEMBER
50 extern const struct mf_field mf_fields[MFF_N_IDS]; /* Silence a warning. */
52 const struct mf_field mf_fields[MFF_N_IDS] = {
53 #include "meta-flow.inc"
56 /* Maps from an mf_field's 'name' or 'extra_name' to the mf_field. */
57 static struct shash mf_by_name;
59 /* Rate limit for parse errors. These always indicate a bug in an OpenFlow
60 * controller and so there's not much point in showing a lot of them. */
61 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
63 #define MF_VALUE_EXACT_8 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
64 #define MF_VALUE_EXACT_16 MF_VALUE_EXACT_8, MF_VALUE_EXACT_8
65 #define MF_VALUE_EXACT_32 MF_VALUE_EXACT_16, MF_VALUE_EXACT_16
66 #define MF_VALUE_EXACT_64 MF_VALUE_EXACT_32, MF_VALUE_EXACT_32
67 #define MF_VALUE_EXACT_128 MF_VALUE_EXACT_64, MF_VALUE_EXACT_64
68 #define MF_VALUE_EXACT_INITIALIZER { .tun_metadata = { MF_VALUE_EXACT_128 } }
70 const union mf_value exact_match_mask = MF_VALUE_EXACT_INITIALIZER;
72 static void nxm_init(void);
74 /* Returns the field with the given 'name', or a null pointer if no field has
76 const struct mf_field *
77 mf_from_name(const char *name)
80 return shash_find_data(&mf_by_name, name);
88 shash_init(&mf_by_name);
89 for (i = 0; i < MFF_N_IDS; i++) {
90 const struct mf_field *mf = &mf_fields[i];
92 ovs_assert(mf->id == i); /* Fields must be in the enum order. */
94 shash_add_once(&mf_by_name, mf->name, mf);
96 shash_add_once(&mf_by_name, mf->extra_name, mf);
104 static pthread_once_t once = PTHREAD_ONCE_INIT;
105 pthread_once(&once, nxm_do_init);
108 /* Consider the two value/mask pairs 'a_value/a_mask' and 'b_value/b_mask' as
109 * restrictions on a field's value. Then, this function initializes
110 * 'dst_value/dst_mask' such that it combines the restrictions of both pairs.
111 * This is not always possible, i.e. if one pair insists on a value of 0 in
112 * some bit and the other pair insists on a value of 1 in that bit. This
113 * function returns false in a case where the combined restriction is
114 * impossible (in which case 'dst_value/dst_mask' is not fully initialized),
117 * (As usually true for value/mask pairs in OVS, any 1-bit in a value must have
118 * a corresponding 1-bit in its mask.) */
120 mf_subvalue_intersect(const union mf_subvalue *a_value,
121 const union mf_subvalue *a_mask,
122 const union mf_subvalue *b_value,
123 const union mf_subvalue *b_mask,
124 union mf_subvalue *dst_value,
125 union mf_subvalue *dst_mask)
127 for (int i = 0; i < ARRAY_SIZE(a_value->be64); i++) {
128 ovs_be64 av = a_value->be64[i];
129 ovs_be64 am = a_mask->be64[i];
130 ovs_be64 bv = b_value->be64[i];
131 ovs_be64 bm = b_mask->be64[i];
132 ovs_be64 *dv = &dst_value->be64[i];
133 ovs_be64 *dm = &dst_mask->be64[i];
135 if ((av ^ bv) & (am & bm)) {
144 /* Returns the "number of bits" in 'v', e.g. 1 if only the lowest-order bit is
145 * set, 2 if the second-lowest-order bit is set, and so on. */
147 mf_subvalue_width(const union mf_subvalue *v)
149 return 1 + bitwise_rscan(v, sizeof *v, true, sizeof *v * 8 - 1, -1);
152 /* For positive 'n', shifts the bits in 'value' 'n' bits to the left, and for
153 * negative 'n', shifts the bits '-n' bits to the right. */
155 mf_subvalue_shift(union mf_subvalue *value, int n)
158 union mf_subvalue tmp;
159 memset(&tmp, 0, sizeof tmp);
161 if (n > 0 && n < 8 * sizeof tmp) {
162 bitwise_copy(value, sizeof *value, 0,
165 } else if (n < 0 && n > -8 * sizeof tmp) {
166 bitwise_copy(value, sizeof *value, -n,
174 /* Returns true if 'wc' wildcards all the bits in field 'mf', false if 'wc'
175 * specifies at least one bit in the field.
177 * The caller is responsible for ensuring that 'wc' corresponds to a flow that
178 * meets 'mf''s prerequisites. */
180 mf_is_all_wild(const struct mf_field *mf, const struct flow_wildcards *wc)
184 return !wc->masks.dp_hash;
186 return !wc->masks.recirc_id;
188 return !wc->masks.conj_id;
190 return !wc->masks.tunnel.ip_src;
192 return !wc->masks.tunnel.ip_dst;
194 return !wc->masks.tunnel.tun_id;
196 return !wc->masks.tunnel.ip_tos;
198 return !wc->masks.tunnel.ip_ttl;
200 return !(wc->masks.tunnel.flags & FLOW_TNL_PUB_F_MASK);
202 return !wc->masks.tunnel.gbp_id;
203 case MFF_TUN_GBP_FLAGS:
204 return !wc->masks.tunnel.gbp_flags;
205 CASE_MFF_TUN_METADATA:
206 return !ULLONG_GET(wc->masks.tunnel.metadata.present.map,
207 mf->id - MFF_TUN_METADATA0);
209 return !wc->masks.metadata;
211 case MFF_IN_PORT_OXM:
212 return !wc->masks.in_port.ofp_port;
213 case MFF_SKB_PRIORITY:
214 return !wc->masks.skb_priority;
216 return !wc->masks.pkt_mark;
218 return !wc->masks.ct_state;
220 return !wc->masks.ct_zone;
222 return !wc->masks.ct_mark;
224 return !wc->masks.regs[mf->id - MFF_REG0];
226 return !flow_get_xreg(&wc->masks, mf->id - MFF_XREG0);
227 case MFF_ACTSET_OUTPUT:
228 return !wc->masks.actset_output;
231 return eth_addr_is_zero(wc->masks.dl_src);
233 return eth_addr_is_zero(wc->masks.dl_dst);
235 return !wc->masks.dl_type;
239 return eth_addr_is_zero(wc->masks.arp_sha);
243 return eth_addr_is_zero(wc->masks.arp_tha);
246 return !wc->masks.vlan_tci;
248 return !(wc->masks.vlan_tci & htons(VLAN_VID_MASK));
250 return !(wc->masks.vlan_tci & htons(VLAN_VID_MASK | VLAN_CFI));
251 case MFF_DL_VLAN_PCP:
253 return !(wc->masks.vlan_tci & htons(VLAN_PCP_MASK));
256 return !(wc->masks.mpls_lse[0] & htonl(MPLS_LABEL_MASK));
258 return !(wc->masks.mpls_lse[0] & htonl(MPLS_TC_MASK));
260 return !(wc->masks.mpls_lse[0] & htonl(MPLS_BOS_MASK));
263 return !wc->masks.nw_src;
265 return !wc->masks.nw_dst;
268 return ipv6_mask_is_any(&wc->masks.ipv6_src);
270 return ipv6_mask_is_any(&wc->masks.ipv6_dst);
273 return !wc->masks.ipv6_label;
276 return !wc->masks.nw_proto;
278 case MFF_IP_DSCP_SHIFTED:
279 return !(wc->masks.nw_tos & IP_DSCP_MASK);
281 return !(wc->masks.nw_tos & IP_ECN_MASK);
283 return !wc->masks.nw_ttl;
286 return ipv6_mask_is_any(&wc->masks.nd_target);
289 return !(wc->masks.nw_frag & FLOW_NW_FRAG_MASK);
292 return !wc->masks.nw_proto;
294 return !wc->masks.nw_src;
296 return !wc->masks.nw_dst;
301 case MFF_ICMPV4_TYPE:
302 case MFF_ICMPV6_TYPE:
303 return !wc->masks.tp_src;
307 case MFF_ICMPV4_CODE:
308 case MFF_ICMPV6_CODE:
309 return !wc->masks.tp_dst;
311 return !wc->masks.tcp_flags;
319 /* Initializes 'mask' with the wildcard bit pattern for field 'mf' within 'wc'.
320 * Each bit in 'mask' will be set to 1 if the bit is significant for matching
321 * purposes, or to 0 if it is wildcarded.
323 * The caller is responsible for ensuring that 'wc' corresponds to a flow that
324 * meets 'mf''s prerequisites. */
326 mf_get_mask(const struct mf_field *mf, const struct flow_wildcards *wc,
327 union mf_value *mask)
329 mf_get_value(mf, &wc->masks, mask);
332 /* Tests whether 'mask' is a valid wildcard bit pattern for 'mf'. Returns true
333 * if the mask is valid, false otherwise. */
335 mf_is_mask_valid(const struct mf_field *mf, const union mf_value *mask)
337 switch (mf->maskable) {
339 return (is_all_zeros(mask, mf->n_bytes) ||
340 is_all_ones(mask, mf->n_bytes));
349 /* Returns true if 'flow' meets the prerequisites for 'mf', false otherwise. */
351 mf_are_prereqs_ok(const struct mf_field *mf, const struct flow *flow)
353 switch (mf->prereqs) {
358 return (flow->dl_type == htons(ETH_TYPE_ARP) ||
359 flow->dl_type == htons(ETH_TYPE_RARP));
361 return flow->dl_type == htons(ETH_TYPE_IP);
363 return flow->dl_type == htons(ETH_TYPE_IPV6);
365 return (flow->vlan_tci & htons(VLAN_CFI)) != 0;
367 return eth_type_mpls(flow->dl_type);
369 return is_ip_any(flow);
372 return is_ip_any(flow) && flow->nw_proto == IPPROTO_TCP
373 && !(flow->nw_frag & FLOW_NW_FRAG_LATER);
375 return is_ip_any(flow) && flow->nw_proto == IPPROTO_UDP
376 && !(flow->nw_frag & FLOW_NW_FRAG_LATER);
378 return is_ip_any(flow) && flow->nw_proto == IPPROTO_SCTP
379 && !(flow->nw_frag & FLOW_NW_FRAG_LATER);
381 return is_icmpv4(flow);
383 return is_icmpv6(flow);
386 return (is_icmpv6(flow)
387 && flow->tp_dst == htons(0)
388 && (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT) ||
389 flow->tp_src == htons(ND_NEIGHBOR_ADVERT)));
391 return (is_icmpv6(flow)
392 && flow->tp_dst == htons(0)
393 && (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT)));
395 return (is_icmpv6(flow)
396 && flow->tp_dst == htons(0)
397 && (flow->tp_src == htons(ND_NEIGHBOR_ADVERT)));
403 /* Set field and it's prerequisities in the mask.
404 * This is only ever called for writeable 'mf's, but we do not make the
405 * distinction here. */
407 mf_mask_field_and_prereqs(const struct mf_field *mf, struct flow_wildcards *wc)
409 mf_set_flow_value(mf, &exact_match_mask, &wc->masks);
411 switch (mf->prereqs) {
415 WC_MASK_FIELD(wc, tp_src);
416 WC_MASK_FIELD(wc, tp_dst);
423 /* nw_frag always unwildcarded. */
424 WC_MASK_FIELD(wc, nw_proto);
431 /* dl_type always unwildcarded. */
434 WC_MASK_FIELD_MASK(wc, vlan_tci, htons(VLAN_CFI));
441 /* Set bits of 'bm' corresponding to the field 'mf' and it's prerequisities. */
443 mf_bitmap_set_field_and_prereqs(const struct mf_field *mf, struct mf_bitmap *bm)
445 bitmap_set1(bm->bm, mf->id);
447 switch (mf->prereqs) {
451 bitmap_set1(bm->bm, MFF_TCP_SRC);
452 bitmap_set1(bm->bm, MFF_TCP_DST);
459 /* nw_frag always unwildcarded. */
460 bitmap_set1(bm->bm, MFF_IP_PROTO);
467 bitmap_set1(bm->bm, MFF_ETH_TYPE);
470 bitmap_set1(bm->bm, MFF_VLAN_TCI);
477 /* Returns true if 'value' may be a valid value *as part of a masked match*,
480 * A value is not rejected just because it is not valid for the field in
481 * question, but only if it doesn't make sense to test the bits in question at
482 * all. For example, the MFF_VLAN_TCI field will never have a nonzero value
483 * without the VLAN_CFI bit being set, but we can't reject those values because
484 * it is still legitimate to test just for those bits (see the documentation
485 * for NXM_OF_VLAN_TCI in nicira-ext.h). On the other hand, there is never a
486 * reason to set the low bit of MFF_IP_DSCP to 1, so we reject that. */
488 mf_is_value_valid(const struct mf_field *mf, const union mf_value *value)
500 case MFF_TUN_GBP_FLAGS:
501 CASE_MFF_TUN_METADATA:
504 case MFF_SKB_PRIORITY:
530 case MFF_ICMPV4_TYPE:
531 case MFF_ICMPV4_CODE:
532 case MFF_ICMPV6_TYPE:
533 case MFF_ICMPV6_CODE:
539 case MFF_IN_PORT_OXM:
540 case MFF_ACTSET_OUTPUT: {
542 return !ofputil_port_from_ofp11(value->be32, &port);
546 return !(value->u8 & ~IP_DSCP_MASK);
547 case MFF_IP_DSCP_SHIFTED:
548 return !(value->u8 & (~IP_DSCP_MASK >> 2));
550 return !(value->u8 & ~IP_ECN_MASK);
552 return !(value->u8 & ~FLOW_NW_FRAG_MASK);
554 return !(value->be16 & ~htons(0x0fff));
557 return !(value->be16 & htons(0xff00));
560 return !(value->be16 & htons(VLAN_CFI | VLAN_PCP_MASK));
562 return !(value->be16 & htons(VLAN_PCP_MASK));
564 case MFF_DL_VLAN_PCP:
566 return !(value->u8 & ~(VLAN_PCP_MASK >> VLAN_PCP_SHIFT));
569 return !(value->be32 & ~htonl(IPV6_LABEL_MASK));
572 return !(value->be32 & ~htonl(MPLS_LABEL_MASK >> MPLS_LABEL_SHIFT));
575 return !(value->u8 & ~(MPLS_TC_MASK >> MPLS_TC_SHIFT));
578 return !(value->u8 & ~(MPLS_BOS_MASK >> MPLS_BOS_SHIFT));
581 return !(value->be16 & ~htons(FLOW_TNL_PUB_F_MASK));
584 return !(value->be32 & ~htonl(CS_SUPPORTED_MASK));
592 /* Copies the value of field 'mf' from 'flow' into 'value'. The caller is
593 * responsible for ensuring that 'flow' meets 'mf''s prerequisites. */
595 mf_get_value(const struct mf_field *mf, const struct flow *flow,
596 union mf_value *value)
600 value->be32 = htonl(flow->dp_hash);
603 value->be32 = htonl(flow->recirc_id);
606 value->be32 = htonl(flow->conj_id);
609 value->be64 = flow->tunnel.tun_id;
612 value->be32 = flow->tunnel.ip_src;
615 value->be32 = flow->tunnel.ip_dst;
618 value->be16 = htons(flow->tunnel.flags & FLOW_TNL_PUB_F_MASK);
621 value->be16 = flow->tunnel.gbp_id;
623 case MFF_TUN_GBP_FLAGS:
624 value->u8 = flow->tunnel.gbp_flags;
627 value->u8 = flow->tunnel.ip_ttl;
630 value->u8 = flow->tunnel.ip_tos;
632 CASE_MFF_TUN_METADATA:
633 tun_metadata_read(&flow->tunnel, mf, value);
637 value->be64 = flow->metadata;
641 value->be16 = htons(ofp_to_u16(flow->in_port.ofp_port));
643 case MFF_IN_PORT_OXM:
644 value->be32 = ofputil_port_to_ofp11(flow->in_port.ofp_port);
646 case MFF_ACTSET_OUTPUT:
647 value->be32 = ofputil_port_to_ofp11(flow->actset_output);
650 case MFF_SKB_PRIORITY:
651 value->be32 = htonl(flow->skb_priority);
655 value->be32 = htonl(flow->pkt_mark);
659 value->be32 = htonl(flow->ct_state);
663 value->be16 = htons(flow->ct_zone);
667 value->be32 = htonl(flow->ct_mark);
671 value->be32 = htonl(flow->regs[mf->id - MFF_REG0]);
675 value->be64 = htonll(flow_get_xreg(flow, mf->id - MFF_XREG0));
679 value->mac = flow->dl_src;
683 value->mac = flow->dl_dst;
687 value->be16 = flow->dl_type;
691 value->be16 = flow->vlan_tci;
695 value->be16 = flow->vlan_tci & htons(VLAN_VID_MASK);
698 value->be16 = flow->vlan_tci & htons(VLAN_VID_MASK | VLAN_CFI);
701 case MFF_DL_VLAN_PCP:
703 value->u8 = vlan_tci_to_pcp(flow->vlan_tci);
707 value->be32 = htonl(mpls_lse_to_label(flow->mpls_lse[0]));
711 value->u8 = mpls_lse_to_tc(flow->mpls_lse[0]);
715 value->u8 = mpls_lse_to_bos(flow->mpls_lse[0]);
719 value->be32 = flow->nw_src;
723 value->be32 = flow->nw_dst;
727 value->ipv6 = flow->ipv6_src;
731 value->ipv6 = flow->ipv6_dst;
735 value->be32 = flow->ipv6_label;
739 value->u8 = flow->nw_proto;
743 value->u8 = flow->nw_tos & IP_DSCP_MASK;
746 case MFF_IP_DSCP_SHIFTED:
747 value->u8 = flow->nw_tos >> 2;
751 value->u8 = flow->nw_tos & IP_ECN_MASK;
755 value->u8 = flow->nw_ttl;
759 value->u8 = flow->nw_frag;
763 value->be16 = htons(flow->nw_proto);
767 value->be32 = flow->nw_src;
771 value->be32 = flow->nw_dst;
776 value->mac = flow->arp_sha;
781 value->mac = flow->arp_tha;
787 value->be16 = flow->tp_src;
793 value->be16 = flow->tp_dst;
797 value->be16 = flow->tcp_flags;
800 case MFF_ICMPV4_TYPE:
801 case MFF_ICMPV6_TYPE:
802 value->u8 = ntohs(flow->tp_src);
805 case MFF_ICMPV4_CODE:
806 case MFF_ICMPV6_CODE:
807 value->u8 = ntohs(flow->tp_dst);
811 value->ipv6 = flow->nd_target;
820 /* Makes 'match' match field 'mf' exactly, with the value matched taken from
821 * 'value'. The caller is responsible for ensuring that 'match' meets 'mf''s
824 * If non-NULL, 'err_str' returns a malloc'ed string describing any errors
825 * with the request or NULL if there is no error. The caller is reponsible
826 * for freeing the string. */
828 mf_set_value(const struct mf_field *mf,
829 const union mf_value *value, struct match *match, char **err_str)
837 match_set_dp_hash(match, ntohl(value->be32));
840 match_set_recirc_id(match, ntohl(value->be32));
843 match_set_conj_id(match, ntohl(value->be32));
846 match_set_tun_id(match, value->be64);
849 match_set_tun_src(match, value->be32);
852 match_set_tun_dst(match, value->be32);
855 match_set_tun_flags(match, ntohs(value->be16));
858 match_set_tun_gbp_id(match, value->be16);
860 case MFF_TUN_GBP_FLAGS:
861 match_set_tun_gbp_flags(match, value->u8);
864 match_set_tun_tos(match, value->u8);
867 match_set_tun_ttl(match, value->u8);
869 CASE_MFF_TUN_METADATA:
870 tun_metadata_set_match(mf, value, NULL, match, err_str);
874 match_set_metadata(match, value->be64);
878 match_set_in_port(match, u16_to_ofp(ntohs(value->be16)));
881 case MFF_IN_PORT_OXM: {
883 ofputil_port_from_ofp11(value->be32, &port);
884 match_set_in_port(match, port);
887 case MFF_ACTSET_OUTPUT: {
889 ofputil_port_from_ofp11(value->be32, &port);
890 match_set_actset_output(match, port);
894 case MFF_SKB_PRIORITY:
895 match_set_skb_priority(match, ntohl(value->be32));
899 match_set_pkt_mark(match, ntohl(value->be32));
903 match_set_ct_state(match, ntohl(value->be32));
907 match_set_ct_zone(match, ntohs(value->be16));
911 match_set_ct_mark(match, ntohl(value->be32));
915 match_set_reg(match, mf->id - MFF_REG0, ntohl(value->be32));
919 match_set_xreg(match, mf->id - MFF_XREG0, ntohll(value->be64));
923 match_set_dl_src(match, value->mac);
927 match_set_dl_dst(match, value->mac);
931 match_set_dl_type(match, value->be16);
935 match_set_dl_tci(match, value->be16);
939 match_set_dl_vlan(match, value->be16);
942 match_set_vlan_vid(match, value->be16);
945 case MFF_DL_VLAN_PCP:
947 match_set_dl_vlan_pcp(match, value->u8);
951 match_set_mpls_label(match, 0, value->be32);
955 match_set_mpls_tc(match, 0, value->u8);
959 match_set_mpls_bos(match, 0, value->u8);
963 match_set_nw_src(match, value->be32);
967 match_set_nw_dst(match, value->be32);
971 match_set_ipv6_src(match, &value->ipv6);
975 match_set_ipv6_dst(match, &value->ipv6);
979 match_set_ipv6_label(match, value->be32);
983 match_set_nw_proto(match, value->u8);
987 match_set_nw_dscp(match, value->u8);
990 case MFF_IP_DSCP_SHIFTED:
991 match_set_nw_dscp(match, value->u8 << 2);
995 match_set_nw_ecn(match, value->u8);
999 match_set_nw_ttl(match, value->u8);
1003 match_set_nw_frag(match, value->u8);
1007 match_set_nw_proto(match, ntohs(value->be16));
1011 match_set_nw_src(match, value->be32);
1015 match_set_nw_dst(match, value->be32);
1020 match_set_arp_sha(match, value->mac);
1025 match_set_arp_tha(match, value->mac);
1031 match_set_tp_src(match, value->be16);
1037 match_set_tp_dst(match, value->be16);
1041 match_set_tcp_flags(match, value->be16);
1044 case MFF_ICMPV4_TYPE:
1045 case MFF_ICMPV6_TYPE:
1046 match_set_icmp_type(match, value->u8);
1049 case MFF_ICMPV4_CODE:
1050 case MFF_ICMPV6_CODE:
1051 match_set_icmp_code(match, value->u8);
1055 match_set_nd_target(match, &value->ipv6);
1064 /* Unwildcard 'mask' member field described by 'mf'. The caller is
1065 * responsible for ensuring that 'mask' meets 'mf''s prerequisites. */
1067 mf_mask_field(const struct mf_field *mf, struct flow *mask)
1069 /* For MFF_DL_VLAN, we cannot send a all 1's to flow_set_dl_vlan()
1070 * as that will be considered as OFP10_VLAN_NONE. So consider it as a
1071 * special case. For the rest, calling mf_set_flow_value() is good
1073 if (mf->id == MFF_DL_VLAN) {
1074 flow_set_dl_vlan(mask, htons(VLAN_VID_MASK));
1076 mf_set_flow_value(mf, &exact_match_mask, mask);
1081 field_len(const struct mf_field *mf, const union mf_value *value_)
1083 const uint8_t *value = &value_->u8;
1086 if (!mf->variable_len) {
1094 for (i = 0; i < mf->n_bytes; i++) {
1095 if (value[i] != 0) {
1100 return mf->n_bytes - i;
1103 /* Returns the effective length of the field. For fixed length fields,
1104 * this is just the defined length. For variable length fields, it is
1105 * the minimum size encoding that retains the same meaning (i.e.
1106 * discarding leading zeros).
1108 * 'is_masked' returns (if non-NULL) whether the original contained
1109 * a mask. Otherwise, a mask that is the same length as the value
1110 * might be misinterpreted as an exact match. */
1112 mf_field_len(const struct mf_field *mf, const union mf_value *value,
1113 const union mf_value *mask, bool *is_masked_)
1116 bool is_masked = mask && !is_all_ones(mask, mf->n_bytes);
1118 len = field_len(mf, value);
1120 mask_len = field_len(mf, mask);
1121 len = MAX(len, mask_len);
1125 *is_masked_ = is_masked;
1131 /* Sets 'flow' member field described by 'mf' to 'value'. The caller is
1132 * responsible for ensuring that 'flow' meets 'mf''s prerequisites.*/
1134 mf_set_flow_value(const struct mf_field *mf,
1135 const union mf_value *value, struct flow *flow)
1139 flow->dp_hash = ntohl(value->be32);
1142 flow->recirc_id = ntohl(value->be32);
1145 flow->conj_id = ntohl(value->be32);
1148 flow->tunnel.tun_id = value->be64;
1151 flow->tunnel.ip_src = value->be32;
1154 flow->tunnel.ip_dst = value->be32;
1157 flow->tunnel.flags = (flow->tunnel.flags & ~FLOW_TNL_PUB_F_MASK) |
1160 case MFF_TUN_GBP_ID:
1161 flow->tunnel.gbp_id = value->be16;
1163 case MFF_TUN_GBP_FLAGS:
1164 flow->tunnel.gbp_flags = value->u8;
1167 flow->tunnel.ip_tos = value->u8;
1170 flow->tunnel.ip_ttl = value->u8;
1172 CASE_MFF_TUN_METADATA:
1173 tun_metadata_write(&flow->tunnel, mf, value);
1176 flow->metadata = value->be64;
1180 flow->in_port.ofp_port = u16_to_ofp(ntohs(value->be16));
1183 case MFF_IN_PORT_OXM:
1184 ofputil_port_from_ofp11(value->be32, &flow->in_port.ofp_port);
1186 case MFF_ACTSET_OUTPUT:
1187 ofputil_port_from_ofp11(value->be32, &flow->actset_output);
1190 case MFF_SKB_PRIORITY:
1191 flow->skb_priority = ntohl(value->be32);
1195 flow->pkt_mark = ntohl(value->be32);
1199 flow->ct_state = ntohl(value->be32);
1203 flow->ct_zone = ntohs(value->be16);
1207 flow->ct_mark = ntohl(value->be32);
1211 flow->regs[mf->id - MFF_REG0] = ntohl(value->be32);
1215 flow_set_xreg(flow, mf->id - MFF_XREG0, ntohll(value->be64));
1219 flow->dl_src = value->mac;
1223 flow->dl_dst = value->mac;
1227 flow->dl_type = value->be16;
1231 flow->vlan_tci = value->be16;
1235 flow_set_dl_vlan(flow, value->be16);
1238 flow_set_vlan_vid(flow, value->be16);
1241 case MFF_DL_VLAN_PCP:
1243 flow_set_vlan_pcp(flow, value->u8);
1246 case MFF_MPLS_LABEL:
1247 flow_set_mpls_label(flow, 0, value->be32);
1251 flow_set_mpls_tc(flow, 0, value->u8);
1255 flow_set_mpls_bos(flow, 0, value->u8);
1259 flow->nw_src = value->be32;
1263 flow->nw_dst = value->be32;
1267 flow->ipv6_src = value->ipv6;
1271 flow->ipv6_dst = value->ipv6;
1274 case MFF_IPV6_LABEL:
1275 flow->ipv6_label = value->be32 & htonl(IPV6_LABEL_MASK);
1279 flow->nw_proto = value->u8;
1283 flow->nw_tos &= ~IP_DSCP_MASK;
1284 flow->nw_tos |= value->u8 & IP_DSCP_MASK;
1287 case MFF_IP_DSCP_SHIFTED:
1288 flow->nw_tos &= ~IP_DSCP_MASK;
1289 flow->nw_tos |= value->u8 << 2;
1293 flow->nw_tos &= ~IP_ECN_MASK;
1294 flow->nw_tos |= value->u8 & IP_ECN_MASK;
1298 flow->nw_ttl = value->u8;
1302 flow->nw_frag = value->u8 & FLOW_NW_FRAG_MASK;
1306 flow->nw_proto = ntohs(value->be16);
1310 flow->nw_src = value->be32;
1314 flow->nw_dst = value->be32;
1319 flow->arp_sha = value->mac;
1324 flow->arp_tha = value->mac;
1330 flow->tp_src = value->be16;
1336 flow->tp_dst = value->be16;
1340 flow->tcp_flags = value->be16;
1343 case MFF_ICMPV4_TYPE:
1344 case MFF_ICMPV6_TYPE:
1345 flow->tp_src = htons(value->u8);
1348 case MFF_ICMPV4_CODE:
1349 case MFF_ICMPV6_CODE:
1350 flow->tp_dst = htons(value->u8);
1354 flow->nd_target = value->ipv6;
1363 /* Consider each of 'src', 'mask', and 'dst' as if they were arrays of 8*n
1364 * bits. Then, for each 0 <= i < 8 * n such that mask[i] == 1, sets dst[i] =
1367 apply_mask(const uint8_t *src, const uint8_t *mask, uint8_t *dst, size_t n)
1371 for (i = 0; i < n; i++) {
1372 dst[i] = (src[i] & mask[i]) | (dst[i] & ~mask[i]);
1376 /* Sets 'flow' member field described by 'field' to 'value', except that bits
1377 * for which 'mask' has a 0-bit keep their existing values. The caller is
1378 * responsible for ensuring that 'flow' meets 'field''s prerequisites.*/
1380 mf_set_flow_value_masked(const struct mf_field *field,
1381 const union mf_value *value,
1382 const union mf_value *mask,
1387 mf_get_value(field, flow, &tmp);
1388 apply_mask((const uint8_t *) value, (const uint8_t *) mask,
1389 (uint8_t *) &tmp, field->n_bytes);
1390 mf_set_flow_value(field, &tmp, flow);
1394 mf_is_tun_metadata(const struct mf_field *mf)
1396 return mf->id >= MFF_TUN_METADATA0 &&
1397 mf->id < MFF_TUN_METADATA0 + TUN_METADATA_NUM_OPTS;
1400 /* Returns true if 'mf' has previously been set in 'flow', false if
1401 * it contains a non-default value.
1403 * The caller is responsible for ensuring that 'flow' meets 'mf''s
1406 mf_is_set(const struct mf_field *mf, const struct flow *flow)
1408 if (!mf_is_tun_metadata(mf)) {
1409 union mf_value value;
1411 mf_get_value(mf, flow, &value);
1412 return !is_all_zeros(&value, mf->n_bytes);
1414 return ULLONG_GET(flow->tunnel.metadata.present.map,
1415 mf->id - MFF_TUN_METADATA0);
1419 /* Makes 'match' wildcard field 'mf'.
1421 * The caller is responsible for ensuring that 'match' meets 'mf''s
1424 * If non-NULL, 'err_str' returns a malloc'ed string describing any errors
1425 * with the request or NULL if there is no error. The caller is reponsible
1426 * for freeing the string. */
1428 mf_set_wild(const struct mf_field *mf, struct match *match, char **err_str)
1436 match->flow.dp_hash = 0;
1437 match->wc.masks.dp_hash = 0;
1440 match->flow.recirc_id = 0;
1441 match->wc.masks.recirc_id = 0;
1444 match->flow.conj_id = 0;
1445 match->wc.masks.conj_id = 0;
1448 match_set_tun_id_masked(match, htonll(0), htonll(0));
1451 match_set_tun_src_masked(match, htonl(0), htonl(0));
1454 match_set_tun_dst_masked(match, htonl(0), htonl(0));
1457 match_set_tun_flags_masked(match, 0, 0);
1459 case MFF_TUN_GBP_ID:
1460 match_set_tun_gbp_id_masked(match, 0, 0);
1462 case MFF_TUN_GBP_FLAGS:
1463 match_set_tun_gbp_flags_masked(match, 0, 0);
1466 match_set_tun_tos_masked(match, 0, 0);
1469 match_set_tun_ttl_masked(match, 0, 0);
1471 CASE_MFF_TUN_METADATA:
1472 tun_metadata_set_match(mf, NULL, NULL, match, err_str);
1476 match_set_metadata_masked(match, htonll(0), htonll(0));
1480 case MFF_IN_PORT_OXM:
1481 match->flow.in_port.ofp_port = 0;
1482 match->wc.masks.in_port.ofp_port = 0;
1484 case MFF_ACTSET_OUTPUT:
1485 match->flow.actset_output = 0;
1486 match->wc.masks.actset_output = 0;
1489 case MFF_SKB_PRIORITY:
1490 match->flow.skb_priority = 0;
1491 match->wc.masks.skb_priority = 0;
1495 match->flow.pkt_mark = 0;
1496 match->wc.masks.pkt_mark = 0;
1500 match->flow.ct_state = 0;
1501 match->wc.masks.ct_state = 0;
1505 match->flow.ct_zone = 0;
1506 match->wc.masks.ct_zone = 0;
1510 match->flow.ct_mark = 0;
1511 match->wc.masks.ct_mark = 0;
1515 match_set_reg_masked(match, mf->id - MFF_REG0, 0, 0);
1519 match_set_xreg_masked(match, mf->id - MFF_XREG0, 0, 0);
1523 match->flow.dl_src = eth_addr_zero;
1524 match->wc.masks.dl_src = eth_addr_zero;
1528 match->flow.dl_dst = eth_addr_zero;
1529 match->wc.masks.dl_dst = eth_addr_zero;
1533 match->flow.dl_type = htons(0);
1534 match->wc.masks.dl_type = htons(0);
1538 match_set_dl_tci_masked(match, htons(0), htons(0));
1543 match_set_any_vid(match);
1546 case MFF_DL_VLAN_PCP:
1548 match_set_any_pcp(match);
1551 case MFF_MPLS_LABEL:
1552 match_set_any_mpls_label(match, 0);
1556 match_set_any_mpls_tc(match, 0);
1560 match_set_any_mpls_bos(match, 0);
1565 match_set_nw_src_masked(match, htonl(0), htonl(0));
1570 match_set_nw_dst_masked(match, htonl(0), htonl(0));
1574 memset(&match->wc.masks.ipv6_src, 0, sizeof match->wc.masks.ipv6_src);
1575 memset(&match->flow.ipv6_src, 0, sizeof match->flow.ipv6_src);
1579 memset(&match->wc.masks.ipv6_dst, 0, sizeof match->wc.masks.ipv6_dst);
1580 memset(&match->flow.ipv6_dst, 0, sizeof match->flow.ipv6_dst);
1583 case MFF_IPV6_LABEL:
1584 match->wc.masks.ipv6_label = htonl(0);
1585 match->flow.ipv6_label = htonl(0);
1589 match->wc.masks.nw_proto = 0;
1590 match->flow.nw_proto = 0;
1594 case MFF_IP_DSCP_SHIFTED:
1595 match->wc.masks.nw_tos &= ~IP_DSCP_MASK;
1596 match->flow.nw_tos &= ~IP_DSCP_MASK;
1600 match->wc.masks.nw_tos &= ~IP_ECN_MASK;
1601 match->flow.nw_tos &= ~IP_ECN_MASK;
1605 match->wc.masks.nw_ttl = 0;
1606 match->flow.nw_ttl = 0;
1610 match->wc.masks.nw_frag &= ~FLOW_NW_FRAG_MASK;
1611 match->flow.nw_frag &= ~FLOW_NW_FRAG_MASK;
1615 match->wc.masks.nw_proto = 0;
1616 match->flow.nw_proto = 0;
1621 match->flow.arp_sha = eth_addr_zero;
1622 match->wc.masks.arp_sha = eth_addr_zero;
1627 match->flow.arp_tha = eth_addr_zero;
1628 match->wc.masks.arp_tha = eth_addr_zero;
1634 case MFF_ICMPV4_TYPE:
1635 case MFF_ICMPV6_TYPE:
1636 match->wc.masks.tp_src = htons(0);
1637 match->flow.tp_src = htons(0);
1643 case MFF_ICMPV4_CODE:
1644 case MFF_ICMPV6_CODE:
1645 match->wc.masks.tp_dst = htons(0);
1646 match->flow.tp_dst = htons(0);
1650 match->wc.masks.tcp_flags = htons(0);
1651 match->flow.tcp_flags = htons(0);
1655 memset(&match->wc.masks.nd_target, 0,
1656 sizeof match->wc.masks.nd_target);
1657 memset(&match->flow.nd_target, 0, sizeof match->flow.nd_target);
1666 /* Makes 'match' match field 'mf' with the specified 'value' and 'mask'.
1667 * 'value' specifies a value to match and 'mask' specifies a wildcard pattern,
1668 * with a 1-bit indicating that the corresponding value bit must match and a
1669 * 0-bit indicating a don't-care.
1671 * If 'mask' is NULL or points to all-1-bits, then this call is equivalent to
1672 * mf_set_value(mf, value, match). If 'mask' points to all-0-bits, then this
1673 * call is equivalent to mf_set_wild(mf, match).
1675 * 'mask' must be a valid mask for 'mf' (see mf_is_mask_valid()). The caller
1676 * is responsible for ensuring that 'match' meets 'mf''s prerequisites.
1678 * If non-NULL, 'err_str' returns a malloc'ed string describing any errors
1679 * with the request or NULL if there is no error. The caller is reponsible
1680 * for freeing the string.
1682 * Return a set of enum ofputil_protocol bits (as an uint32_t to avoid circular
1683 * dependency on enum ofputil_protocol definition) indicating which OpenFlow
1684 * protocol versions can support this functionality. */
1686 mf_set(const struct mf_field *mf,
1687 const union mf_value *value, const union mf_value *mask,
1688 struct match *match, char **err_str)
1690 if (!mask || is_all_ones(mask, mf->n_bytes)) {
1691 mf_set_value(mf, value, match, err_str);
1692 return mf->usable_protocols_exact;
1693 } else if (is_all_zeros(mask, mf->n_bytes) && !mf_is_tun_metadata(mf)) {
1694 /* Tunnel metadata matches on the existence of the field itself, so
1695 * it still needs to be encoded even if the value is wildcarded. */
1696 mf_set_wild(mf, match, err_str);
1697 return OFPUTIL_P_ANY;
1709 case MFF_IN_PORT_OXM:
1710 case MFF_ACTSET_OUTPUT:
1711 case MFF_SKB_PRIORITY:
1714 case MFF_DL_VLAN_PCP:
1716 case MFF_MPLS_LABEL:
1722 case MFF_IP_DSCP_SHIFTED:
1725 case MFF_ICMPV4_TYPE:
1726 case MFF_ICMPV4_CODE:
1727 case MFF_ICMPV6_TYPE:
1728 case MFF_ICMPV6_CODE:
1729 return OFPUTIL_P_NONE;
1732 match_set_dp_hash_masked(match, ntohl(value->be32), ntohl(mask->be32));
1735 match_set_tun_id_masked(match, value->be64, mask->be64);
1738 match_set_tun_src_masked(match, value->be32, mask->be32);
1741 match_set_tun_dst_masked(match, value->be32, mask->be32);
1744 match_set_tun_flags_masked(match, ntohs(value->be16), ntohs(mask->be16));
1746 case MFF_TUN_GBP_ID:
1747 match_set_tun_gbp_id_masked(match, value->be16, mask->be16);
1749 case MFF_TUN_GBP_FLAGS:
1750 match_set_tun_gbp_flags_masked(match, value->u8, mask->u8);
1753 match_set_tun_ttl_masked(match, value->u8, mask->u8);
1756 match_set_tun_tos_masked(match, value->u8, mask->u8);
1758 CASE_MFF_TUN_METADATA:
1759 tun_metadata_set_match(mf, value, mask, match, err_str);
1763 match_set_metadata_masked(match, value->be64, mask->be64);
1767 match_set_reg_masked(match, mf->id - MFF_REG0,
1768 ntohl(value->be32), ntohl(mask->be32));
1772 match_set_xreg_masked(match, mf->id - MFF_XREG0,
1773 ntohll(value->be64), ntohll(mask->be64));
1777 match_set_pkt_mark_masked(match, ntohl(value->be32),
1782 match_set_ct_state_masked(match, ntohl(value->be32), ntohl(mask->be32));
1786 match_set_ct_mark_masked(match, ntohl(value->be32), ntohl(mask->be32));
1790 match_set_dl_dst_masked(match, value->mac, mask->mac);
1794 match_set_dl_src_masked(match, value->mac, mask->mac);
1799 match_set_arp_sha_masked(match, value->mac, mask->mac);
1804 match_set_arp_tha_masked(match, value->mac, mask->mac);
1808 match_set_dl_tci_masked(match, value->be16, mask->be16);
1812 match_set_vlan_vid_masked(match, value->be16, mask->be16);
1816 match_set_nw_src_masked(match, value->be32, mask->be32);
1820 match_set_nw_dst_masked(match, value->be32, mask->be32);
1824 match_set_ipv6_src_masked(match, &value->ipv6, &mask->ipv6);
1828 match_set_ipv6_dst_masked(match, &value->ipv6, &mask->ipv6);
1831 case MFF_IPV6_LABEL:
1832 if ((mask->be32 & htonl(IPV6_LABEL_MASK)) == htonl(IPV6_LABEL_MASK)) {
1833 mf_set_value(mf, value, match, err_str);
1835 match_set_ipv6_label_masked(match, value->be32, mask->be32);
1840 match_set_nd_target_masked(match, &value->ipv6, &mask->ipv6);
1844 match_set_nw_frag_masked(match, value->u8, mask->u8);
1848 match_set_nw_src_masked(match, value->be32, mask->be32);
1852 match_set_nw_dst_masked(match, value->be32, mask->be32);
1858 match_set_tp_src_masked(match, value->be16, mask->be16);
1864 match_set_tp_dst_masked(match, value->be16, mask->be16);
1868 match_set_tcp_flags_masked(match, value->be16, mask->be16);
1876 return ((mf->usable_protocols_bitwise == mf->usable_protocols_cidr
1877 || ip_is_cidr(mask->be32))
1878 ? mf->usable_protocols_cidr
1879 : mf->usable_protocols_bitwise);
1883 mf_check__(const struct mf_subfield *sf, const struct flow *flow,
1887 VLOG_WARN_RL(&rl, "unknown %s field", type);
1888 return OFPERR_OFPBAC_BAD_SET_TYPE;
1889 } else if (!sf->n_bits) {
1890 VLOG_WARN_RL(&rl, "zero bit %s field %s", type, sf->field->name);
1891 return OFPERR_OFPBAC_BAD_SET_LEN;
1892 } else if (sf->ofs >= sf->field->n_bits) {
1893 VLOG_WARN_RL(&rl, "bit offset %d exceeds %d-bit width of %s field %s",
1894 sf->ofs, sf->field->n_bits, type, sf->field->name);
1895 return OFPERR_OFPBAC_BAD_SET_LEN;
1896 } else if (sf->ofs + sf->n_bits > sf->field->n_bits) {
1897 VLOG_WARN_RL(&rl, "bit offset %d and width %d exceeds %d-bit width "
1898 "of %s field %s", sf->ofs, sf->n_bits,
1899 sf->field->n_bits, type, sf->field->name);
1900 return OFPERR_OFPBAC_BAD_SET_LEN;
1901 } else if (flow && !mf_are_prereqs_ok(sf->field, flow)) {
1902 VLOG_WARN_RL(&rl, "%s field %s lacks correct prerequisites",
1903 type, sf->field->name);
1904 return OFPERR_OFPBAC_MATCH_INCONSISTENT;
1910 /* Checks whether 'sf' is valid for reading a subfield out of 'flow'. Returns
1911 * 0 if so, otherwise an OpenFlow error code (e.g. as returned by
1914 mf_check_src(const struct mf_subfield *sf, const struct flow *flow)
1916 return mf_check__(sf, flow, "source");
1919 /* Checks whether 'sf' is valid for writing a subfield into 'flow'. Returns 0
1920 * if so, otherwise an OpenFlow error code (e.g. as returned by
1923 mf_check_dst(const struct mf_subfield *sf, const struct flow *flow)
1925 int error = mf_check__(sf, flow, "destination");
1926 if (!error && !sf->field->writable) {
1927 VLOG_WARN_RL(&rl, "destination field %s is not writable",
1929 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
1934 /* Copies the value and wildcard bit pattern for 'mf' from 'match' into the
1935 * 'value' and 'mask', respectively. */
1937 mf_get(const struct mf_field *mf, const struct match *match,
1938 union mf_value *value, union mf_value *mask)
1940 mf_get_value(mf, &match->flow, value);
1941 mf_get_mask(mf, &match->wc, mask);
1945 mf_from_integer_string(const struct mf_field *mf, const char *s,
1946 uint8_t *valuep, uint8_t *maskp)
1949 const char *err_str = "";
1952 err = parse_int_string(s, valuep, mf->n_bytes, &tail);
1953 if (err || (*tail != '\0' && *tail != '/')) {
1959 err = parse_int_string(tail + 1, maskp, mf->n_bytes, &tail);
1960 if (err || *tail != '\0') {
1965 memset(maskp, 0xff, mf->n_bytes);
1971 if (err == ERANGE) {
1972 return xasprintf("%s: %s too large for %u-byte field %s",
1973 s, err_str, mf->n_bytes, mf->name);
1975 return xasprintf("%s: bad syntax for %s %s", s, mf->name, err_str);
1980 mf_from_ethernet_string(const struct mf_field *mf, const char *s,
1981 struct eth_addr *mac, struct eth_addr *mask)
1985 ovs_assert(mf->n_bytes == ETH_ADDR_LEN);
1988 if (ovs_scan(s, ETH_ADDR_SCAN_FMT"%n", ETH_ADDR_SCAN_ARGS(*mac), &n)
1989 && n == strlen(s)) {
1990 *mask = eth_addr_exact;
1995 if (ovs_scan(s, ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT"%n",
1996 ETH_ADDR_SCAN_ARGS(*mac), ETH_ADDR_SCAN_ARGS(*mask), &n)
1997 && n == strlen(s)) {
2001 return xasprintf("%s: invalid Ethernet address", s);
2005 mf_from_ipv4_string(const struct mf_field *mf, const char *s,
2006 ovs_be32 *ip, ovs_be32 *mask)
2010 ovs_assert(mf->n_bytes == sizeof *ip);
2012 if (ovs_scan(s, IP_SCAN_FMT"/"IP_SCAN_FMT,
2013 IP_SCAN_ARGS(ip), IP_SCAN_ARGS(mask))) {
2015 } else if (ovs_scan(s, IP_SCAN_FMT"/%d", IP_SCAN_ARGS(ip), &prefix)) {
2016 if (prefix <= 0 || prefix > 32) {
2017 return xasprintf("%s: network prefix bits not between 0 and "
2020 *mask = be32_prefix_mask(prefix);
2021 } else if (ovs_scan(s, IP_SCAN_FMT, IP_SCAN_ARGS(ip))) {
2022 *mask = OVS_BE32_MAX;
2024 return xasprintf("%s: invalid IP address", s);
2030 mf_from_ipv6_string(const struct mf_field *mf, const char *s,
2031 struct in6_addr *value, struct in6_addr *mask)
2033 char *str = xstrdup(s);
2034 char *save_ptr = NULL;
2035 const char *name, *netmask;
2038 ovs_assert(mf->n_bytes == sizeof *value);
2040 name = strtok_r(str, "/", &save_ptr);
2041 retval = name ? lookup_ipv6(name, value) : EINVAL;
2045 err = xasprintf("%s: could not convert to IPv6 address", str);
2051 netmask = strtok_r(NULL, "/", &save_ptr);
2053 if (inet_pton(AF_INET6, netmask, mask) != 1) {
2054 int prefix = atoi(netmask);
2055 if (prefix <= 0 || prefix > 128) {
2057 return xasprintf("%s: prefix bits not between 1 and 128", s);
2059 *mask = ipv6_create_mask(prefix);
2063 *mask = in6addr_exact;
2071 mf_from_ofp_port_string(const struct mf_field *mf, const char *s,
2072 ovs_be16 *valuep, ovs_be16 *maskp)
2076 ovs_assert(mf->n_bytes == sizeof(ovs_be16));
2078 if (ofputil_port_from_string(s, &port)) {
2079 *valuep = htons(ofp_to_u16(port));
2080 *maskp = OVS_BE16_MAX;
2083 return xasprintf("%s: port value out of range for %s", s, mf->name);
2087 mf_from_ofp_port_string32(const struct mf_field *mf, const char *s,
2088 ovs_be32 *valuep, ovs_be32 *maskp)
2092 ovs_assert(mf->n_bytes == sizeof(ovs_be32));
2093 if (ofputil_port_from_string(s, &port)) {
2094 *valuep = ofputil_port_to_ofp11(port);
2095 *maskp = OVS_BE32_MAX;
2098 return xasprintf("%s: port value out of range for %s", s, mf->name);
2101 struct frag_handling {
2107 static const struct frag_handling all_frags[] = {
2108 #define A FLOW_NW_FRAG_ANY
2109 #define L FLOW_NW_FRAG_LATER
2110 /* name mask value */
2113 { "first", A|L, A },
2114 { "later", A|L, A|L },
2119 { "not_later", L, 0 },
2126 mf_from_frag_string(const char *s, uint8_t *valuep, uint8_t *maskp)
2128 const struct frag_handling *h;
2130 for (h = all_frags; h < &all_frags[ARRAY_SIZE(all_frags)]; h++) {
2131 if (!strcasecmp(s, h->name)) {
2132 /* We force the upper bits of the mask on to make mf_parse_value()
2133 * happy (otherwise it will never think it's an exact match.) */
2134 *maskp = h->mask | ~FLOW_NW_FRAG_MASK;
2140 return xasprintf("%s: unknown fragment type (valid types are \"no\", "
2141 "\"yes\", \"first\", \"later\", \"not_first\"", s);
2145 parse_mf_flags(const char *s, const char *(*bit_to_string)(uint32_t),
2146 const char *field_name, ovs_be16 *flagsp, ovs_be16 allowed,
2151 uint32_t flags, mask;
2153 err = parse_flags(s, bit_to_string, '\0', field_name, &err_str,
2154 &flags, ntohs(allowed), maskp ? &mask : NULL);
2159 *flagsp = htons(flags);
2161 *maskp = htons(mask);
2168 mf_from_tcp_flags_string(const char *s, ovs_be16 *flagsp, ovs_be16 *maskp)
2170 return parse_mf_flags(s, packet_tcp_flag_to_string, "TCP", flagsp,
2171 TCP_FLAGS_BE16(OVS_BE16_MAX), maskp);
2175 mf_from_tun_flags_string(const char *s, ovs_be16 *flagsp, ovs_be16 *maskp)
2177 return parse_mf_flags(s, flow_tun_flag_to_string, "tunnel", flagsp,
2178 htons(FLOW_TNL_PUB_F_MASK), maskp);
2182 mf_from_ct_state_string(const char *s, ovs_be32 *flagsp, ovs_be32 *maskp)
2186 uint32_t flags, mask;
2188 err = parse_flags(s, ct_state_to_string, '\0', "ct_state", &err_str,
2189 &flags, CS_SUPPORTED_MASK, maskp ? &mask : NULL);
2194 *flagsp = htonl(flags);
2196 *maskp = htonl(mask);
2202 /* Parses 's', a string value for field 'mf', into 'value' and 'mask'. Returns
2203 * NULL if successful, otherwise a malloc()'d string describing the error. */
2205 mf_parse(const struct mf_field *mf, const char *s,
2206 union mf_value *value, union mf_value *mask)
2210 if (!strcmp(s, "*")) {
2211 memset(value, 0, mf->n_bytes);
2212 memset(mask, 0, mf->n_bytes);
2216 switch (mf->string) {
2218 case MFS_HEXADECIMAL:
2219 error = mf_from_integer_string(mf, s,
2220 (uint8_t *) value, (uint8_t *) mask);
2224 ovs_assert(mf->n_bytes == sizeof(ovs_be32));
2225 error = mf_from_ct_state_string(s, &value->be32, &mask->be32);
2229 error = mf_from_ethernet_string(mf, s, &value->mac, &mask->mac);
2233 error = mf_from_ipv4_string(mf, s, &value->be32, &mask->be32);
2237 error = mf_from_ipv6_string(mf, s, &value->ipv6, &mask->ipv6);
2241 error = mf_from_ofp_port_string(mf, s, &value->be16, &mask->be16);
2244 case MFS_OFP_PORT_OXM:
2245 error = mf_from_ofp_port_string32(mf, s, &value->be32, &mask->be32);
2249 error = mf_from_frag_string(s, &value->u8, &mask->u8);
2253 ovs_assert(mf->n_bytes == sizeof(ovs_be16));
2254 error = mf_from_tun_flags_string(s, &value->be16, &mask->be16);
2258 ovs_assert(mf->n_bytes == sizeof(ovs_be16));
2259 error = mf_from_tcp_flags_string(s, &value->be16, &mask->be16);
2266 if (!error && !mf_is_mask_valid(mf, mask)) {
2267 error = xasprintf("%s: invalid mask for field %s", s, mf->name);
2272 /* Parses 's', a string value for field 'mf', into 'value'. Returns NULL if
2273 * successful, otherwise a malloc()'d string describing the error. */
2275 mf_parse_value(const struct mf_field *mf, const char *s, union mf_value *value)
2277 union mf_value mask;
2280 error = mf_parse(mf, s, value, &mask);
2285 if (!is_all_ones((const uint8_t *) &mask, mf->n_bytes)) {
2286 return xasprintf("%s: wildcards not allowed here", s);
2292 mf_format_integer_string(const struct mf_field *mf, const uint8_t *valuep,
2293 const uint8_t *maskp, struct ds *s)
2295 if (mf->string == MFS_HEXADECIMAL) {
2296 ds_put_hex(s, valuep, mf->n_bytes);
2298 unsigned long long int integer = 0;
2301 ovs_assert(mf->n_bytes <= 8);
2302 for (i = 0; i < mf->n_bytes; i++) {
2303 integer = (integer << 8) | valuep[i];
2305 ds_put_format(s, "%lld", integer);
2309 /* I guess we could write the mask in decimal for MFS_DECIMAL but I'm
2310 * not sure that that a bit-mask written in decimal is ever easier to
2311 * understand than the same bit-mask written in hexadecimal. */
2312 ds_put_char(s, '/');
2313 ds_put_hex(s, maskp, mf->n_bytes);
2318 mf_format_frag_string(uint8_t value, uint8_t mask, struct ds *s)
2320 const struct frag_handling *h;
2322 mask &= FLOW_NW_FRAG_MASK;
2325 for (h = all_frags; h < &all_frags[ARRAY_SIZE(all_frags)]; h++) {
2326 if (value == h->value && mask == h->mask) {
2327 ds_put_cstr(s, h->name);
2331 ds_put_cstr(s, "<error>");
2335 mf_format_tnl_flags_string(ovs_be16 value, ovs_be16 mask, struct ds *s)
2337 format_flags_masked(s, NULL, flow_tun_flag_to_string, ntohs(value),
2338 ntohs(mask) & FLOW_TNL_PUB_F_MASK, FLOW_TNL_PUB_F_MASK);
2342 mf_format_tcp_flags_string(ovs_be16 value, ovs_be16 mask, struct ds *s)
2344 format_flags_masked(s, NULL, packet_tcp_flag_to_string, ntohs(value),
2345 TCP_FLAGS(mask), TCP_FLAGS(OVS_BE16_MAX));
2349 mf_format_ct_state_string(ovs_be32 value, ovs_be32 mask, struct ds *s)
2351 format_flags_masked(s, NULL, ct_state_to_string, ntohl(value),
2352 ntohl(mask), UINT16_MAX);
2355 /* Appends to 's' a string representation of field 'mf' whose value is in
2356 * 'value' and 'mask'. 'mask' may be NULL to indicate an exact match. */
2358 mf_format(const struct mf_field *mf,
2359 const union mf_value *value, const union mf_value *mask,
2363 if (is_all_zeros(mask, mf->n_bytes)) {
2364 ds_put_cstr(s, "ANY");
2366 } else if (is_all_ones(mask, mf->n_bytes)) {
2371 switch (mf->string) {
2372 case MFS_OFP_PORT_OXM:
2375 ofputil_port_from_ofp11(value->be32, &port);
2376 ofputil_format_port(port, s);
2382 ofputil_format_port(u16_to_ofp(ntohs(value->be16)), s);
2387 case MFS_HEXADECIMAL:
2388 mf_format_integer_string(mf, (uint8_t *) value, (uint8_t *) mask, s);
2392 mf_format_ct_state_string(value->be32,
2393 mask ? mask->be32 : OVS_BE32_MAX, s);
2397 eth_format_masked(value->mac, mask ? &mask->mac : NULL, s);
2401 ip_format_masked(value->be32, mask ? mask->be32 : OVS_BE32_MAX, s);
2405 print_ipv6_masked(s, &value->ipv6, mask ? &mask->ipv6 : NULL);
2409 mf_format_frag_string(value->u8, mask ? mask->u8 : UINT8_MAX, s);
2413 mf_format_tnl_flags_string(value->be16,
2414 mask ? mask->be16 : OVS_BE16_MAX, s);
2418 mf_format_tcp_flags_string(value->be16,
2419 mask ? mask->be16 : OVS_BE16_MAX, s);
2427 /* Makes subfield 'sf' within 'flow' exactly match the 'sf->n_bits'
2428 * least-significant bits in 'x'.
2431 mf_write_subfield_flow(const struct mf_subfield *sf,
2432 const union mf_subvalue *x, struct flow *flow)
2434 const struct mf_field *field = sf->field;
2435 union mf_value value;
2437 mf_get_value(field, flow, &value);
2438 bitwise_copy(x, sizeof *x, 0, &value, field->n_bytes,
2439 sf->ofs, sf->n_bits);
2440 mf_set_flow_value(field, &value, flow);
2443 /* Makes subfield 'sf' within 'match' exactly match the 'sf->n_bits'
2444 * least-significant bits in 'x'.
2447 mf_write_subfield(const struct mf_subfield *sf, const union mf_subvalue *x,
2448 struct match *match)
2450 const struct mf_field *field = sf->field;
2451 union mf_value value, mask;
2453 mf_get(field, match, &value, &mask);
2454 bitwise_copy(x, sizeof *x, 0, &value, field->n_bytes, sf->ofs, sf->n_bits);
2455 bitwise_one ( &mask, field->n_bytes, sf->ofs, sf->n_bits);
2456 mf_set(field, &value, &mask, match, NULL);
2459 /* 'v' and 'm' correspond to values of 'field'. This function copies them into
2460 * 'match' in the correspond positions. */
2462 mf_mask_subfield(const struct mf_field *field,
2463 const union mf_subvalue *v,
2464 const union mf_subvalue *m,
2465 struct match *match)
2467 union mf_value value, mask;
2469 mf_get(field, match, &value, &mask);
2470 bitwise_copy(v, sizeof *v, 0, &value, field->n_bytes, 0, field->n_bits);
2471 bitwise_copy(m, sizeof *m, 0, &mask, field->n_bytes, 0, field->n_bits);
2472 mf_set(field, &value, &mask, match, NULL);
2475 /* Initializes 'x' to the value of 'sf' within 'flow'. 'sf' must be valid for
2476 * reading 'flow', e.g. as checked by mf_check_src(). */
2478 mf_read_subfield(const struct mf_subfield *sf, const struct flow *flow,
2479 union mf_subvalue *x)
2481 union mf_value value;
2483 mf_get_value(sf->field, flow, &value);
2485 memset(x, 0, sizeof *x);
2486 bitwise_copy(&value, sf->field->n_bytes, sf->ofs,
2491 /* Returns the value of 'sf' within 'flow'. 'sf' must be valid for reading
2492 * 'flow', e.g. as checked by mf_check_src() and sf->n_bits must be 64 or
2495 mf_get_subfield(const struct mf_subfield *sf, const struct flow *flow)
2497 union mf_value value;
2499 mf_get_value(sf->field, flow, &value);
2500 return bitwise_get(&value, sf->field->n_bytes, sf->ofs, sf->n_bits);
2504 mf_format_subvalue(const union mf_subvalue *subvalue, struct ds *s)
2506 ds_put_hex(s, subvalue->u8, sizeof subvalue->u8);
2510 field_array_set(enum mf_field_id id, const union mf_value *value,
2511 struct field_array *fa)
2513 ovs_assert(id < MFF_N_IDS);
2514 bitmap_set1(fa->used.bm, id);
2515 fa->value[id] = *value;