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 "unaligned.h"
38 #include "openvswitch/vlog.h"
40 VLOG_DEFINE_THIS_MODULE(meta_flow);
42 #define FLOW_U32OFS(FIELD) \
43 offsetof(struct flow, FIELD) % 4 ? -1 : offsetof(struct flow, FIELD) / 4
45 #define MF_FIELD_SIZES(MEMBER) \
46 sizeof ((union mf_value *)0)->MEMBER, \
47 8 * sizeof ((union mf_value *)0)->MEMBER
49 extern const struct mf_field mf_fields[MFF_N_IDS]; /* Silence a warning. */
51 const struct mf_field mf_fields[MFF_N_IDS] = {
52 #include "meta-flow.inc"
55 /* Maps from an mf_field's 'name' or 'extra_name' to the mf_field. */
56 static struct shash mf_by_name;
58 /* Rate limit for parse errors. These always indicate a bug in an OpenFlow
59 * controller and so there's not much point in showing a lot of them. */
60 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
62 static void nxm_init(void);
64 /* Returns the field with the given 'name', or a null pointer if no field has
66 const struct mf_field *
67 mf_from_name(const char *name)
70 return shash_find_data(&mf_by_name, name);
78 shash_init(&mf_by_name);
79 for (i = 0; i < MFF_N_IDS; i++) {
80 const struct mf_field *mf = &mf_fields[i];
82 ovs_assert(mf->id == i); /* Fields must be in the enum order. */
84 shash_add_once(&mf_by_name, mf->name, mf);
86 shash_add_once(&mf_by_name, mf->extra_name, mf);
94 static pthread_once_t once = PTHREAD_ONCE_INIT;
95 pthread_once(&once, nxm_do_init);
98 /* Consider the two value/mask pairs 'a_value/a_mask' and 'b_value/b_mask' as
99 * restrictions on a field's value. Then, this function initializes
100 * 'dst_value/dst_mask' such that it combines the restrictions of both pairs.
101 * This is not always possible, i.e. if one pair insists on a value of 0 in
102 * some bit and the other pair insists on a value of 1 in that bit. This
103 * function returns false in a case where the combined restriction is
104 * impossible (in which case 'dst_value/dst_mask' is not fully initialized),
107 * (As usually true for value/mask pairs in OVS, any 1-bit in a value must have
108 * a corresponding 1-bit in its mask.) */
110 mf_subvalue_intersect(const union mf_subvalue *a_value,
111 const union mf_subvalue *a_mask,
112 const union mf_subvalue *b_value,
113 const union mf_subvalue *b_mask,
114 union mf_subvalue *dst_value,
115 union mf_subvalue *dst_mask)
117 for (int i = 0; i < ARRAY_SIZE(a_value->be64); i++) {
118 ovs_be64 av = a_value->be64[i];
119 ovs_be64 am = a_mask->be64[i];
120 ovs_be64 bv = b_value->be64[i];
121 ovs_be64 bm = b_mask->be64[i];
122 ovs_be64 *dv = &dst_value->be64[i];
123 ovs_be64 *dm = &dst_mask->be64[i];
125 if ((av ^ bv) & (am & bm)) {
134 /* Returns the "number of bits" in 'v', e.g. 1 if only the lowest-order bit is
135 * set, 2 if the second-lowest-order bit is set, and so on. */
137 mf_subvalue_width(const union mf_subvalue *v)
139 return 1 + bitwise_rscan(v, sizeof *v, true, sizeof *v * 8 - 1, -1);
142 /* For positive 'n', shifts the bits in 'value' 'n' bits to the left, and for
143 * negative 'n', shifts the bits '-n' bits to the right. */
145 mf_subvalue_shift(union mf_subvalue *value, int n)
148 union mf_subvalue tmp;
149 memset(&tmp, 0, sizeof tmp);
151 if (n > 0 && n < 8 * sizeof tmp) {
152 bitwise_copy(value, sizeof *value, 0,
155 } else if (n < 0 && n > -8 * sizeof tmp) {
156 bitwise_copy(value, sizeof *value, -n,
164 /* Returns true if 'wc' wildcards all the bits in field 'mf', false if 'wc'
165 * specifies at least one bit in the field.
167 * The caller is responsible for ensuring that 'wc' corresponds to a flow that
168 * meets 'mf''s prerequisites. */
170 mf_is_all_wild(const struct mf_field *mf, const struct flow_wildcards *wc)
174 return !wc->masks.dp_hash;
176 return !wc->masks.recirc_id;
178 return !wc->masks.conj_id;
180 return !wc->masks.tunnel.ip_src;
182 return !wc->masks.tunnel.ip_dst;
187 return !wc->masks.tunnel.tun_id;
189 return !wc->masks.tunnel.gbp_id;
190 case MFF_TUN_GBP_FLAGS:
191 return !wc->masks.tunnel.gbp_flags;
193 return !wc->masks.metadata;
195 case MFF_IN_PORT_OXM:
196 return !wc->masks.in_port.ofp_port;
197 case MFF_SKB_PRIORITY:
198 return !wc->masks.skb_priority;
200 return !wc->masks.pkt_mark;
202 return !wc->masks.regs[mf->id - MFF_REG0];
204 return !flow_get_xreg(&wc->masks, mf->id - MFF_XREG0);
205 case MFF_ACTSET_OUTPUT:
206 return !wc->masks.actset_output;
209 return eth_addr_is_zero(wc->masks.dl_src);
211 return eth_addr_is_zero(wc->masks.dl_dst);
213 return !wc->masks.dl_type;
217 return eth_addr_is_zero(wc->masks.arp_sha);
221 return eth_addr_is_zero(wc->masks.arp_tha);
224 return !wc->masks.vlan_tci;
226 return !(wc->masks.vlan_tci & htons(VLAN_VID_MASK));
228 return !(wc->masks.vlan_tci & htons(VLAN_VID_MASK | VLAN_CFI));
229 case MFF_DL_VLAN_PCP:
231 return !(wc->masks.vlan_tci & htons(VLAN_PCP_MASK));
234 return !(wc->masks.mpls_lse[0] & htonl(MPLS_LABEL_MASK));
236 return !(wc->masks.mpls_lse[0] & htonl(MPLS_TC_MASK));
238 return !(wc->masks.mpls_lse[0] & htonl(MPLS_BOS_MASK));
241 return !wc->masks.nw_src;
243 return !wc->masks.nw_dst;
246 return ipv6_mask_is_any(&wc->masks.ipv6_src);
248 return ipv6_mask_is_any(&wc->masks.ipv6_dst);
251 return !wc->masks.ipv6_label;
254 return !wc->masks.nw_proto;
256 case MFF_IP_DSCP_SHIFTED:
257 return !(wc->masks.nw_tos & IP_DSCP_MASK);
259 return !(wc->masks.nw_tos & IP_ECN_MASK);
261 return !wc->masks.nw_ttl;
264 return ipv6_mask_is_any(&wc->masks.nd_target);
267 return !(wc->masks.nw_frag & FLOW_NW_FRAG_MASK);
270 return !wc->masks.nw_proto;
272 return !wc->masks.nw_src;
274 return !wc->masks.nw_dst;
279 case MFF_ICMPV4_TYPE:
280 case MFF_ICMPV6_TYPE:
281 return !wc->masks.tp_src;
285 case MFF_ICMPV4_CODE:
286 case MFF_ICMPV6_CODE:
287 return !wc->masks.tp_dst;
289 return !wc->masks.tcp_flags;
297 /* Initializes 'mask' with the wildcard bit pattern for field 'mf' within 'wc'.
298 * Each bit in 'mask' will be set to 1 if the bit is significant for matching
299 * purposes, or to 0 if it is wildcarded.
301 * The caller is responsible for ensuring that 'wc' corresponds to a flow that
302 * meets 'mf''s prerequisites. */
304 mf_get_mask(const struct mf_field *mf, const struct flow_wildcards *wc,
305 union mf_value *mask)
307 mf_get_value(mf, &wc->masks, mask);
310 /* Tests whether 'mask' is a valid wildcard bit pattern for 'mf'. Returns true
311 * if the mask is valid, false otherwise. */
313 mf_is_mask_valid(const struct mf_field *mf, const union mf_value *mask)
315 switch (mf->maskable) {
317 return (is_all_zeros(mask, mf->n_bytes) ||
318 is_all_ones(mask, mf->n_bytes));
327 /* Returns true if 'flow' meets the prerequisites for 'mf', false otherwise. */
329 mf_are_prereqs_ok(const struct mf_field *mf, const struct flow *flow)
331 switch (mf->prereqs) {
336 return (flow->dl_type == htons(ETH_TYPE_ARP) ||
337 flow->dl_type == htons(ETH_TYPE_RARP));
339 return flow->dl_type == htons(ETH_TYPE_IP);
341 return flow->dl_type == htons(ETH_TYPE_IPV6);
343 return (flow->vlan_tci & htons(VLAN_CFI)) != 0;
345 return eth_type_mpls(flow->dl_type);
347 return is_ip_any(flow);
350 return is_ip_any(flow) && flow->nw_proto == IPPROTO_TCP
351 && !(flow->nw_frag & FLOW_NW_FRAG_LATER);
353 return is_ip_any(flow) && flow->nw_proto == IPPROTO_UDP
354 && !(flow->nw_frag & FLOW_NW_FRAG_LATER);
356 return is_ip_any(flow) && flow->nw_proto == IPPROTO_SCTP
357 && !(flow->nw_frag & FLOW_NW_FRAG_LATER);
359 return is_icmpv4(flow);
361 return is_icmpv6(flow);
364 return (is_icmpv6(flow)
365 && flow->tp_dst == htons(0)
366 && (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT) ||
367 flow->tp_src == htons(ND_NEIGHBOR_ADVERT)));
369 return (is_icmpv6(flow)
370 && flow->tp_dst == htons(0)
371 && (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT)));
373 return (is_icmpv6(flow)
374 && flow->tp_dst == htons(0)
375 && (flow->tp_src == htons(ND_NEIGHBOR_ADVERT)));
381 /* Set field and it's prerequisities in the mask.
382 * This is only ever called for writeable 'mf's, but we do not make the
383 * distinction here. */
385 mf_mask_field_and_prereqs(const struct mf_field *mf, struct flow *mask)
387 static const union mf_value exact_match_mask = MF_EXACT_MASK_INITIALIZER;
389 mf_set_flow_value(mf, &exact_match_mask, mask);
391 switch (mf->prereqs) {
395 mask->tp_src = OVS_BE16_MAX;
396 mask->tp_dst = OVS_BE16_MAX;
403 /* nw_frag always unwildcarded. */
404 mask->nw_proto = 0xff;
411 mask->dl_type = OVS_BE16_MAX;
414 mask->vlan_tci |= htons(VLAN_CFI);
421 /* Set bits of 'bm' corresponding to the field 'mf' and it's prerequisities. */
423 mf_bitmap_set_field_and_prereqs(const struct mf_field *mf, struct mf_bitmap *bm)
425 bitmap_set1(bm->bm, mf->id);
427 switch (mf->prereqs) {
431 bitmap_set1(bm->bm, MFF_TCP_SRC);
432 bitmap_set1(bm->bm, MFF_TCP_DST);
439 /* nw_frag always unwildcarded. */
440 bitmap_set1(bm->bm, MFF_IP_PROTO);
447 bitmap_set1(bm->bm, MFF_ETH_TYPE);
450 bitmap_set1(bm->bm, MFF_VLAN_TCI);
457 /* Returns true if 'value' may be a valid value *as part of a masked match*,
460 * A value is not rejected just because it is not valid for the field in
461 * question, but only if it doesn't make sense to test the bits in question at
462 * all. For example, the MFF_VLAN_TCI field will never have a nonzero value
463 * without the VLAN_CFI bit being set, but we can't reject those values because
464 * it is still legitimate to test just for those bits (see the documentation
465 * for NXM_OF_VLAN_TCI in nicira-ext.h). On the other hand, there is never a
466 * reason to set the low bit of MFF_IP_DSCP to 1, so we reject that. */
468 mf_is_value_valid(const struct mf_field *mf, const union mf_value *value)
481 case MFF_TUN_GBP_FLAGS:
484 case MFF_SKB_PRIORITY:
508 case MFF_ICMPV4_TYPE:
509 case MFF_ICMPV4_CODE:
510 case MFF_ICMPV6_TYPE:
511 case MFF_ICMPV6_CODE:
517 case MFF_IN_PORT_OXM:
518 case MFF_ACTSET_OUTPUT: {
520 return !ofputil_port_from_ofp11(value->be32, &port);
524 return !(value->u8 & ~IP_DSCP_MASK);
525 case MFF_IP_DSCP_SHIFTED:
526 return !(value->u8 & (~IP_DSCP_MASK >> 2));
528 return !(value->u8 & ~IP_ECN_MASK);
530 return !(value->u8 & ~FLOW_NW_FRAG_MASK);
532 return !(value->be16 & ~htons(0x0fff));
535 return !(value->be16 & htons(0xff00));
538 return !(value->be16 & htons(VLAN_CFI | VLAN_PCP_MASK));
540 return !(value->be16 & htons(VLAN_PCP_MASK));
542 case MFF_DL_VLAN_PCP:
544 return !(value->u8 & ~(VLAN_PCP_MASK >> VLAN_PCP_SHIFT));
547 return !(value->be32 & ~htonl(IPV6_LABEL_MASK));
550 return !(value->be32 & ~htonl(MPLS_LABEL_MASK >> MPLS_LABEL_SHIFT));
553 return !(value->u8 & ~(MPLS_TC_MASK >> MPLS_TC_SHIFT));
556 return !(value->u8 & ~(MPLS_BOS_MASK >> MPLS_BOS_SHIFT));
564 /* Copies the value of field 'mf' from 'flow' into 'value'. The caller is
565 * responsible for ensuring that 'flow' meets 'mf''s prerequisites. */
567 mf_get_value(const struct mf_field *mf, const struct flow *flow,
568 union mf_value *value)
572 value->be32 = htonl(flow->dp_hash);
575 value->be32 = htonl(flow->recirc_id);
578 value->be32 = htonl(flow->conj_id);
581 value->be64 = flow->tunnel.tun_id;
584 value->be32 = flow->tunnel.ip_src;
587 value->be32 = flow->tunnel.ip_dst;
590 value->be16 = htons(flow->tunnel.flags);
593 value->be16 = flow->tunnel.gbp_id;
595 case MFF_TUN_GBP_FLAGS:
596 value->u8 = flow->tunnel.gbp_flags;
599 value->u8 = flow->tunnel.ip_ttl;
602 value->u8 = flow->tunnel.ip_tos;
606 value->be64 = flow->metadata;
610 value->be16 = htons(ofp_to_u16(flow->in_port.ofp_port));
612 case MFF_IN_PORT_OXM:
613 value->be32 = ofputil_port_to_ofp11(flow->in_port.ofp_port);
615 case MFF_ACTSET_OUTPUT:
616 value->be32 = ofputil_port_to_ofp11(flow->actset_output);
619 case MFF_SKB_PRIORITY:
620 value->be32 = htonl(flow->skb_priority);
624 value->be32 = htonl(flow->pkt_mark);
628 value->be32 = htonl(flow->regs[mf->id - MFF_REG0]);
632 value->be64 = htonll(flow_get_xreg(flow, mf->id - MFF_XREG0));
636 memcpy(value->mac, flow->dl_src, ETH_ADDR_LEN);
640 memcpy(value->mac, flow->dl_dst, ETH_ADDR_LEN);
644 value->be16 = flow->dl_type;
648 value->be16 = flow->vlan_tci;
652 value->be16 = flow->vlan_tci & htons(VLAN_VID_MASK);
655 value->be16 = flow->vlan_tci & htons(VLAN_VID_MASK | VLAN_CFI);
658 case MFF_DL_VLAN_PCP:
660 value->u8 = vlan_tci_to_pcp(flow->vlan_tci);
664 value->be32 = htonl(mpls_lse_to_label(flow->mpls_lse[0]));
668 value->u8 = mpls_lse_to_tc(flow->mpls_lse[0]);
672 value->u8 = mpls_lse_to_bos(flow->mpls_lse[0]);
676 value->be32 = flow->nw_src;
680 value->be32 = flow->nw_dst;
684 value->ipv6 = flow->ipv6_src;
688 value->ipv6 = flow->ipv6_dst;
692 value->be32 = flow->ipv6_label;
696 value->u8 = flow->nw_proto;
700 value->u8 = flow->nw_tos & IP_DSCP_MASK;
703 case MFF_IP_DSCP_SHIFTED:
704 value->u8 = flow->nw_tos >> 2;
708 value->u8 = flow->nw_tos & IP_ECN_MASK;
712 value->u8 = flow->nw_ttl;
716 value->u8 = flow->nw_frag;
720 value->be16 = htons(flow->nw_proto);
724 value->be32 = flow->nw_src;
728 value->be32 = flow->nw_dst;
733 memcpy(value->mac, flow->arp_sha, ETH_ADDR_LEN);
738 memcpy(value->mac, flow->arp_tha, ETH_ADDR_LEN);
744 value->be16 = flow->tp_src;
750 value->be16 = flow->tp_dst;
754 value->be16 = flow->tcp_flags;
757 case MFF_ICMPV4_TYPE:
758 case MFF_ICMPV6_TYPE:
759 value->u8 = ntohs(flow->tp_src);
762 case MFF_ICMPV4_CODE:
763 case MFF_ICMPV6_CODE:
764 value->u8 = ntohs(flow->tp_dst);
768 value->ipv6 = flow->nd_target;
777 /* Makes 'match' match field 'mf' exactly, with the value matched taken from
778 * 'value'. The caller is responsible for ensuring that 'match' meets 'mf''s
781 mf_set_value(const struct mf_field *mf,
782 const union mf_value *value, struct match *match)
786 match_set_dp_hash(match, ntohl(value->be32));
789 match_set_recirc_id(match, ntohl(value->be32));
792 match_set_conj_id(match, ntohl(value->be32));
795 match_set_tun_id(match, value->be64);
798 match_set_tun_src(match, value->be32);
801 match_set_tun_dst(match, value->be32);
804 match_set_tun_flags(match, ntohs(value->be16));
807 match_set_tun_gbp_id(match, value->be16);
809 case MFF_TUN_GBP_FLAGS:
810 match_set_tun_gbp_flags(match, value->u8);
813 match_set_tun_tos(match, value->u8);
816 match_set_tun_ttl(match, value->u8);
820 match_set_metadata(match, value->be64);
824 match_set_in_port(match, u16_to_ofp(ntohs(value->be16)));
827 case MFF_IN_PORT_OXM: {
829 ofputil_port_from_ofp11(value->be32, &port);
830 match_set_in_port(match, port);
833 case MFF_ACTSET_OUTPUT: {
835 ofputil_port_from_ofp11(value->be32, &port);
836 match_set_actset_output(match, port);
840 case MFF_SKB_PRIORITY:
841 match_set_skb_priority(match, ntohl(value->be32));
845 match_set_pkt_mark(match, ntohl(value->be32));
849 match_set_reg(match, mf->id - MFF_REG0, ntohl(value->be32));
853 match_set_xreg(match, mf->id - MFF_XREG0, ntohll(value->be64));
857 match_set_dl_src(match, value->mac);
861 match_set_dl_dst(match, value->mac);
865 match_set_dl_type(match, value->be16);
869 match_set_dl_tci(match, value->be16);
873 match_set_dl_vlan(match, value->be16);
876 match_set_vlan_vid(match, value->be16);
879 case MFF_DL_VLAN_PCP:
881 match_set_dl_vlan_pcp(match, value->u8);
885 match_set_mpls_label(match, 0, value->be32);
889 match_set_mpls_tc(match, 0, value->u8);
893 match_set_mpls_bos(match, 0, value->u8);
897 match_set_nw_src(match, value->be32);
901 match_set_nw_dst(match, value->be32);
905 match_set_ipv6_src(match, &value->ipv6);
909 match_set_ipv6_dst(match, &value->ipv6);
913 match_set_ipv6_label(match, value->be32);
917 match_set_nw_proto(match, value->u8);
921 match_set_nw_dscp(match, value->u8);
924 case MFF_IP_DSCP_SHIFTED:
925 match_set_nw_dscp(match, value->u8 << 2);
929 match_set_nw_ecn(match, value->u8);
933 match_set_nw_ttl(match, value->u8);
937 match_set_nw_frag(match, value->u8);
941 match_set_nw_proto(match, ntohs(value->be16));
945 match_set_nw_src(match, value->be32);
949 match_set_nw_dst(match, value->be32);
954 match_set_arp_sha(match, value->mac);
959 match_set_arp_tha(match, value->mac);
965 match_set_tp_src(match, value->be16);
971 match_set_tp_dst(match, value->be16);
975 match_set_tcp_flags(match, value->be16);
978 case MFF_ICMPV4_TYPE:
979 case MFF_ICMPV6_TYPE:
980 match_set_icmp_type(match, value->u8);
983 case MFF_ICMPV4_CODE:
984 case MFF_ICMPV6_CODE:
985 match_set_icmp_code(match, value->u8);
989 match_set_nd_target(match, &value->ipv6);
998 /* Unwildcard 'mask' member field described by 'mf'. The caller is
999 * responsible for ensuring that 'mask' meets 'mf''s prerequisites. */
1001 mf_mask_field(const struct mf_field *mf, struct flow *mask)
1003 static const union mf_value exact_match_mask = MF_EXACT_MASK_INITIALIZER;
1005 /* For MFF_DL_VLAN, we cannot send a all 1's to flow_set_dl_vlan()
1006 * as that will be considered as OFP10_VLAN_NONE. So consider it as a
1007 * special case. For the rest, calling mf_set_flow_value() is good
1009 if (mf->id == MFF_DL_VLAN) {
1010 flow_set_dl_vlan(mask, htons(VLAN_VID_MASK));
1012 mf_set_flow_value(mf, &exact_match_mask, mask);
1016 /* Sets 'flow' member field described by 'mf' to 'value'. The caller is
1017 * responsible for ensuring that 'flow' meets 'mf''s prerequisites.*/
1019 mf_set_flow_value(const struct mf_field *mf,
1020 const union mf_value *value, struct flow *flow)
1024 flow->dp_hash = ntohl(value->be32);
1027 flow->recirc_id = ntohl(value->be32);
1030 flow->conj_id = ntohl(value->be32);
1033 flow->tunnel.tun_id = value->be64;
1036 flow->tunnel.ip_src = value->be32;
1039 flow->tunnel.ip_dst = value->be32;
1042 flow->tunnel.flags = ntohs(value->be16);
1044 case MFF_TUN_GBP_ID:
1045 flow->tunnel.gbp_id = value->be16;
1047 case MFF_TUN_GBP_FLAGS:
1048 flow->tunnel.gbp_flags = value->u8;
1051 flow->tunnel.ip_tos = value->u8;
1054 flow->tunnel.ip_ttl = value->u8;
1058 flow->metadata = value->be64;
1062 flow->in_port.ofp_port = u16_to_ofp(ntohs(value->be16));
1065 case MFF_IN_PORT_OXM:
1066 ofputil_port_from_ofp11(value->be32, &flow->in_port.ofp_port);
1068 case MFF_ACTSET_OUTPUT:
1069 ofputil_port_from_ofp11(value->be32, &flow->actset_output);
1072 case MFF_SKB_PRIORITY:
1073 flow->skb_priority = ntohl(value->be32);
1077 flow->pkt_mark = ntohl(value->be32);
1081 flow->regs[mf->id - MFF_REG0] = ntohl(value->be32);
1085 flow_set_xreg(flow, mf->id - MFF_XREG0, ntohll(value->be64));
1089 memcpy(flow->dl_src, value->mac, ETH_ADDR_LEN);
1093 memcpy(flow->dl_dst, value->mac, ETH_ADDR_LEN);
1097 flow->dl_type = value->be16;
1101 flow->vlan_tci = value->be16;
1105 flow_set_dl_vlan(flow, value->be16);
1108 flow_set_vlan_vid(flow, value->be16);
1111 case MFF_DL_VLAN_PCP:
1113 flow_set_vlan_pcp(flow, value->u8);
1116 case MFF_MPLS_LABEL:
1117 flow_set_mpls_label(flow, 0, value->be32);
1121 flow_set_mpls_tc(flow, 0, value->u8);
1125 flow_set_mpls_bos(flow, 0, value->u8);
1129 flow->nw_src = value->be32;
1133 flow->nw_dst = value->be32;
1137 flow->ipv6_src = value->ipv6;
1141 flow->ipv6_dst = value->ipv6;
1144 case MFF_IPV6_LABEL:
1145 flow->ipv6_label = value->be32 & htonl(IPV6_LABEL_MASK);
1149 flow->nw_proto = value->u8;
1153 flow->nw_tos &= ~IP_DSCP_MASK;
1154 flow->nw_tos |= value->u8 & IP_DSCP_MASK;
1157 case MFF_IP_DSCP_SHIFTED:
1158 flow->nw_tos &= ~IP_DSCP_MASK;
1159 flow->nw_tos |= value->u8 << 2;
1163 flow->nw_tos &= ~IP_ECN_MASK;
1164 flow->nw_tos |= value->u8 & IP_ECN_MASK;
1168 flow->nw_ttl = value->u8;
1172 flow->nw_frag = value->u8 & FLOW_NW_FRAG_MASK;
1176 flow->nw_proto = ntohs(value->be16);
1180 flow->nw_src = value->be32;
1184 flow->nw_dst = value->be32;
1189 memcpy(flow->arp_sha, value->mac, ETH_ADDR_LEN);
1194 memcpy(flow->arp_tha, value->mac, ETH_ADDR_LEN);
1200 flow->tp_src = value->be16;
1206 flow->tp_dst = value->be16;
1210 flow->tcp_flags = value->be16;
1213 case MFF_ICMPV4_TYPE:
1214 case MFF_ICMPV6_TYPE:
1215 flow->tp_src = htons(value->u8);
1218 case MFF_ICMPV4_CODE:
1219 case MFF_ICMPV6_CODE:
1220 flow->tp_dst = htons(value->u8);
1224 flow->nd_target = value->ipv6;
1233 /* Consider each of 'src', 'mask', and 'dst' as if they were arrays of 8*n
1234 * bits. Then, for each 0 <= i < 8 * n such that mask[i] == 1, sets dst[i] =
1237 apply_mask(const uint8_t *src, const uint8_t *mask, uint8_t *dst, size_t n)
1241 for (i = 0; i < n; i++) {
1242 dst[i] = (src[i] & mask[i]) | (dst[i] & ~mask[i]);
1246 /* Sets 'flow' member field described by 'field' to 'value', except that bits
1247 * for which 'mask' has a 0-bit keep their existing values. The caller is
1248 * responsible for ensuring that 'flow' meets 'field''s prerequisites.*/
1250 mf_set_flow_value_masked(const struct mf_field *field,
1251 const union mf_value *value,
1252 const union mf_value *mask,
1257 mf_get_value(field, flow, &tmp);
1258 apply_mask((const uint8_t *) value, (const uint8_t *) mask,
1259 (uint8_t *) &tmp, field->n_bytes);
1260 mf_set_flow_value(field, &tmp, flow);
1263 /* Returns true if 'mf' has a zero value in 'flow', false if it is nonzero.
1265 * The caller is responsible for ensuring that 'flow' meets 'mf''s
1268 mf_is_zero(const struct mf_field *mf, const struct flow *flow)
1270 union mf_value value;
1272 mf_get_value(mf, flow, &value);
1273 return is_all_zeros(&value, mf->n_bytes);
1276 /* Makes 'match' wildcard field 'mf'.
1278 * The caller is responsible for ensuring that 'match' meets 'mf''s
1281 mf_set_wild(const struct mf_field *mf, struct match *match)
1285 match->flow.dp_hash = 0;
1286 match->wc.masks.dp_hash = 0;
1289 match->flow.recirc_id = 0;
1290 match->wc.masks.recirc_id = 0;
1293 match->flow.conj_id = 0;
1294 match->wc.masks.conj_id = 0;
1297 match_set_tun_id_masked(match, htonll(0), htonll(0));
1300 match_set_tun_src_masked(match, htonl(0), htonl(0));
1303 match_set_tun_dst_masked(match, htonl(0), htonl(0));
1306 match_set_tun_flags_masked(match, 0, 0);
1308 case MFF_TUN_GBP_ID:
1309 match_set_tun_gbp_id_masked(match, 0, 0);
1311 case MFF_TUN_GBP_FLAGS:
1312 match_set_tun_gbp_flags_masked(match, 0, 0);
1315 match_set_tun_tos_masked(match, 0, 0);
1318 match_set_tun_ttl_masked(match, 0, 0);
1322 match_set_metadata_masked(match, htonll(0), htonll(0));
1326 case MFF_IN_PORT_OXM:
1327 match->flow.in_port.ofp_port = 0;
1328 match->wc.masks.in_port.ofp_port = 0;
1330 case MFF_ACTSET_OUTPUT:
1331 match->flow.actset_output = 0;
1332 match->wc.masks.actset_output = 0;
1335 case MFF_SKB_PRIORITY:
1336 match->flow.skb_priority = 0;
1337 match->wc.masks.skb_priority = 0;
1341 match->flow.pkt_mark = 0;
1342 match->wc.masks.pkt_mark = 0;
1346 match_set_reg_masked(match, mf->id - MFF_REG0, 0, 0);
1350 match_set_xreg_masked(match, mf->id - MFF_XREG0, 0, 0);
1354 memset(match->flow.dl_src, 0, ETH_ADDR_LEN);
1355 memset(match->wc.masks.dl_src, 0, ETH_ADDR_LEN);
1359 memset(match->flow.dl_dst, 0, ETH_ADDR_LEN);
1360 memset(match->wc.masks.dl_dst, 0, ETH_ADDR_LEN);
1364 match->flow.dl_type = htons(0);
1365 match->wc.masks.dl_type = htons(0);
1369 match_set_dl_tci_masked(match, htons(0), htons(0));
1374 match_set_any_vid(match);
1377 case MFF_DL_VLAN_PCP:
1379 match_set_any_pcp(match);
1382 case MFF_MPLS_LABEL:
1383 match_set_any_mpls_label(match, 0);
1387 match_set_any_mpls_tc(match, 0);
1391 match_set_any_mpls_bos(match, 0);
1396 match_set_nw_src_masked(match, htonl(0), htonl(0));
1401 match_set_nw_dst_masked(match, htonl(0), htonl(0));
1405 memset(&match->wc.masks.ipv6_src, 0, sizeof match->wc.masks.ipv6_src);
1406 memset(&match->flow.ipv6_src, 0, sizeof match->flow.ipv6_src);
1410 memset(&match->wc.masks.ipv6_dst, 0, sizeof match->wc.masks.ipv6_dst);
1411 memset(&match->flow.ipv6_dst, 0, sizeof match->flow.ipv6_dst);
1414 case MFF_IPV6_LABEL:
1415 match->wc.masks.ipv6_label = htonl(0);
1416 match->flow.ipv6_label = htonl(0);
1420 match->wc.masks.nw_proto = 0;
1421 match->flow.nw_proto = 0;
1425 case MFF_IP_DSCP_SHIFTED:
1426 match->wc.masks.nw_tos &= ~IP_DSCP_MASK;
1427 match->flow.nw_tos &= ~IP_DSCP_MASK;
1431 match->wc.masks.nw_tos &= ~IP_ECN_MASK;
1432 match->flow.nw_tos &= ~IP_ECN_MASK;
1436 match->wc.masks.nw_ttl = 0;
1437 match->flow.nw_ttl = 0;
1441 match->wc.masks.nw_frag |= FLOW_NW_FRAG_MASK;
1442 match->flow.nw_frag &= ~FLOW_NW_FRAG_MASK;
1446 match->wc.masks.nw_proto = 0;
1447 match->flow.nw_proto = 0;
1452 memset(match->flow.arp_sha, 0, ETH_ADDR_LEN);
1453 memset(match->wc.masks.arp_sha, 0, ETH_ADDR_LEN);
1458 memset(match->flow.arp_tha, 0, ETH_ADDR_LEN);
1459 memset(match->wc.masks.arp_tha, 0, ETH_ADDR_LEN);
1465 case MFF_ICMPV4_TYPE:
1466 case MFF_ICMPV6_TYPE:
1467 match->wc.masks.tp_src = htons(0);
1468 match->flow.tp_src = htons(0);
1474 case MFF_ICMPV4_CODE:
1475 case MFF_ICMPV6_CODE:
1476 match->wc.masks.tp_dst = htons(0);
1477 match->flow.tp_dst = htons(0);
1481 match->wc.masks.tcp_flags = htons(0);
1482 match->flow.tcp_flags = htons(0);
1486 memset(&match->wc.masks.nd_target, 0,
1487 sizeof match->wc.masks.nd_target);
1488 memset(&match->flow.nd_target, 0, sizeof match->flow.nd_target);
1497 /* Makes 'match' match field 'mf' with the specified 'value' and 'mask'.
1498 * 'value' specifies a value to match and 'mask' specifies a wildcard pattern,
1499 * with a 1-bit indicating that the corresponding value bit must match and a
1500 * 0-bit indicating a don't-care.
1502 * If 'mask' is NULL or points to all-1-bits, then this call is equivalent to
1503 * mf_set_value(mf, value, match). If 'mask' points to all-0-bits, then this
1504 * call is equivalent to mf_set_wild(mf, match).
1506 * 'mask' must be a valid mask for 'mf' (see mf_is_mask_valid()). The caller
1507 * is responsible for ensuring that 'match' meets 'mf''s prerequisites. */
1508 enum ofputil_protocol
1509 mf_set(const struct mf_field *mf,
1510 const union mf_value *value, const union mf_value *mask,
1511 struct match *match)
1513 if (!mask || is_all_ones(mask, mf->n_bytes)) {
1514 mf_set_value(mf, value, match);
1515 return mf->usable_protocols_exact;
1516 } else if (is_all_zeros(mask, mf->n_bytes)) {
1517 mf_set_wild(mf, match);
1518 return OFPUTIL_P_ANY;
1525 case MFF_IN_PORT_OXM:
1526 case MFF_ACTSET_OUTPUT:
1527 case MFF_SKB_PRIORITY:
1530 case MFF_DL_VLAN_PCP:
1532 case MFF_MPLS_LABEL:
1538 case MFF_IP_DSCP_SHIFTED:
1541 case MFF_ICMPV4_TYPE:
1542 case MFF_ICMPV4_CODE:
1543 case MFF_ICMPV6_TYPE:
1544 case MFF_ICMPV6_CODE:
1545 return OFPUTIL_P_NONE;
1548 match_set_dp_hash_masked(match, ntohl(value->be32), ntohl(mask->be32));
1551 match_set_tun_id_masked(match, value->be64, mask->be64);
1554 match_set_tun_src_masked(match, value->be32, mask->be32);
1557 match_set_tun_dst_masked(match, value->be32, mask->be32);
1560 match_set_tun_flags_masked(match, ntohs(value->be16), ntohs(mask->be16));
1562 case MFF_TUN_GBP_ID:
1563 match_set_tun_gbp_id_masked(match, value->be16, mask->be16);
1565 case MFF_TUN_GBP_FLAGS:
1566 match_set_tun_gbp_flags_masked(match, value->u8, mask->u8);
1569 match_set_tun_ttl_masked(match, value->u8, mask->u8);
1572 match_set_tun_tos_masked(match, value->u8, mask->u8);
1576 match_set_metadata_masked(match, value->be64, mask->be64);
1580 match_set_reg_masked(match, mf->id - MFF_REG0,
1581 ntohl(value->be32), ntohl(mask->be32));
1585 match_set_xreg_masked(match, mf->id - MFF_XREG0,
1586 ntohll(value->be64), ntohll(mask->be64));
1590 match_set_pkt_mark_masked(match, ntohl(value->be32),
1595 match_set_dl_dst_masked(match, value->mac, mask->mac);
1599 match_set_dl_src_masked(match, value->mac, mask->mac);
1604 match_set_arp_sha_masked(match, value->mac, mask->mac);
1609 match_set_arp_tha_masked(match, value->mac, mask->mac);
1613 match_set_dl_tci_masked(match, value->be16, mask->be16);
1617 match_set_vlan_vid_masked(match, value->be16, mask->be16);
1621 match_set_nw_src_masked(match, value->be32, mask->be32);
1625 match_set_nw_dst_masked(match, value->be32, mask->be32);
1629 match_set_ipv6_src_masked(match, &value->ipv6, &mask->ipv6);
1633 match_set_ipv6_dst_masked(match, &value->ipv6, &mask->ipv6);
1636 case MFF_IPV6_LABEL:
1637 if ((mask->be32 & htonl(IPV6_LABEL_MASK)) == htonl(IPV6_LABEL_MASK)) {
1638 mf_set_value(mf, value, match);
1640 match_set_ipv6_label_masked(match, value->be32, mask->be32);
1645 match_set_nd_target_masked(match, &value->ipv6, &mask->ipv6);
1649 match_set_nw_frag_masked(match, value->u8, mask->u8);
1653 match_set_nw_src_masked(match, value->be32, mask->be32);
1657 match_set_nw_dst_masked(match, value->be32, mask->be32);
1663 match_set_tp_src_masked(match, value->be16, mask->be16);
1669 match_set_tp_dst_masked(match, value->be16, mask->be16);
1673 match_set_tcp_flags_masked(match, value->be16, mask->be16);
1681 return ((mf->usable_protocols_bitwise == mf->usable_protocols_cidr
1682 || ip_is_cidr(mask->be32))
1683 ? mf->usable_protocols_cidr
1684 : mf->usable_protocols_bitwise);
1688 mf_check__(const struct mf_subfield *sf, const struct flow *flow,
1692 VLOG_WARN_RL(&rl, "unknown %s field", type);
1693 return OFPERR_OFPBAC_BAD_SET_TYPE;
1694 } else if (!sf->n_bits) {
1695 VLOG_WARN_RL(&rl, "zero bit %s field %s", type, sf->field->name);
1696 return OFPERR_OFPBAC_BAD_SET_LEN;
1697 } else if (sf->ofs >= sf->field->n_bits) {
1698 VLOG_WARN_RL(&rl, "bit offset %d exceeds %d-bit width of %s field %s",
1699 sf->ofs, sf->field->n_bits, type, sf->field->name);
1700 return OFPERR_OFPBAC_BAD_SET_LEN;
1701 } else if (sf->ofs + sf->n_bits > sf->field->n_bits) {
1702 VLOG_WARN_RL(&rl, "bit offset %d and width %d exceeds %d-bit width "
1703 "of %s field %s", sf->ofs, sf->n_bits,
1704 sf->field->n_bits, type, sf->field->name);
1705 return OFPERR_OFPBAC_BAD_SET_LEN;
1706 } else if (flow && !mf_are_prereqs_ok(sf->field, flow)) {
1707 VLOG_WARN_RL(&rl, "%s field %s lacks correct prerequisites",
1708 type, sf->field->name);
1709 return OFPERR_OFPBAC_MATCH_INCONSISTENT;
1715 /* Checks whether 'sf' is valid for reading a subfield out of 'flow'. Returns
1716 * 0 if so, otherwise an OpenFlow error code (e.g. as returned by
1719 mf_check_src(const struct mf_subfield *sf, const struct flow *flow)
1721 return mf_check__(sf, flow, "source");
1724 /* Checks whether 'sf' is valid for writing a subfield into 'flow'. Returns 0
1725 * if so, otherwise an OpenFlow error code (e.g. as returned by
1728 mf_check_dst(const struct mf_subfield *sf, const struct flow *flow)
1730 int error = mf_check__(sf, flow, "destination");
1731 if (!error && !sf->field->writable) {
1732 VLOG_WARN_RL(&rl, "destination field %s is not writable",
1734 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
1739 /* Copies the value and wildcard bit pattern for 'mf' from 'match' into the
1740 * 'value' and 'mask', respectively. */
1742 mf_get(const struct mf_field *mf, const struct match *match,
1743 union mf_value *value, union mf_value *mask)
1745 mf_get_value(mf, &match->flow, value);
1746 mf_get_mask(mf, &match->wc, mask);
1750 mf_from_integer_string(const struct mf_field *mf, const char *s,
1751 uint8_t *valuep, uint8_t *maskp)
1754 const char *err_str = "";
1757 err = parse_int_string(s, valuep, mf->n_bytes, &tail);
1758 if (err || (*tail != '\0' && *tail != '/')) {
1764 err = parse_int_string(tail + 1, maskp, mf->n_bytes, &tail);
1765 if (err || *tail != '\0') {
1770 memset(maskp, 0xff, mf->n_bytes);
1776 if (err == ERANGE) {
1777 return xasprintf("%s: %s too large for %u-byte field %s",
1778 s, err_str, mf->n_bytes, mf->name);
1780 return xasprintf("%s: bad syntax for %s %s", s, mf->name, err_str);
1785 mf_from_ethernet_string(const struct mf_field *mf, const char *s,
1786 uint8_t mac[ETH_ADDR_LEN],
1787 uint8_t mask[ETH_ADDR_LEN])
1791 ovs_assert(mf->n_bytes == ETH_ADDR_LEN);
1794 if (ovs_scan(s, ETH_ADDR_SCAN_FMT"%n", ETH_ADDR_SCAN_ARGS(mac), &n)
1795 && n == strlen(s)) {
1796 memset(mask, 0xff, ETH_ADDR_LEN);
1801 if (ovs_scan(s, ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT"%n",
1802 ETH_ADDR_SCAN_ARGS(mac), ETH_ADDR_SCAN_ARGS(mask), &n)
1803 && n == strlen(s)) {
1807 return xasprintf("%s: invalid Ethernet address", s);
1811 mf_from_ipv4_string(const struct mf_field *mf, const char *s,
1812 ovs_be32 *ip, ovs_be32 *mask)
1816 ovs_assert(mf->n_bytes == sizeof *ip);
1818 if (ovs_scan(s, IP_SCAN_FMT"/"IP_SCAN_FMT,
1819 IP_SCAN_ARGS(ip), IP_SCAN_ARGS(mask))) {
1821 } else if (ovs_scan(s, IP_SCAN_FMT"/%d", IP_SCAN_ARGS(ip), &prefix)) {
1822 if (prefix <= 0 || prefix > 32) {
1823 return xasprintf("%s: network prefix bits not between 0 and "
1826 *mask = be32_prefix_mask(prefix);
1827 } else if (ovs_scan(s, IP_SCAN_FMT, IP_SCAN_ARGS(ip))) {
1828 *mask = OVS_BE32_MAX;
1830 return xasprintf("%s: invalid IP address", s);
1836 mf_from_ipv6_string(const struct mf_field *mf, const char *s,
1837 struct in6_addr *value, struct in6_addr *mask)
1839 char *str = xstrdup(s);
1840 char *save_ptr = NULL;
1841 const char *name, *netmask;
1844 ovs_assert(mf->n_bytes == sizeof *value);
1846 name = strtok_r(str, "/", &save_ptr);
1847 retval = name ? lookup_ipv6(name, value) : EINVAL;
1851 err = xasprintf("%s: could not convert to IPv6 address", str);
1857 netmask = strtok_r(NULL, "/", &save_ptr);
1859 if (inet_pton(AF_INET6, netmask, mask) != 1) {
1860 int prefix = atoi(netmask);
1861 if (prefix <= 0 || prefix > 128) {
1863 return xasprintf("%s: prefix bits not between 1 and 128", s);
1865 *mask = ipv6_create_mask(prefix);
1869 *mask = in6addr_exact;
1877 mf_from_ofp_port_string(const struct mf_field *mf, const char *s,
1878 ovs_be16 *valuep, ovs_be16 *maskp)
1882 ovs_assert(mf->n_bytes == sizeof(ovs_be16));
1884 if (ofputil_port_from_string(s, &port)) {
1885 *valuep = htons(ofp_to_u16(port));
1886 *maskp = OVS_BE16_MAX;
1889 return xasprintf("%s: port value out of range for %s", s, mf->name);
1893 mf_from_ofp_port_string32(const struct mf_field *mf, const char *s,
1894 ovs_be32 *valuep, ovs_be32 *maskp)
1898 ovs_assert(mf->n_bytes == sizeof(ovs_be32));
1899 if (ofputil_port_from_string(s, &port)) {
1900 *valuep = ofputil_port_to_ofp11(port);
1901 *maskp = OVS_BE32_MAX;
1904 return xasprintf("%s: port value out of range for %s", s, mf->name);
1907 struct frag_handling {
1913 static const struct frag_handling all_frags[] = {
1914 #define A FLOW_NW_FRAG_ANY
1915 #define L FLOW_NW_FRAG_LATER
1916 /* name mask value */
1919 { "first", A|L, A },
1920 { "later", A|L, A|L },
1925 { "not_later", L, 0 },
1932 mf_from_frag_string(const char *s, uint8_t *valuep, uint8_t *maskp)
1934 const struct frag_handling *h;
1936 for (h = all_frags; h < &all_frags[ARRAY_SIZE(all_frags)]; h++) {
1937 if (!strcasecmp(s, h->name)) {
1938 /* We force the upper bits of the mask on to make mf_parse_value()
1939 * happy (otherwise it will never think it's an exact match.) */
1940 *maskp = h->mask | ~FLOW_NW_FRAG_MASK;
1946 return xasprintf("%s: unknown fragment type (valid types are \"no\", "
1947 "\"yes\", \"first\", \"later\", \"not_first\"", s);
1951 parse_flow_tun_flags(const char *s_, const char *(*bit_to_string)(uint32_t),
1954 uint32_t result = 0;
1955 char *save_ptr = NULL;
1958 char *s = xstrdup(s_);
1960 for (name = strtok_r((char *)s, " |", &save_ptr); name;
1961 name = strtok_r(NULL, " |", &save_ptr)) {
1963 unsigned long long int flags;
1966 if (ovs_scan(name, "%lli", &flags)) {
1970 name_len = strlen(name);
1971 for (bit = 1; bit; bit <<= 1) {
1972 const char *fname = bit_to_string(bit);
1979 len = strlen(fname);
1980 if (len != name_len) {
1983 if (!strncmp(name, fname, len)) {
1995 *res = htons(result);
2002 mf_from_tun_flags_string(const char *s, ovs_be16 *valuep, ovs_be16 *maskp)
2004 if (!parse_flow_tun_flags(s, flow_tun_flag_to_string, valuep)) {
2005 *maskp = OVS_BE16_MAX;
2009 return xasprintf("%s: unknown tunnel flags (valid flags are \"df\", "
2010 "\"csum\", \"key\")", s);
2014 mf_from_tcp_flags_string(const char *s, ovs_be16 *flagsp, ovs_be16 *maskp)
2021 if (ovs_scan(s, "%"SCNi16"/%"SCNi16"%n", &flags, &mask, &n) && !s[n]) {
2022 *flagsp = htons(flags);
2023 *maskp = htons(mask);
2026 if (ovs_scan(s, "%"SCNi16"%n", &flags, &n) && !s[n]) {
2027 *flagsp = htons(flags);
2028 *maskp = OVS_BE16_MAX;
2032 while (*s != '\0') {
2044 return xasprintf("%s: TCP flag must be preceded by '+' (for SET) "
2045 "or '-' (NOT SET)", s);
2049 name_len = strcspn(s,"+-");
2051 for (bit = 1; bit; bit <<= 1) {
2052 const char *fname = packet_tcp_flag_to_string(bit);
2059 len = strlen(fname);
2060 if (len != name_len) {
2063 if (!strncmp(s, fname, len)) {
2065 return xasprintf("%s: Each TCP flag can be specified only "
2077 return xasprintf("%s: unknown TCP flag(s)", s);
2082 *flagsp = htons(flags);
2083 *maskp = htons(mask);
2088 /* Parses 's', a string value for field 'mf', into 'value' and 'mask'. Returns
2089 * NULL if successful, otherwise a malloc()'d string describing the error. */
2091 mf_parse(const struct mf_field *mf, const char *s,
2092 union mf_value *value, union mf_value *mask)
2096 if (!strcmp(s, "*")) {
2097 memset(value, 0, mf->n_bytes);
2098 memset(mask, 0, mf->n_bytes);
2102 switch (mf->string) {
2104 case MFS_HEXADECIMAL:
2105 error = mf_from_integer_string(mf, s,
2106 (uint8_t *) value, (uint8_t *) mask);
2110 error = mf_from_ethernet_string(mf, s, value->mac, mask->mac);
2114 error = mf_from_ipv4_string(mf, s, &value->be32, &mask->be32);
2118 error = mf_from_ipv6_string(mf, s, &value->ipv6, &mask->ipv6);
2122 error = mf_from_ofp_port_string(mf, s, &value->be16, &mask->be16);
2125 case MFS_OFP_PORT_OXM:
2126 error = mf_from_ofp_port_string32(mf, s, &value->be32, &mask->be32);
2130 error = mf_from_frag_string(s, &value->u8, &mask->u8);
2134 ovs_assert(mf->n_bytes == sizeof(ovs_be16));
2135 error = mf_from_tun_flags_string(s, &value->be16, &mask->be16);
2139 ovs_assert(mf->n_bytes == sizeof(ovs_be16));
2140 error = mf_from_tcp_flags_string(s, &value->be16, &mask->be16);
2147 if (!error && !mf_is_mask_valid(mf, mask)) {
2148 error = xasprintf("%s: invalid mask for field %s", s, mf->name);
2153 /* Parses 's', a string value for field 'mf', into 'value'. Returns NULL if
2154 * successful, otherwise a malloc()'d string describing the error. */
2156 mf_parse_value(const struct mf_field *mf, const char *s, union mf_value *value)
2158 union mf_value mask;
2161 error = mf_parse(mf, s, value, &mask);
2166 if (!is_all_ones((const uint8_t *) &mask, mf->n_bytes)) {
2167 return xasprintf("%s: wildcards not allowed here", s);
2173 mf_format_integer_string(const struct mf_field *mf, const uint8_t *valuep,
2174 const uint8_t *maskp, struct ds *s)
2176 if (mf->string == MFS_HEXADECIMAL) {
2177 ds_put_hex(s, valuep, mf->n_bytes);
2179 unsigned long long int integer = 0;
2182 ovs_assert(mf->n_bytes <= 8);
2183 for (i = 0; i < mf->n_bytes; i++) {
2184 integer = (integer << 8) | valuep[i];
2186 ds_put_format(s, "%lld", integer);
2190 /* I guess we could write the mask in decimal for MFS_DECIMAL but I'm
2191 * not sure that that a bit-mask written in decimal is ever easier to
2192 * understand than the same bit-mask written in hexadecimal. */
2193 ds_put_char(s, '/');
2194 ds_put_hex(s, maskp, mf->n_bytes);
2199 mf_format_frag_string(uint8_t value, uint8_t mask, struct ds *s)
2201 const struct frag_handling *h;
2203 mask &= FLOW_NW_FRAG_MASK;
2206 for (h = all_frags; h < &all_frags[ARRAY_SIZE(all_frags)]; h++) {
2207 if (value == h->value && mask == h->mask) {
2208 ds_put_cstr(s, h->name);
2212 ds_put_cstr(s, "<error>");
2216 mf_format_tnl_flags_string(const ovs_be16 *valuep, struct ds *s)
2218 format_flags(s, flow_tun_flag_to_string, ntohs(*valuep), '|');
2222 mf_format_tcp_flags_string(ovs_be16 value, ovs_be16 mask, struct ds *s)
2224 format_flags_masked(s, NULL, packet_tcp_flag_to_string, ntohs(value),
2228 /* Appends to 's' a string representation of field 'mf' whose value is in
2229 * 'value' and 'mask'. 'mask' may be NULL to indicate an exact match. */
2231 mf_format(const struct mf_field *mf,
2232 const union mf_value *value, const union mf_value *mask,
2236 if (is_all_zeros(mask, mf->n_bytes)) {
2237 ds_put_cstr(s, "ANY");
2239 } else if (is_all_ones(mask, mf->n_bytes)) {
2244 switch (mf->string) {
2245 case MFS_OFP_PORT_OXM:
2248 ofputil_port_from_ofp11(value->be32, &port);
2249 ofputil_format_port(port, s);
2255 ofputil_format_port(u16_to_ofp(ntohs(value->be16)), s);
2260 case MFS_HEXADECIMAL:
2261 mf_format_integer_string(mf, (uint8_t *) value, (uint8_t *) mask, s);
2265 eth_format_masked(value->mac, mask->mac, s);
2269 ip_format_masked(value->be32, mask ? mask->be32 : OVS_BE32_MAX, s);
2273 print_ipv6_masked(s, &value->ipv6, mask ? &mask->ipv6 : NULL);
2277 mf_format_frag_string(value->u8, mask ? mask->u8 : UINT8_MAX, s);
2281 mf_format_tnl_flags_string(&value->be16, s);
2285 mf_format_tcp_flags_string(value->be16,
2286 mask ? mask->be16 : OVS_BE16_MAX, s);
2294 /* Makes subfield 'sf' within 'flow' exactly match the 'sf->n_bits'
2295 * least-significant bits in 'x'.
2298 mf_write_subfield_flow(const struct mf_subfield *sf,
2299 const union mf_subvalue *x, struct flow *flow)
2301 const struct mf_field *field = sf->field;
2302 union mf_value value;
2304 mf_get_value(field, flow, &value);
2305 bitwise_copy(x, sizeof *x, 0, &value, field->n_bytes,
2306 sf->ofs, sf->n_bits);
2307 mf_set_flow_value(field, &value, flow);
2310 /* Makes subfield 'sf' within 'match' exactly match the 'sf->n_bits'
2311 * least-significant bits in 'x'.
2314 mf_write_subfield(const struct mf_subfield *sf, const union mf_subvalue *x,
2315 struct match *match)
2317 const struct mf_field *field = sf->field;
2318 union mf_value value, mask;
2320 mf_get(field, match, &value, &mask);
2321 bitwise_copy(x, sizeof *x, 0, &value, field->n_bytes, sf->ofs, sf->n_bits);
2322 bitwise_one ( &mask, field->n_bytes, sf->ofs, sf->n_bits);
2323 mf_set(field, &value, &mask, match);
2326 /* 'v' and 'm' correspond to values of 'field'. This function copies them into
2327 * 'match' in the correspond positions. */
2329 mf_mask_subfield(const struct mf_field *field,
2330 const union mf_subvalue *v,
2331 const union mf_subvalue *m,
2332 struct match *match)
2334 union mf_value value, mask;
2336 mf_get(field, match, &value, &mask);
2337 bitwise_copy(v, sizeof *v, 0, &value, field->n_bytes, 0, field->n_bits);
2338 bitwise_copy(m, sizeof *m, 0, &mask, field->n_bytes, 0, field->n_bits);
2339 mf_set(field, &value, &mask, match);
2342 /* Initializes 'x' to the value of 'sf' within 'flow'. 'sf' must be valid for
2343 * reading 'flow', e.g. as checked by mf_check_src(). */
2345 mf_read_subfield(const struct mf_subfield *sf, const struct flow *flow,
2346 union mf_subvalue *x)
2348 union mf_value value;
2350 mf_get_value(sf->field, flow, &value);
2352 memset(x, 0, sizeof *x);
2353 bitwise_copy(&value, sf->field->n_bytes, sf->ofs,
2358 /* Returns the value of 'sf' within 'flow'. 'sf' must be valid for reading
2359 * 'flow', e.g. as checked by mf_check_src() and sf->n_bits must be 64 or
2362 mf_get_subfield(const struct mf_subfield *sf, const struct flow *flow)
2364 union mf_value value;
2366 mf_get_value(sf->field, flow, &value);
2367 return bitwise_get(&value, sf->field->n_bytes, sf->ofs, sf->n_bits);
2371 mf_format_subvalue(const union mf_subvalue *subvalue, struct ds *s)
2373 ds_put_hex(s, subvalue->u8, sizeof subvalue->u8);
2377 field_array_set(enum mf_field_id id, const union mf_value *value,
2378 struct field_array *fa)
2380 ovs_assert(id < MFF_N_IDS);
2381 bitmap_set1(fa->used.bm, id);
2382 fa->value[id] = *value;