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 "openvswitch/meta-flow.h"
23 #include <netinet/icmp6.h>
24 #include <netinet/ip6.h>
26 #include "classifier.h"
27 #include "openvswitch/dynamic-string.h"
29 #include "openvswitch/ofp-util.h"
30 #include "ovs-thread.h"
34 #include "socket-util.h"
35 #include "tun-metadata.h"
36 #include "unaligned.h"
38 #include "openvswitch/ofp-errors.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);
241 ovs_u128 value = flow_get_xxreg(&wc->masks, mf->id - MFF_XXREG0);
242 return ovs_u128_is_zero(value);
244 case MFF_ACTSET_OUTPUT:
245 return !wc->masks.actset_output;
248 return eth_addr_is_zero(wc->masks.dl_src);
250 return eth_addr_is_zero(wc->masks.dl_dst);
252 return !wc->masks.dl_type;
256 return eth_addr_is_zero(wc->masks.arp_sha);
260 return eth_addr_is_zero(wc->masks.arp_tha);
263 return !wc->masks.vlan_tci;
265 return !(wc->masks.vlan_tci & htons(VLAN_VID_MASK));
267 return !(wc->masks.vlan_tci & htons(VLAN_VID_MASK | VLAN_CFI));
268 case MFF_DL_VLAN_PCP:
270 return !(wc->masks.vlan_tci & htons(VLAN_PCP_MASK));
273 return !(wc->masks.mpls_lse[0] & htonl(MPLS_LABEL_MASK));
275 return !(wc->masks.mpls_lse[0] & htonl(MPLS_TC_MASK));
277 return !(wc->masks.mpls_lse[0] & htonl(MPLS_BOS_MASK));
279 return !(wc->masks.mpls_lse[0] & htonl(MPLS_TTL_MASK));
282 return !wc->masks.nw_src;
284 return !wc->masks.nw_dst;
287 return ipv6_mask_is_any(&wc->masks.ipv6_src);
289 return ipv6_mask_is_any(&wc->masks.ipv6_dst);
292 return !wc->masks.ipv6_label;
295 return !wc->masks.nw_proto;
297 case MFF_IP_DSCP_SHIFTED:
298 return !(wc->masks.nw_tos & IP_DSCP_MASK);
300 return !(wc->masks.nw_tos & IP_ECN_MASK);
302 return !wc->masks.nw_ttl;
305 return ipv6_mask_is_any(&wc->masks.nd_target);
308 return !(wc->masks.nw_frag & FLOW_NW_FRAG_MASK);
311 return !wc->masks.nw_proto;
313 return !wc->masks.nw_src;
315 return !wc->masks.nw_dst;
320 case MFF_ICMPV4_TYPE:
321 case MFF_ICMPV6_TYPE:
322 return !wc->masks.tp_src;
326 case MFF_ICMPV4_CODE:
327 case MFF_ICMPV6_CODE:
328 return !wc->masks.tp_dst;
330 return !wc->masks.tcp_flags;
338 /* Initializes 'mask' with the wildcard bit pattern for field 'mf' within 'wc'.
339 * Each bit in 'mask' will be set to 1 if the bit is significant for matching
340 * purposes, or to 0 if it is wildcarded.
342 * The caller is responsible for ensuring that 'wc' corresponds to a flow that
343 * meets 'mf''s prerequisites. */
345 mf_get_mask(const struct mf_field *mf, const struct flow_wildcards *wc,
346 union mf_value *mask)
348 mf_get_value(mf, &wc->masks, mask);
351 /* Tests whether 'mask' is a valid wildcard bit pattern for 'mf'. Returns true
352 * if the mask is valid, false otherwise. */
354 mf_is_mask_valid(const struct mf_field *mf, const union mf_value *mask)
356 switch (mf->maskable) {
358 return (is_all_zeros(mask, mf->n_bytes) ||
359 is_all_ones(mask, mf->n_bytes));
368 /* Returns true if 'flow' meets the prerequisites for 'mf', false otherwise. */
370 mf_are_prereqs_ok(const struct mf_field *mf, const struct flow *flow)
372 switch (mf->prereqs) {
377 return (flow->dl_type == htons(ETH_TYPE_ARP) ||
378 flow->dl_type == htons(ETH_TYPE_RARP));
380 return flow->dl_type == htons(ETH_TYPE_IP);
382 return flow->dl_type == htons(ETH_TYPE_IPV6);
384 return (flow->vlan_tci & htons(VLAN_CFI)) != 0;
386 return eth_type_mpls(flow->dl_type);
388 return is_ip_any(flow);
391 return is_ip_any(flow) && flow->nw_proto == IPPROTO_TCP
392 && !(flow->nw_frag & FLOW_NW_FRAG_LATER);
394 return is_ip_any(flow) && flow->nw_proto == IPPROTO_UDP
395 && !(flow->nw_frag & FLOW_NW_FRAG_LATER);
397 return is_ip_any(flow) && flow->nw_proto == IPPROTO_SCTP
398 && !(flow->nw_frag & FLOW_NW_FRAG_LATER);
400 return is_icmpv4(flow, NULL);
402 return is_icmpv6(flow, NULL);
405 return is_nd(flow, NULL);
407 return is_nd(flow, NULL) && flow->tp_src == htons(ND_NEIGHBOR_SOLICIT);
409 return is_nd(flow, NULL) && flow->tp_src == htons(ND_NEIGHBOR_ADVERT);
415 /* Set field and it's prerequisities in the mask.
416 * This is only ever called for writeable 'mf's, but we do not make the
417 * distinction here. */
419 mf_mask_field_and_prereqs(const struct mf_field *mf, struct flow_wildcards *wc)
421 mf_mask_field_and_prereqs__(mf, &exact_match_mask, wc);
425 mf_mask_field_and_prereqs__(const struct mf_field *mf,
426 const union mf_value *mask,
427 struct flow_wildcards *wc)
429 mf_set_flow_value_masked(mf, &exact_match_mask, mask, &wc->masks);
431 switch (mf->prereqs) {
435 WC_MASK_FIELD(wc, tp_src);
436 WC_MASK_FIELD(wc, tp_dst);
443 /* nw_frag always unwildcarded. */
444 WC_MASK_FIELD(wc, nw_proto);
451 /* dl_type always unwildcarded. */
454 WC_MASK_FIELD_MASK(wc, vlan_tci, htons(VLAN_CFI));
461 /* Set bits of 'bm' corresponding to the field 'mf' and it's prerequisities. */
463 mf_bitmap_set_field_and_prereqs(const struct mf_field *mf, struct mf_bitmap *bm)
465 bitmap_set1(bm->bm, mf->id);
467 switch (mf->prereqs) {
471 bitmap_set1(bm->bm, MFF_TCP_SRC);
472 bitmap_set1(bm->bm, MFF_TCP_DST);
479 /* nw_frag always unwildcarded. */
480 bitmap_set1(bm->bm, MFF_IP_PROTO);
487 bitmap_set1(bm->bm, MFF_ETH_TYPE);
490 bitmap_set1(bm->bm, MFF_VLAN_TCI);
497 /* Returns true if 'value' may be a valid value *as part of a masked match*,
500 * A value is not rejected just because it is not valid for the field in
501 * question, but only if it doesn't make sense to test the bits in question at
502 * all. For example, the MFF_VLAN_TCI field will never have a nonzero value
503 * without the VLAN_CFI bit being set, but we can't reject those values because
504 * it is still legitimate to test just for those bits (see the documentation
505 * for NXM_OF_VLAN_TCI in nicira-ext.h). On the other hand, there is never a
506 * reason to set the low bit of MFF_IP_DSCP to 1, so we reject that. */
508 mf_is_value_valid(const struct mf_field *mf, const union mf_value *value)
517 case MFF_TUN_IPV6_SRC:
518 case MFF_TUN_IPV6_DST:
522 case MFF_TUN_GBP_FLAGS:
523 CASE_MFF_TUN_METADATA:
526 case MFF_SKB_PRIORITY:
555 case MFF_ICMPV4_TYPE:
556 case MFF_ICMPV4_CODE:
557 case MFF_ICMPV6_TYPE:
558 case MFF_ICMPV6_CODE:
564 case MFF_IN_PORT_OXM:
565 case MFF_ACTSET_OUTPUT: {
567 return !ofputil_port_from_ofp11(value->be32, &port);
571 return !(value->u8 & ~IP_DSCP_MASK);
572 case MFF_IP_DSCP_SHIFTED:
573 return !(value->u8 & (~IP_DSCP_MASK >> 2));
575 return !(value->u8 & ~IP_ECN_MASK);
577 return !(value->u8 & ~FLOW_NW_FRAG_MASK);
579 return !(value->be16 & ~htons(0x0fff));
582 return !(value->be16 & htons(0xff00));
585 return !(value->be16 & htons(VLAN_CFI | VLAN_PCP_MASK));
587 return !(value->be16 & htons(VLAN_PCP_MASK));
589 case MFF_DL_VLAN_PCP:
591 return !(value->u8 & ~(VLAN_PCP_MASK >> VLAN_PCP_SHIFT));
594 return !(value->be32 & ~htonl(IPV6_LABEL_MASK));
597 return !(value->be32 & ~htonl(MPLS_LABEL_MASK >> MPLS_LABEL_SHIFT));
600 return !(value->u8 & ~(MPLS_TC_MASK >> MPLS_TC_SHIFT));
603 return !(value->u8 & ~(MPLS_BOS_MASK >> MPLS_BOS_SHIFT));
606 return !(value->be16 & ~htons(FLOW_TNL_PUB_F_MASK));
609 return !(value->be32 & ~htonl(CS_SUPPORTED_MASK));
617 /* Copies the value of field 'mf' from 'flow' into 'value'. The caller is
618 * responsible for ensuring that 'flow' meets 'mf''s prerequisites. */
620 mf_get_value(const struct mf_field *mf, const struct flow *flow,
621 union mf_value *value)
625 value->be32 = htonl(flow->dp_hash);
628 value->be32 = htonl(flow->recirc_id);
631 value->be32 = htonl(flow->conj_id);
634 value->be64 = flow->tunnel.tun_id;
637 value->be32 = flow->tunnel.ip_src;
640 value->be32 = flow->tunnel.ip_dst;
642 case MFF_TUN_IPV6_SRC:
643 value->ipv6 = flow->tunnel.ipv6_src;
645 case MFF_TUN_IPV6_DST:
646 value->ipv6 = flow->tunnel.ipv6_dst;
649 value->be16 = htons(flow->tunnel.flags & FLOW_TNL_PUB_F_MASK);
652 value->be16 = flow->tunnel.gbp_id;
654 case MFF_TUN_GBP_FLAGS:
655 value->u8 = flow->tunnel.gbp_flags;
658 value->u8 = flow->tunnel.ip_ttl;
661 value->u8 = flow->tunnel.ip_tos;
663 CASE_MFF_TUN_METADATA:
664 tun_metadata_read(&flow->tunnel, mf, value);
668 value->be64 = flow->metadata;
672 value->be16 = htons(ofp_to_u16(flow->in_port.ofp_port));
674 case MFF_IN_PORT_OXM:
675 value->be32 = ofputil_port_to_ofp11(flow->in_port.ofp_port);
677 case MFF_ACTSET_OUTPUT:
678 value->be32 = ofputil_port_to_ofp11(flow->actset_output);
681 case MFF_SKB_PRIORITY:
682 value->be32 = htonl(flow->skb_priority);
686 value->be32 = htonl(flow->pkt_mark);
690 value->be32 = htonl(flow->ct_state);
694 value->be16 = htons(flow->ct_zone);
698 value->be32 = htonl(flow->ct_mark);
702 value->be128 = hton128(flow->ct_label);
706 value->be32 = htonl(flow->regs[mf->id - MFF_REG0]);
710 value->be64 = htonll(flow_get_xreg(flow, mf->id - MFF_XREG0));
714 value->be128 = hton128(flow_get_xxreg(flow, mf->id - MFF_XXREG0));
718 value->mac = flow->dl_src;
722 value->mac = flow->dl_dst;
726 value->be16 = flow->dl_type;
730 value->be16 = flow->vlan_tci;
734 value->be16 = flow->vlan_tci & htons(VLAN_VID_MASK);
737 value->be16 = flow->vlan_tci & htons(VLAN_VID_MASK | VLAN_CFI);
740 case MFF_DL_VLAN_PCP:
742 value->u8 = vlan_tci_to_pcp(flow->vlan_tci);
746 value->be32 = htonl(mpls_lse_to_label(flow->mpls_lse[0]));
750 value->u8 = mpls_lse_to_tc(flow->mpls_lse[0]);
754 value->u8 = mpls_lse_to_bos(flow->mpls_lse[0]);
758 value->u8 = mpls_lse_to_ttl(flow->mpls_lse[0]);
762 value->be32 = flow->nw_src;
766 value->be32 = flow->nw_dst;
770 value->ipv6 = flow->ipv6_src;
774 value->ipv6 = flow->ipv6_dst;
778 value->be32 = flow->ipv6_label;
782 value->u8 = flow->nw_proto;
786 value->u8 = flow->nw_tos & IP_DSCP_MASK;
789 case MFF_IP_DSCP_SHIFTED:
790 value->u8 = flow->nw_tos >> 2;
794 value->u8 = flow->nw_tos & IP_ECN_MASK;
798 value->u8 = flow->nw_ttl;
802 value->u8 = flow->nw_frag;
806 value->be16 = htons(flow->nw_proto);
810 value->be32 = flow->nw_src;
814 value->be32 = flow->nw_dst;
819 value->mac = flow->arp_sha;
824 value->mac = flow->arp_tha;
830 value->be16 = flow->tp_src;
836 value->be16 = flow->tp_dst;
840 value->be16 = flow->tcp_flags;
843 case MFF_ICMPV4_TYPE:
844 case MFF_ICMPV6_TYPE:
845 value->u8 = ntohs(flow->tp_src);
848 case MFF_ICMPV4_CODE:
849 case MFF_ICMPV6_CODE:
850 value->u8 = ntohs(flow->tp_dst);
854 value->ipv6 = flow->nd_target;
863 /* Makes 'match' match field 'mf' exactly, with the value matched taken from
864 * 'value'. The caller is responsible for ensuring that 'match' meets 'mf''s
867 * If non-NULL, 'err_str' returns a malloc'ed string describing any errors
868 * with the request or NULL if there is no error. The caller is reponsible
869 * for freeing the string. */
871 mf_set_value(const struct mf_field *mf,
872 const union mf_value *value, struct match *match, char **err_str)
880 match_set_dp_hash(match, ntohl(value->be32));
883 match_set_recirc_id(match, ntohl(value->be32));
886 match_set_conj_id(match, ntohl(value->be32));
889 match_set_tun_id(match, value->be64);
892 match_set_tun_src(match, value->be32);
895 match_set_tun_dst(match, value->be32);
897 case MFF_TUN_IPV6_SRC:
898 match_set_tun_ipv6_src(match, &value->ipv6);
900 case MFF_TUN_IPV6_DST:
901 match_set_tun_ipv6_dst(match, &value->ipv6);
904 match_set_tun_flags(match, ntohs(value->be16));
907 match_set_tun_gbp_id(match, value->be16);
909 case MFF_TUN_GBP_FLAGS:
910 match_set_tun_gbp_flags(match, value->u8);
913 match_set_tun_tos(match, value->u8);
916 match_set_tun_ttl(match, value->u8);
918 CASE_MFF_TUN_METADATA:
919 tun_metadata_set_match(mf, value, NULL, match, err_str);
923 match_set_metadata(match, value->be64);
927 match_set_in_port(match, u16_to_ofp(ntohs(value->be16)));
930 case MFF_IN_PORT_OXM: {
932 ofputil_port_from_ofp11(value->be32, &port);
933 match_set_in_port(match, port);
936 case MFF_ACTSET_OUTPUT: {
938 ofputil_port_from_ofp11(value->be32, &port);
939 match_set_actset_output(match, port);
943 case MFF_SKB_PRIORITY:
944 match_set_skb_priority(match, ntohl(value->be32));
948 match_set_pkt_mark(match, ntohl(value->be32));
952 match_set_ct_state(match, ntohl(value->be32));
956 match_set_ct_zone(match, ntohs(value->be16));
960 match_set_ct_mark(match, ntohl(value->be32));
964 match_set_ct_label(match, ntoh128(value->be128));
968 match_set_reg(match, mf->id - MFF_REG0, ntohl(value->be32));
972 match_set_xreg(match, mf->id - MFF_XREG0, ntohll(value->be64));
976 match_set_xxreg(match, mf->id - MFF_XXREG0, ntoh128(value->be128));
980 match_set_dl_src(match, value->mac);
984 match_set_dl_dst(match, value->mac);
988 match_set_dl_type(match, value->be16);
992 match_set_dl_tci(match, value->be16);
996 match_set_dl_vlan(match, value->be16);
999 match_set_vlan_vid(match, value->be16);
1002 case MFF_DL_VLAN_PCP:
1004 match_set_dl_vlan_pcp(match, value->u8);
1007 case MFF_MPLS_LABEL:
1008 match_set_mpls_label(match, 0, value->be32);
1012 match_set_mpls_tc(match, 0, value->u8);
1016 match_set_mpls_bos(match, 0, value->u8);
1020 match_set_mpls_ttl(match, 0, value->u8);
1024 match_set_nw_src(match, value->be32);
1028 match_set_nw_dst(match, value->be32);
1032 match_set_ipv6_src(match, &value->ipv6);
1036 match_set_ipv6_dst(match, &value->ipv6);
1039 case MFF_IPV6_LABEL:
1040 match_set_ipv6_label(match, value->be32);
1044 match_set_nw_proto(match, value->u8);
1048 match_set_nw_dscp(match, value->u8);
1051 case MFF_IP_DSCP_SHIFTED:
1052 match_set_nw_dscp(match, value->u8 << 2);
1056 match_set_nw_ecn(match, value->u8);
1060 match_set_nw_ttl(match, value->u8);
1064 match_set_nw_frag(match, value->u8);
1068 match_set_nw_proto(match, ntohs(value->be16));
1072 match_set_nw_src(match, value->be32);
1076 match_set_nw_dst(match, value->be32);
1081 match_set_arp_sha(match, value->mac);
1086 match_set_arp_tha(match, value->mac);
1092 match_set_tp_src(match, value->be16);
1098 match_set_tp_dst(match, value->be16);
1102 match_set_tcp_flags(match, value->be16);
1105 case MFF_ICMPV4_TYPE:
1106 case MFF_ICMPV6_TYPE:
1107 match_set_icmp_type(match, value->u8);
1110 case MFF_ICMPV4_CODE:
1111 case MFF_ICMPV6_CODE:
1112 match_set_icmp_code(match, value->u8);
1116 match_set_nd_target(match, &value->ipv6);
1125 /* Unwildcard 'mask' member field described by 'mf'. The caller is
1126 * responsible for ensuring that 'mask' meets 'mf''s prerequisites. */
1128 mf_mask_field(const struct mf_field *mf, struct flow *mask)
1130 /* For MFF_DL_VLAN, we cannot send a all 1's to flow_set_dl_vlan()
1131 * as that will be considered as OFP10_VLAN_NONE. So consider it as a
1132 * special case. For the rest, calling mf_set_flow_value() is good
1134 if (mf->id == MFF_DL_VLAN) {
1135 flow_set_dl_vlan(mask, htons(VLAN_VID_MASK));
1137 mf_set_flow_value(mf, &exact_match_mask, mask);
1142 field_len(const struct mf_field *mf, const union mf_value *value_)
1144 const uint8_t *value = &value_->u8;
1147 if (!mf->variable_len) {
1155 for (i = 0; i < mf->n_bytes; i++) {
1156 if (value[i] != 0) {
1161 return mf->n_bytes - i;
1164 /* Returns the effective length of the field. For fixed length fields,
1165 * this is just the defined length. For variable length fields, it is
1166 * the minimum size encoding that retains the same meaning (i.e.
1167 * discarding leading zeros).
1169 * 'is_masked' returns (if non-NULL) whether the original contained
1170 * a mask. Otherwise, a mask that is the same length as the value
1171 * might be misinterpreted as an exact match. */
1173 mf_field_len(const struct mf_field *mf, const union mf_value *value,
1174 const union mf_value *mask, bool *is_masked_)
1177 bool is_masked = mask && !is_all_ones(mask, mf->n_bytes);
1179 len = field_len(mf, value);
1181 mask_len = field_len(mf, mask);
1182 len = MAX(len, mask_len);
1186 *is_masked_ = is_masked;
1192 /* Sets 'flow' member field described by 'mf' to 'value'. The caller is
1193 * responsible for ensuring that 'flow' meets 'mf''s prerequisites.*/
1195 mf_set_flow_value(const struct mf_field *mf,
1196 const union mf_value *value, struct flow *flow)
1200 flow->dp_hash = ntohl(value->be32);
1203 flow->recirc_id = ntohl(value->be32);
1206 flow->conj_id = ntohl(value->be32);
1209 flow->tunnel.tun_id = value->be64;
1212 flow->tunnel.ip_src = value->be32;
1215 flow->tunnel.ip_dst = value->be32;
1217 case MFF_TUN_IPV6_SRC:
1218 flow->tunnel.ipv6_src = value->ipv6;
1220 case MFF_TUN_IPV6_DST:
1221 flow->tunnel.ipv6_dst = value->ipv6;
1224 flow->tunnel.flags = (flow->tunnel.flags & ~FLOW_TNL_PUB_F_MASK) |
1227 case MFF_TUN_GBP_ID:
1228 flow->tunnel.gbp_id = value->be16;
1230 case MFF_TUN_GBP_FLAGS:
1231 flow->tunnel.gbp_flags = value->u8;
1234 flow->tunnel.ip_tos = value->u8;
1237 flow->tunnel.ip_ttl = value->u8;
1239 CASE_MFF_TUN_METADATA:
1240 tun_metadata_write(&flow->tunnel, mf, value);
1243 flow->metadata = value->be64;
1247 flow->in_port.ofp_port = u16_to_ofp(ntohs(value->be16));
1250 case MFF_IN_PORT_OXM:
1251 ofputil_port_from_ofp11(value->be32, &flow->in_port.ofp_port);
1253 case MFF_ACTSET_OUTPUT:
1254 ofputil_port_from_ofp11(value->be32, &flow->actset_output);
1257 case MFF_SKB_PRIORITY:
1258 flow->skb_priority = ntohl(value->be32);
1262 flow->pkt_mark = ntohl(value->be32);
1266 flow->ct_state = ntohl(value->be32);
1270 flow->ct_zone = ntohs(value->be16);
1274 flow->ct_mark = ntohl(value->be32);
1278 flow->ct_label = ntoh128(value->be128);
1282 flow->regs[mf->id - MFF_REG0] = ntohl(value->be32);
1286 flow_set_xreg(flow, mf->id - MFF_XREG0, ntohll(value->be64));
1290 flow_set_xxreg(flow, mf->id - MFF_XXREG0, ntoh128(value->be128));
1294 flow->dl_src = value->mac;
1298 flow->dl_dst = value->mac;
1302 flow->dl_type = value->be16;
1306 flow->vlan_tci = value->be16;
1310 flow_set_dl_vlan(flow, value->be16);
1313 flow_set_vlan_vid(flow, value->be16);
1316 case MFF_DL_VLAN_PCP:
1318 flow_set_vlan_pcp(flow, value->u8);
1321 case MFF_MPLS_LABEL:
1322 flow_set_mpls_label(flow, 0, value->be32);
1326 flow_set_mpls_tc(flow, 0, value->u8);
1330 flow_set_mpls_bos(flow, 0, value->u8);
1334 flow_set_mpls_ttl(flow, 0, value->u8);
1338 flow->nw_src = value->be32;
1342 flow->nw_dst = value->be32;
1346 flow->ipv6_src = value->ipv6;
1350 flow->ipv6_dst = value->ipv6;
1353 case MFF_IPV6_LABEL:
1354 flow->ipv6_label = value->be32 & htonl(IPV6_LABEL_MASK);
1358 flow->nw_proto = value->u8;
1362 flow->nw_tos &= ~IP_DSCP_MASK;
1363 flow->nw_tos |= value->u8 & IP_DSCP_MASK;
1366 case MFF_IP_DSCP_SHIFTED:
1367 flow->nw_tos &= ~IP_DSCP_MASK;
1368 flow->nw_tos |= value->u8 << 2;
1372 flow->nw_tos &= ~IP_ECN_MASK;
1373 flow->nw_tos |= value->u8 & IP_ECN_MASK;
1377 flow->nw_ttl = value->u8;
1381 flow->nw_frag = value->u8 & FLOW_NW_FRAG_MASK;
1385 flow->nw_proto = ntohs(value->be16);
1389 flow->nw_src = value->be32;
1393 flow->nw_dst = value->be32;
1398 flow->arp_sha = value->mac;
1403 flow->arp_tha = value->mac;
1409 flow->tp_src = value->be16;
1415 flow->tp_dst = value->be16;
1419 flow->tcp_flags = value->be16;
1422 case MFF_ICMPV4_TYPE:
1423 case MFF_ICMPV6_TYPE:
1424 flow->tp_src = htons(value->u8);
1427 case MFF_ICMPV4_CODE:
1428 case MFF_ICMPV6_CODE:
1429 flow->tp_dst = htons(value->u8);
1433 flow->nd_target = value->ipv6;
1442 /* Consider each of 'src', 'mask', and 'dst' as if they were arrays of 8*n
1443 * bits. Then, for each 0 <= i < 8 * n such that mask[i] == 1, sets dst[i] =
1446 apply_mask(const uint8_t *src, const uint8_t *mask, uint8_t *dst, size_t n)
1450 for (i = 0; i < n; i++) {
1451 dst[i] = (src[i] & mask[i]) | (dst[i] & ~mask[i]);
1455 /* Sets 'flow' member field described by 'field' to 'value', except that bits
1456 * for which 'mask' has a 0-bit keep their existing values. The caller is
1457 * responsible for ensuring that 'flow' meets 'field''s prerequisites.*/
1459 mf_set_flow_value_masked(const struct mf_field *field,
1460 const union mf_value *value,
1461 const union mf_value *mask,
1466 mf_get_value(field, flow, &tmp);
1467 apply_mask((const uint8_t *) value, (const uint8_t *) mask,
1468 (uint8_t *) &tmp, field->n_bytes);
1469 mf_set_flow_value(field, &tmp, flow);
1473 mf_is_tun_metadata(const struct mf_field *mf)
1475 return mf->id >= MFF_TUN_METADATA0 &&
1476 mf->id < MFF_TUN_METADATA0 + TUN_METADATA_NUM_OPTS;
1479 /* Returns true if 'mf' has previously been set in 'flow', false if
1480 * it contains a non-default value.
1482 * The caller is responsible for ensuring that 'flow' meets 'mf''s
1485 mf_is_set(const struct mf_field *mf, const struct flow *flow)
1487 if (!mf_is_tun_metadata(mf)) {
1488 union mf_value value;
1490 mf_get_value(mf, flow, &value);
1491 return !is_all_zeros(&value, mf->n_bytes);
1493 return ULLONG_GET(flow->tunnel.metadata.present.map,
1494 mf->id - MFF_TUN_METADATA0);
1498 /* Makes 'match' wildcard field 'mf'.
1500 * The caller is responsible for ensuring that 'match' meets 'mf''s
1503 * If non-NULL, 'err_str' returns a malloc'ed string describing any errors
1504 * with the request or NULL if there is no error. The caller is reponsible
1505 * for freeing the string. */
1507 mf_set_wild(const struct mf_field *mf, struct match *match, char **err_str)
1515 match->flow.dp_hash = 0;
1516 match->wc.masks.dp_hash = 0;
1519 match->flow.recirc_id = 0;
1520 match->wc.masks.recirc_id = 0;
1523 match->flow.conj_id = 0;
1524 match->wc.masks.conj_id = 0;
1527 match_set_tun_id_masked(match, htonll(0), htonll(0));
1530 match_set_tun_src_masked(match, htonl(0), htonl(0));
1533 match_set_tun_dst_masked(match, htonl(0), htonl(0));
1535 case MFF_TUN_IPV6_SRC:
1536 memset(&match->wc.masks.tunnel.ipv6_src, 0,
1537 sizeof match->wc.masks.tunnel.ipv6_src);
1538 memset(&match->flow.tunnel.ipv6_src, 0,
1539 sizeof match->flow.tunnel.ipv6_src);
1541 case MFF_TUN_IPV6_DST:
1542 memset(&match->wc.masks.tunnel.ipv6_dst, 0,
1543 sizeof match->wc.masks.tunnel.ipv6_dst);
1544 memset(&match->flow.tunnel.ipv6_dst, 0,
1545 sizeof match->flow.tunnel.ipv6_dst);
1548 match_set_tun_flags_masked(match, 0, 0);
1550 case MFF_TUN_GBP_ID:
1551 match_set_tun_gbp_id_masked(match, 0, 0);
1553 case MFF_TUN_GBP_FLAGS:
1554 match_set_tun_gbp_flags_masked(match, 0, 0);
1557 match_set_tun_tos_masked(match, 0, 0);
1560 match_set_tun_ttl_masked(match, 0, 0);
1562 CASE_MFF_TUN_METADATA:
1563 tun_metadata_set_match(mf, NULL, NULL, match, err_str);
1567 match_set_metadata_masked(match, htonll(0), htonll(0));
1571 case MFF_IN_PORT_OXM:
1572 match->flow.in_port.ofp_port = 0;
1573 match->wc.masks.in_port.ofp_port = 0;
1575 case MFF_ACTSET_OUTPUT:
1576 match->flow.actset_output = 0;
1577 match->wc.masks.actset_output = 0;
1580 case MFF_SKB_PRIORITY:
1581 match->flow.skb_priority = 0;
1582 match->wc.masks.skb_priority = 0;
1586 match->flow.pkt_mark = 0;
1587 match->wc.masks.pkt_mark = 0;
1591 match->flow.ct_state = 0;
1592 match->wc.masks.ct_state = 0;
1596 match->flow.ct_zone = 0;
1597 match->wc.masks.ct_zone = 0;
1601 match->flow.ct_mark = 0;
1602 match->wc.masks.ct_mark = 0;
1606 memset(&match->flow.ct_label, 0, sizeof(match->flow.ct_label));
1607 memset(&match->wc.masks.ct_label, 0, sizeof(match->wc.masks.ct_label));
1611 match_set_reg_masked(match, mf->id - MFF_REG0, 0, 0);
1615 match_set_xreg_masked(match, mf->id - MFF_XREG0, 0, 0);
1619 match_set_xxreg_masked(match, mf->id - MFF_XXREG0, OVS_U128_ZERO,
1625 match->flow.dl_src = eth_addr_zero;
1626 match->wc.masks.dl_src = eth_addr_zero;
1630 match->flow.dl_dst = eth_addr_zero;
1631 match->wc.masks.dl_dst = eth_addr_zero;
1635 match->flow.dl_type = htons(0);
1636 match->wc.masks.dl_type = htons(0);
1640 match_set_dl_tci_masked(match, htons(0), htons(0));
1645 match_set_any_vid(match);
1648 case MFF_DL_VLAN_PCP:
1650 match_set_any_pcp(match);
1653 case MFF_MPLS_LABEL:
1654 match_set_any_mpls_label(match, 0);
1658 match_set_any_mpls_tc(match, 0);
1662 match_set_any_mpls_bos(match, 0);
1666 match_set_any_mpls_ttl(match, 0);
1671 match_set_nw_src_masked(match, htonl(0), htonl(0));
1676 match_set_nw_dst_masked(match, htonl(0), htonl(0));
1680 memset(&match->wc.masks.ipv6_src, 0, sizeof match->wc.masks.ipv6_src);
1681 memset(&match->flow.ipv6_src, 0, sizeof match->flow.ipv6_src);
1685 memset(&match->wc.masks.ipv6_dst, 0, sizeof match->wc.masks.ipv6_dst);
1686 memset(&match->flow.ipv6_dst, 0, sizeof match->flow.ipv6_dst);
1689 case MFF_IPV6_LABEL:
1690 match->wc.masks.ipv6_label = htonl(0);
1691 match->flow.ipv6_label = htonl(0);
1695 match->wc.masks.nw_proto = 0;
1696 match->flow.nw_proto = 0;
1700 case MFF_IP_DSCP_SHIFTED:
1701 match->wc.masks.nw_tos &= ~IP_DSCP_MASK;
1702 match->flow.nw_tos &= ~IP_DSCP_MASK;
1706 match->wc.masks.nw_tos &= ~IP_ECN_MASK;
1707 match->flow.nw_tos &= ~IP_ECN_MASK;
1711 match->wc.masks.nw_ttl = 0;
1712 match->flow.nw_ttl = 0;
1716 match->wc.masks.nw_frag &= ~FLOW_NW_FRAG_MASK;
1717 match->flow.nw_frag &= ~FLOW_NW_FRAG_MASK;
1721 match->wc.masks.nw_proto = 0;
1722 match->flow.nw_proto = 0;
1727 match->flow.arp_sha = eth_addr_zero;
1728 match->wc.masks.arp_sha = eth_addr_zero;
1733 match->flow.arp_tha = eth_addr_zero;
1734 match->wc.masks.arp_tha = eth_addr_zero;
1740 case MFF_ICMPV4_TYPE:
1741 case MFF_ICMPV6_TYPE:
1742 match->wc.masks.tp_src = htons(0);
1743 match->flow.tp_src = htons(0);
1749 case MFF_ICMPV4_CODE:
1750 case MFF_ICMPV6_CODE:
1751 match->wc.masks.tp_dst = htons(0);
1752 match->flow.tp_dst = htons(0);
1756 match->wc.masks.tcp_flags = htons(0);
1757 match->flow.tcp_flags = htons(0);
1761 memset(&match->wc.masks.nd_target, 0,
1762 sizeof match->wc.masks.nd_target);
1763 memset(&match->flow.nd_target, 0, sizeof match->flow.nd_target);
1772 /* Makes 'match' match field 'mf' with the specified 'value' and 'mask'.
1773 * 'value' specifies a value to match and 'mask' specifies a wildcard pattern,
1774 * with a 1-bit indicating that the corresponding value bit must match and a
1775 * 0-bit indicating a don't-care.
1777 * If 'mask' is NULL or points to all-1-bits, then this call is equivalent to
1778 * mf_set_value(mf, value, match). If 'mask' points to all-0-bits, then this
1779 * call is equivalent to mf_set_wild(mf, match).
1781 * 'mask' must be a valid mask for 'mf' (see mf_is_mask_valid()). The caller
1782 * is responsible for ensuring that 'match' meets 'mf''s prerequisites.
1784 * If non-NULL, 'err_str' returns a malloc'ed string describing any errors
1785 * with the request or NULL if there is no error. The caller is reponsible
1786 * for freeing the string.
1788 * Return a set of enum ofputil_protocol bits (as an uint32_t to avoid circular
1789 * dependency on enum ofputil_protocol definition) indicating which OpenFlow
1790 * protocol versions can support this functionality. */
1792 mf_set(const struct mf_field *mf,
1793 const union mf_value *value, const union mf_value *mask,
1794 struct match *match, char **err_str)
1796 if (!mask || is_all_ones(mask, mf->n_bytes)) {
1797 mf_set_value(mf, value, match, err_str);
1798 return mf->usable_protocols_exact;
1799 } else if (is_all_zeros(mask, mf->n_bytes) && !mf_is_tun_metadata(mf)) {
1800 /* Tunnel metadata matches on the existence of the field itself, so
1801 * it still needs to be encoded even if the value is wildcarded. */
1802 mf_set_wild(mf, match, err_str);
1803 return OFPUTIL_P_ANY;
1815 case MFF_IN_PORT_OXM:
1816 case MFF_ACTSET_OUTPUT:
1817 case MFF_SKB_PRIORITY:
1820 case MFF_DL_VLAN_PCP:
1822 case MFF_MPLS_LABEL:
1829 case MFF_IP_DSCP_SHIFTED:
1832 case MFF_ICMPV4_TYPE:
1833 case MFF_ICMPV4_CODE:
1834 case MFF_ICMPV6_TYPE:
1835 case MFF_ICMPV6_CODE:
1836 return OFPUTIL_P_NONE;
1839 match_set_dp_hash_masked(match, ntohl(value->be32), ntohl(mask->be32));
1842 match_set_tun_id_masked(match, value->be64, mask->be64);
1845 match_set_tun_src_masked(match, value->be32, mask->be32);
1848 match_set_tun_dst_masked(match, value->be32, mask->be32);
1850 case MFF_TUN_IPV6_SRC:
1851 match_set_tun_ipv6_src_masked(match, &value->ipv6, &mask->ipv6);
1853 case MFF_TUN_IPV6_DST:
1854 match_set_tun_ipv6_dst_masked(match, &value->ipv6, &mask->ipv6);
1857 match_set_tun_flags_masked(match, ntohs(value->be16), ntohs(mask->be16));
1859 case MFF_TUN_GBP_ID:
1860 match_set_tun_gbp_id_masked(match, value->be16, mask->be16);
1862 case MFF_TUN_GBP_FLAGS:
1863 match_set_tun_gbp_flags_masked(match, value->u8, mask->u8);
1866 match_set_tun_ttl_masked(match, value->u8, mask->u8);
1869 match_set_tun_tos_masked(match, value->u8, mask->u8);
1871 CASE_MFF_TUN_METADATA:
1872 tun_metadata_set_match(mf, value, mask, match, err_str);
1876 match_set_metadata_masked(match, value->be64, mask->be64);
1880 match_set_reg_masked(match, mf->id - MFF_REG0,
1881 ntohl(value->be32), ntohl(mask->be32));
1885 match_set_xreg_masked(match, mf->id - MFF_XREG0,
1886 ntohll(value->be64), ntohll(mask->be64));
1890 match_set_xxreg_masked(match, mf->id - MFF_XXREG0,
1891 ntoh128(value->be128), ntoh128(mask->be128));
1896 match_set_pkt_mark_masked(match, ntohl(value->be32),
1901 match_set_ct_state_masked(match, ntohl(value->be32), ntohl(mask->be32));
1905 match_set_ct_mark_masked(match, ntohl(value->be32), ntohl(mask->be32));
1909 match_set_ct_label_masked(match, ntoh128(value->be128),
1910 mask ? ntoh128(mask->be128) : OVS_U128_MAX);
1914 match_set_dl_dst_masked(match, value->mac, mask->mac);
1918 match_set_dl_src_masked(match, value->mac, mask->mac);
1923 match_set_arp_sha_masked(match, value->mac, mask->mac);
1928 match_set_arp_tha_masked(match, value->mac, mask->mac);
1932 match_set_dl_tci_masked(match, value->be16, mask->be16);
1936 match_set_vlan_vid_masked(match, value->be16, mask->be16);
1940 match_set_nw_src_masked(match, value->be32, mask->be32);
1944 match_set_nw_dst_masked(match, value->be32, mask->be32);
1948 match_set_ipv6_src_masked(match, &value->ipv6, &mask->ipv6);
1952 match_set_ipv6_dst_masked(match, &value->ipv6, &mask->ipv6);
1955 case MFF_IPV6_LABEL:
1956 if ((mask->be32 & htonl(IPV6_LABEL_MASK)) == htonl(IPV6_LABEL_MASK)) {
1957 mf_set_value(mf, value, match, err_str);
1959 match_set_ipv6_label_masked(match, value->be32, mask->be32);
1964 match_set_nd_target_masked(match, &value->ipv6, &mask->ipv6);
1968 match_set_nw_frag_masked(match, value->u8, mask->u8);
1972 match_set_nw_src_masked(match, value->be32, mask->be32);
1976 match_set_nw_dst_masked(match, value->be32, mask->be32);
1982 match_set_tp_src_masked(match, value->be16, mask->be16);
1988 match_set_tp_dst_masked(match, value->be16, mask->be16);
1992 match_set_tcp_flags_masked(match, value->be16, mask->be16);
2000 return ((mf->usable_protocols_bitwise == mf->usable_protocols_cidr
2001 || ip_is_cidr(mask->be32))
2002 ? mf->usable_protocols_cidr
2003 : mf->usable_protocols_bitwise);
2007 mf_check__(const struct mf_subfield *sf, const struct flow *flow,
2011 VLOG_WARN_RL(&rl, "unknown %s field", type);
2012 return OFPERR_OFPBAC_BAD_SET_TYPE;
2013 } else if (!sf->n_bits) {
2014 VLOG_WARN_RL(&rl, "zero bit %s field %s", type, sf->field->name);
2015 return OFPERR_OFPBAC_BAD_SET_LEN;
2016 } else if (sf->ofs >= sf->field->n_bits) {
2017 VLOG_WARN_RL(&rl, "bit offset %d exceeds %d-bit width of %s field %s",
2018 sf->ofs, sf->field->n_bits, type, sf->field->name);
2019 return OFPERR_OFPBAC_BAD_SET_LEN;
2020 } else if (sf->ofs + sf->n_bits > sf->field->n_bits) {
2021 VLOG_WARN_RL(&rl, "bit offset %d and width %d exceeds %d-bit width "
2022 "of %s field %s", sf->ofs, sf->n_bits,
2023 sf->field->n_bits, type, sf->field->name);
2024 return OFPERR_OFPBAC_BAD_SET_LEN;
2025 } else if (flow && !mf_are_prereqs_ok(sf->field, flow)) {
2026 VLOG_WARN_RL(&rl, "%s field %s lacks correct prerequisites",
2027 type, sf->field->name);
2028 return OFPERR_OFPBAC_MATCH_INCONSISTENT;
2034 /* Checks whether 'sf' is valid for reading a subfield out of 'flow'. Returns
2035 * 0 if so, otherwise an OpenFlow error code (e.g. as returned by
2038 mf_check_src(const struct mf_subfield *sf, const struct flow *flow)
2040 return mf_check__(sf, flow, "source");
2043 /* Checks whether 'sf' is valid for writing a subfield into 'flow'. Returns 0
2044 * if so, otherwise an OpenFlow error code (e.g. as returned by
2047 mf_check_dst(const struct mf_subfield *sf, const struct flow *flow)
2049 int error = mf_check__(sf, flow, "destination");
2050 if (!error && !sf->field->writable) {
2051 VLOG_WARN_RL(&rl, "destination field %s is not writable",
2053 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
2058 /* Copies the value and wildcard bit pattern for 'mf' from 'match' into the
2059 * 'value' and 'mask', respectively. */
2061 mf_get(const struct mf_field *mf, const struct match *match,
2062 union mf_value *value, union mf_value *mask)
2064 mf_get_value(mf, &match->flow, value);
2065 mf_get_mask(mf, &match->wc, mask);
2069 mf_from_integer_string(const struct mf_field *mf, const char *s,
2070 uint8_t *valuep, uint8_t *maskp)
2073 const char *err_str = "";
2076 err = parse_int_string(s, valuep, mf->n_bytes, &tail);
2077 if (err || (*tail != '\0' && *tail != '/')) {
2083 err = parse_int_string(tail + 1, maskp, mf->n_bytes, &tail);
2084 if (err || *tail != '\0') {
2089 memset(maskp, 0xff, mf->n_bytes);
2095 if (err == ERANGE) {
2096 return xasprintf("%s: %s too large for %u-byte field %s",
2097 s, err_str, mf->n_bytes, mf->name);
2099 return xasprintf("%s: bad syntax for %s %s", s, mf->name, err_str);
2104 mf_from_ethernet_string(const struct mf_field *mf, const char *s,
2105 struct eth_addr *mac, struct eth_addr *mask)
2109 ovs_assert(mf->n_bytes == ETH_ADDR_LEN);
2112 if (ovs_scan(s, ETH_ADDR_SCAN_FMT"%n", ETH_ADDR_SCAN_ARGS(*mac), &n)
2113 && n == strlen(s)) {
2114 *mask = eth_addr_exact;
2119 if (ovs_scan(s, ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT"%n",
2120 ETH_ADDR_SCAN_ARGS(*mac), ETH_ADDR_SCAN_ARGS(*mask), &n)
2121 && n == strlen(s)) {
2125 return xasprintf("%s: invalid Ethernet address", s);
2129 mf_from_ipv4_string(const struct mf_field *mf, const char *s,
2130 ovs_be32 *ip, ovs_be32 *mask)
2132 ovs_assert(mf->n_bytes == sizeof *ip);
2133 return ip_parse_masked(s, ip, mask);
2137 mf_from_ipv6_string(const struct mf_field *mf, const char *s,
2138 struct in6_addr *ipv6, struct in6_addr *mask)
2140 ovs_assert(mf->n_bytes == sizeof *ipv6);
2141 return ipv6_parse_masked(s, ipv6, mask);
2145 mf_from_ofp_port_string(const struct mf_field *mf, const char *s,
2146 ovs_be16 *valuep, ovs_be16 *maskp)
2150 ovs_assert(mf->n_bytes == sizeof(ovs_be16));
2152 if (ofputil_port_from_string(s, &port)) {
2153 *valuep = htons(ofp_to_u16(port));
2154 *maskp = OVS_BE16_MAX;
2157 return xasprintf("%s: port value out of range for %s", s, mf->name);
2161 mf_from_ofp_port_string32(const struct mf_field *mf, const char *s,
2162 ovs_be32 *valuep, ovs_be32 *maskp)
2166 ovs_assert(mf->n_bytes == sizeof(ovs_be32));
2167 if (ofputil_port_from_string(s, &port)) {
2168 *valuep = ofputil_port_to_ofp11(port);
2169 *maskp = OVS_BE32_MAX;
2172 return xasprintf("%s: port value out of range for %s", s, mf->name);
2175 struct frag_handling {
2181 static const struct frag_handling all_frags[] = {
2182 #define A FLOW_NW_FRAG_ANY
2183 #define L FLOW_NW_FRAG_LATER
2184 /* name mask value */
2187 { "first", A|L, A },
2188 { "later", A|L, A|L },
2193 { "not_later", L, 0 },
2200 mf_from_frag_string(const char *s, uint8_t *valuep, uint8_t *maskp)
2202 const struct frag_handling *h;
2204 for (h = all_frags; h < &all_frags[ARRAY_SIZE(all_frags)]; h++) {
2205 if (!strcasecmp(s, h->name)) {
2206 /* We force the upper bits of the mask on to make mf_parse_value()
2207 * happy (otherwise it will never think it's an exact match.) */
2208 *maskp = h->mask | ~FLOW_NW_FRAG_MASK;
2214 return xasprintf("%s: unknown fragment type (valid types are \"no\", "
2215 "\"yes\", \"first\", \"later\", \"not_first\"", s);
2219 parse_mf_flags(const char *s, const char *(*bit_to_string)(uint32_t),
2220 const char *field_name, ovs_be16 *flagsp, ovs_be16 allowed,
2225 uint32_t flags, mask;
2227 err = parse_flags(s, bit_to_string, '\0', field_name, &err_str,
2228 &flags, ntohs(allowed), maskp ? &mask : NULL);
2233 *flagsp = htons(flags);
2235 *maskp = htons(mask);
2242 mf_from_tcp_flags_string(const char *s, ovs_be16 *flagsp, ovs_be16 *maskp)
2244 return parse_mf_flags(s, packet_tcp_flag_to_string, "TCP", flagsp,
2245 TCP_FLAGS_BE16(OVS_BE16_MAX), maskp);
2249 mf_from_tun_flags_string(const char *s, ovs_be16 *flagsp, ovs_be16 *maskp)
2251 return parse_mf_flags(s, flow_tun_flag_to_string, "tunnel", flagsp,
2252 htons(FLOW_TNL_PUB_F_MASK), maskp);
2256 mf_from_ct_state_string(const char *s, ovs_be32 *flagsp, ovs_be32 *maskp)
2260 uint32_t flags, mask;
2262 err = parse_flags(s, ct_state_to_string, '\0', "ct_state", &err_str,
2263 &flags, CS_SUPPORTED_MASK, maskp ? &mask : NULL);
2268 *flagsp = htonl(flags);
2270 *maskp = htonl(mask);
2276 /* Parses 's', a string value for field 'mf', into 'value' and 'mask'. Returns
2277 * NULL if successful, otherwise a malloc()'d string describing the error. */
2279 mf_parse(const struct mf_field *mf, const char *s,
2280 union mf_value *value, union mf_value *mask)
2284 if (!strcmp(s, "*")) {
2285 memset(value, 0, mf->n_bytes);
2286 memset(mask, 0, mf->n_bytes);
2290 switch (mf->string) {
2292 case MFS_HEXADECIMAL:
2293 error = mf_from_integer_string(mf, s,
2294 (uint8_t *) value, (uint8_t *) mask);
2298 ovs_assert(mf->n_bytes == sizeof(ovs_be32));
2299 error = mf_from_ct_state_string(s, &value->be32, &mask->be32);
2303 error = mf_from_ethernet_string(mf, s, &value->mac, &mask->mac);
2307 error = mf_from_ipv4_string(mf, s, &value->be32, &mask->be32);
2311 error = mf_from_ipv6_string(mf, s, &value->ipv6, &mask->ipv6);
2315 error = mf_from_ofp_port_string(mf, s, &value->be16, &mask->be16);
2318 case MFS_OFP_PORT_OXM:
2319 error = mf_from_ofp_port_string32(mf, s, &value->be32, &mask->be32);
2323 error = mf_from_frag_string(s, &value->u8, &mask->u8);
2327 ovs_assert(mf->n_bytes == sizeof(ovs_be16));
2328 error = mf_from_tun_flags_string(s, &value->be16, &mask->be16);
2332 ovs_assert(mf->n_bytes == sizeof(ovs_be16));
2333 error = mf_from_tcp_flags_string(s, &value->be16, &mask->be16);
2340 if (!error && !mf_is_mask_valid(mf, mask)) {
2341 error = xasprintf("%s: invalid mask for field %s", s, mf->name);
2346 /* Parses 's', a string value for field 'mf', into 'value'. Returns NULL if
2347 * successful, otherwise a malloc()'d string describing the error. */
2349 mf_parse_value(const struct mf_field *mf, const char *s, union mf_value *value)
2351 union mf_value mask;
2354 error = mf_parse(mf, s, value, &mask);
2359 if (!is_all_ones((const uint8_t *) &mask, mf->n_bytes)) {
2360 return xasprintf("%s: wildcards not allowed here", s);
2366 mf_format_integer_string(const struct mf_field *mf, const uint8_t *valuep,
2367 const uint8_t *maskp, struct ds *s)
2369 if (mf->string == MFS_HEXADECIMAL) {
2370 ds_put_hex(s, valuep, mf->n_bytes);
2372 unsigned long long int integer = 0;
2375 ovs_assert(mf->n_bytes <= 8);
2376 for (i = 0; i < mf->n_bytes; i++) {
2377 integer = (integer << 8) | valuep[i];
2379 ds_put_format(s, "%lld", integer);
2383 /* I guess we could write the mask in decimal for MFS_DECIMAL but I'm
2384 * not sure that that a bit-mask written in decimal is ever easier to
2385 * understand than the same bit-mask written in hexadecimal. */
2386 ds_put_char(s, '/');
2387 ds_put_hex(s, maskp, mf->n_bytes);
2392 mf_format_frag_string(uint8_t value, uint8_t mask, struct ds *s)
2394 const struct frag_handling *h;
2396 mask &= FLOW_NW_FRAG_MASK;
2399 for (h = all_frags; h < &all_frags[ARRAY_SIZE(all_frags)]; h++) {
2400 if (value == h->value && mask == h->mask) {
2401 ds_put_cstr(s, h->name);
2405 ds_put_cstr(s, "<error>");
2409 mf_format_tnl_flags_string(ovs_be16 value, ovs_be16 mask, struct ds *s)
2411 format_flags_masked(s, NULL, flow_tun_flag_to_string, ntohs(value),
2412 ntohs(mask) & FLOW_TNL_PUB_F_MASK, FLOW_TNL_PUB_F_MASK);
2416 mf_format_tcp_flags_string(ovs_be16 value, ovs_be16 mask, struct ds *s)
2418 format_flags_masked(s, NULL, packet_tcp_flag_to_string, ntohs(value),
2419 TCP_FLAGS(mask), TCP_FLAGS(OVS_BE16_MAX));
2423 mf_format_ct_state_string(ovs_be32 value, ovs_be32 mask, struct ds *s)
2425 format_flags_masked(s, NULL, ct_state_to_string, ntohl(value),
2426 ntohl(mask), UINT16_MAX);
2429 /* Appends to 's' a string representation of field 'mf' whose value is in
2430 * 'value' and 'mask'. 'mask' may be NULL to indicate an exact match. */
2432 mf_format(const struct mf_field *mf,
2433 const union mf_value *value, const union mf_value *mask,
2437 if (is_all_zeros(mask, mf->n_bytes)) {
2438 ds_put_cstr(s, "ANY");
2440 } else if (is_all_ones(mask, mf->n_bytes)) {
2445 switch (mf->string) {
2446 case MFS_OFP_PORT_OXM:
2449 ofputil_port_from_ofp11(value->be32, &port);
2450 ofputil_format_port(port, s);
2456 ofputil_format_port(u16_to_ofp(ntohs(value->be16)), s);
2461 case MFS_HEXADECIMAL:
2462 mf_format_integer_string(mf, (uint8_t *) value, (uint8_t *) mask, s);
2466 mf_format_ct_state_string(value->be32,
2467 mask ? mask->be32 : OVS_BE32_MAX, s);
2471 eth_format_masked(value->mac, mask ? &mask->mac : NULL, s);
2475 ip_format_masked(value->be32, mask ? mask->be32 : OVS_BE32_MAX, s);
2479 ipv6_format_masked(&value->ipv6, mask ? &mask->ipv6 : NULL, s);
2483 mf_format_frag_string(value->u8, mask ? mask->u8 : UINT8_MAX, s);
2487 mf_format_tnl_flags_string(value->be16,
2488 mask ? mask->be16 : OVS_BE16_MAX, s);
2492 mf_format_tcp_flags_string(value->be16,
2493 mask ? mask->be16 : OVS_BE16_MAX, s);
2501 /* Makes subfield 'sf' within 'flow' exactly match the 'sf->n_bits'
2502 * least-significant bits in 'x'.
2505 mf_write_subfield_flow(const struct mf_subfield *sf,
2506 const union mf_subvalue *x, struct flow *flow)
2508 const struct mf_field *field = sf->field;
2509 union mf_value value;
2511 mf_get_value(field, flow, &value);
2512 bitwise_copy(x, sizeof *x, 0, &value, field->n_bytes,
2513 sf->ofs, sf->n_bits);
2514 mf_set_flow_value(field, &value, flow);
2517 /* Makes subfield 'sf' within 'match' exactly match the 'sf->n_bits'
2518 * least-significant bits in 'x'.
2521 mf_write_subfield(const struct mf_subfield *sf, const union mf_subvalue *x,
2522 struct match *match)
2524 const struct mf_field *field = sf->field;
2525 union mf_value value, mask;
2527 mf_get(field, match, &value, &mask);
2528 bitwise_copy(x, sizeof *x, 0, &value, field->n_bytes, sf->ofs, sf->n_bits);
2529 bitwise_one ( &mask, field->n_bytes, sf->ofs, sf->n_bits);
2530 mf_set(field, &value, &mask, match, NULL);
2533 /* 'v' and 'm' correspond to values of 'field'. This function copies them into
2534 * 'match' in the correspond positions. */
2536 mf_mask_subfield(const struct mf_field *field,
2537 const union mf_subvalue *v,
2538 const union mf_subvalue *m,
2539 struct match *match)
2541 union mf_value value, mask;
2543 mf_get(field, match, &value, &mask);
2544 bitwise_copy(v, sizeof *v, 0, &value, field->n_bytes, 0, field->n_bits);
2545 bitwise_copy(m, sizeof *m, 0, &mask, field->n_bytes, 0, field->n_bits);
2546 mf_set(field, &value, &mask, match, NULL);
2549 /* Initializes 'x' to the value of 'sf' within 'flow'. 'sf' must be valid for
2550 * reading 'flow', e.g. as checked by mf_check_src(). */
2552 mf_read_subfield(const struct mf_subfield *sf, const struct flow *flow,
2553 union mf_subvalue *x)
2555 union mf_value value;
2557 mf_get_value(sf->field, flow, &value);
2559 memset(x, 0, sizeof *x);
2560 bitwise_copy(&value, sf->field->n_bytes, sf->ofs,
2565 /* Returns the value of 'sf' within 'flow'. 'sf' must be valid for reading
2566 * 'flow', e.g. as checked by mf_check_src() and sf->n_bits must be 64 or
2569 mf_get_subfield(const struct mf_subfield *sf, const struct flow *flow)
2571 union mf_value value;
2573 mf_get_value(sf->field, flow, &value);
2574 return bitwise_get(&value, sf->field->n_bytes, sf->ofs, sf->n_bits);
2578 mf_format_subvalue(const union mf_subvalue *subvalue, struct ds *s)
2580 ds_put_hex(s, subvalue->u8, sizeof subvalue->u8);
2584 field_array_set(enum mf_field_id id, const union mf_value *value,
2585 struct field_array *fa)
2587 ovs_assert(id < MFF_N_IDS);
2588 bitmap_set1(fa->used.bm, id);
2589 fa->value[id] = *value;