2 * Copyright (c) 2011, 2012, 2013, 2014 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 /* Returns true if 'wc' wildcards all the bits in field 'mf', false if 'wc'
99 * specifies at least one bit in the field.
101 * The caller is responsible for ensuring that 'wc' corresponds to a flow that
102 * meets 'mf''s prerequisites. */
104 mf_is_all_wild(const struct mf_field *mf, const struct flow_wildcards *wc)
108 return !wc->masks.dp_hash;
110 return !wc->masks.recirc_id;
112 return !wc->masks.conj_id;
114 return !wc->masks.tunnel.ip_src;
116 return !wc->masks.tunnel.ip_dst;
121 return !wc->masks.tunnel.tun_id;
123 return !wc->masks.tunnel.gbp_id;
124 case MFF_TUN_GBP_FLAGS:
125 return !wc->masks.tunnel.gbp_flags;
127 return !wc->masks.metadata;
129 case MFF_IN_PORT_OXM:
130 return !wc->masks.in_port.ofp_port;
131 case MFF_SKB_PRIORITY:
132 return !wc->masks.skb_priority;
134 return !wc->masks.pkt_mark;
136 return !wc->masks.regs[mf->id - MFF_REG0];
138 return !flow_get_xreg(&wc->masks, mf->id - MFF_XREG0);
139 case MFF_ACTSET_OUTPUT:
140 return !wc->masks.actset_output;
143 return eth_addr_is_zero(wc->masks.dl_src);
145 return eth_addr_is_zero(wc->masks.dl_dst);
147 return !wc->masks.dl_type;
151 return eth_addr_is_zero(wc->masks.arp_sha);
155 return eth_addr_is_zero(wc->masks.arp_tha);
158 return !wc->masks.vlan_tci;
160 return !(wc->masks.vlan_tci & htons(VLAN_VID_MASK));
162 return !(wc->masks.vlan_tci & htons(VLAN_VID_MASK | VLAN_CFI));
163 case MFF_DL_VLAN_PCP:
165 return !(wc->masks.vlan_tci & htons(VLAN_PCP_MASK));
168 return !(wc->masks.mpls_lse[0] & htonl(MPLS_LABEL_MASK));
170 return !(wc->masks.mpls_lse[0] & htonl(MPLS_TC_MASK));
172 return !(wc->masks.mpls_lse[0] & htonl(MPLS_BOS_MASK));
175 return !wc->masks.nw_src;
177 return !wc->masks.nw_dst;
180 return ipv6_mask_is_any(&wc->masks.ipv6_src);
182 return ipv6_mask_is_any(&wc->masks.ipv6_dst);
185 return !wc->masks.ipv6_label;
188 return !wc->masks.nw_proto;
190 case MFF_IP_DSCP_SHIFTED:
191 return !(wc->masks.nw_tos & IP_DSCP_MASK);
193 return !(wc->masks.nw_tos & IP_ECN_MASK);
195 return !wc->masks.nw_ttl;
198 return ipv6_mask_is_any(&wc->masks.nd_target);
201 return !(wc->masks.nw_frag & FLOW_NW_FRAG_MASK);
204 return !wc->masks.nw_proto;
206 return !wc->masks.nw_src;
208 return !wc->masks.nw_dst;
213 case MFF_ICMPV4_TYPE:
214 case MFF_ICMPV6_TYPE:
215 return !wc->masks.tp_src;
219 case MFF_ICMPV4_CODE:
220 case MFF_ICMPV6_CODE:
221 return !wc->masks.tp_dst;
223 return !wc->masks.tcp_flags;
231 /* Initializes 'mask' with the wildcard bit pattern for field 'mf' within 'wc'.
232 * Each bit in 'mask' will be set to 1 if the bit is significant for matching
233 * purposes, or to 0 if it is wildcarded.
235 * The caller is responsible for ensuring that 'wc' corresponds to a flow that
236 * meets 'mf''s prerequisites. */
238 mf_get_mask(const struct mf_field *mf, const struct flow_wildcards *wc,
239 union mf_value *mask)
241 mf_get_value(mf, &wc->masks, mask);
244 /* Tests whether 'mask' is a valid wildcard bit pattern for 'mf'. Returns true
245 * if the mask is valid, false otherwise. */
247 mf_is_mask_valid(const struct mf_field *mf, const union mf_value *mask)
249 switch (mf->maskable) {
251 return (is_all_zeros(mask, mf->n_bytes) ||
252 is_all_ones(mask, mf->n_bytes));
261 /* Returns true if 'flow' meets the prerequisites for 'mf', false otherwise. */
263 mf_are_prereqs_ok(const struct mf_field *mf, const struct flow *flow)
265 switch (mf->prereqs) {
270 return (flow->dl_type == htons(ETH_TYPE_ARP) ||
271 flow->dl_type == htons(ETH_TYPE_RARP));
273 return flow->dl_type == htons(ETH_TYPE_IP);
275 return flow->dl_type == htons(ETH_TYPE_IPV6);
277 return (flow->vlan_tci & htons(VLAN_CFI)) != 0;
279 return eth_type_mpls(flow->dl_type);
281 return is_ip_any(flow);
284 return is_ip_any(flow) && flow->nw_proto == IPPROTO_TCP
285 && !(flow->nw_frag & FLOW_NW_FRAG_LATER);
287 return is_ip_any(flow) && flow->nw_proto == IPPROTO_UDP
288 && !(flow->nw_frag & FLOW_NW_FRAG_LATER);
290 return is_ip_any(flow) && flow->nw_proto == IPPROTO_SCTP
291 && !(flow->nw_frag & FLOW_NW_FRAG_LATER);
293 return is_icmpv4(flow);
295 return is_icmpv6(flow);
298 return (is_icmpv6(flow)
299 && flow->tp_dst == htons(0)
300 && (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT) ||
301 flow->tp_src == htons(ND_NEIGHBOR_ADVERT)));
303 return (is_icmpv6(flow)
304 && flow->tp_dst == htons(0)
305 && (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT)));
307 return (is_icmpv6(flow)
308 && flow->tp_dst == htons(0)
309 && (flow->tp_src == htons(ND_NEIGHBOR_ADVERT)));
315 /* Set field and it's prerequisities in the mask.
316 * This is only ever called for writeable 'mf's, but we do not make the
317 * distinction here. */
319 mf_mask_field_and_prereqs(const struct mf_field *mf, struct flow *mask)
321 static const union mf_value exact_match_mask = MF_EXACT_MASK_INITIALIZER;
323 mf_set_flow_value(mf, &exact_match_mask, mask);
325 switch (mf->prereqs) {
329 mask->tp_src = OVS_BE16_MAX;
330 mask->tp_dst = OVS_BE16_MAX;
337 /* nw_frag always unwildcarded. */
338 mask->nw_proto = 0xff;
345 mask->dl_type = OVS_BE16_MAX;
348 mask->vlan_tci |= htons(VLAN_CFI);
356 /* Returns true if 'value' may be a valid value *as part of a masked match*,
359 * A value is not rejected just because it is not valid for the field in
360 * question, but only if it doesn't make sense to test the bits in question at
361 * all. For example, the MFF_VLAN_TCI field will never have a nonzero value
362 * without the VLAN_CFI bit being set, but we can't reject those values because
363 * it is still legitimate to test just for those bits (see the documentation
364 * for NXM_OF_VLAN_TCI in nicira-ext.h). On the other hand, there is never a
365 * reason to set the low bit of MFF_IP_DSCP to 1, so we reject that. */
367 mf_is_value_valid(const struct mf_field *mf, const union mf_value *value)
380 case MFF_TUN_GBP_FLAGS:
383 case MFF_SKB_PRIORITY:
407 case MFF_ICMPV4_TYPE:
408 case MFF_ICMPV4_CODE:
409 case MFF_ICMPV6_TYPE:
410 case MFF_ICMPV6_CODE:
416 case MFF_IN_PORT_OXM:
417 case MFF_ACTSET_OUTPUT: {
419 return !ofputil_port_from_ofp11(value->be32, &port);
423 return !(value->u8 & ~IP_DSCP_MASK);
424 case MFF_IP_DSCP_SHIFTED:
425 return !(value->u8 & (~IP_DSCP_MASK >> 2));
427 return !(value->u8 & ~IP_ECN_MASK);
429 return !(value->u8 & ~FLOW_NW_FRAG_MASK);
431 return !(value->be16 & ~htons(0x0fff));
434 return !(value->be16 & htons(0xff00));
437 return !(value->be16 & htons(VLAN_CFI | VLAN_PCP_MASK));
439 return !(value->be16 & htons(VLAN_PCP_MASK));
441 case MFF_DL_VLAN_PCP:
443 return !(value->u8 & ~(VLAN_PCP_MASK >> VLAN_PCP_SHIFT));
446 return !(value->be32 & ~htonl(IPV6_LABEL_MASK));
449 return !(value->be32 & ~htonl(MPLS_LABEL_MASK >> MPLS_LABEL_SHIFT));
452 return !(value->u8 & ~(MPLS_TC_MASK >> MPLS_TC_SHIFT));
455 return !(value->u8 & ~(MPLS_BOS_MASK >> MPLS_BOS_SHIFT));
463 /* Copies the value of field 'mf' from 'flow' into 'value'. The caller is
464 * responsible for ensuring that 'flow' meets 'mf''s prerequisites. */
466 mf_get_value(const struct mf_field *mf, const struct flow *flow,
467 union mf_value *value)
471 value->be32 = htonl(flow->dp_hash);
474 value->be32 = htonl(flow->recirc_id);
477 value->be32 = htonl(flow->conj_id);
480 value->be64 = flow->tunnel.tun_id;
483 value->be32 = flow->tunnel.ip_src;
486 value->be32 = flow->tunnel.ip_dst;
489 value->be16 = htons(flow->tunnel.flags);
492 value->be16 = flow->tunnel.gbp_id;
494 case MFF_TUN_GBP_FLAGS:
495 value->u8 = flow->tunnel.gbp_flags;
498 value->u8 = flow->tunnel.ip_ttl;
501 value->u8 = flow->tunnel.ip_tos;
505 value->be64 = flow->metadata;
509 value->be16 = htons(ofp_to_u16(flow->in_port.ofp_port));
511 case MFF_IN_PORT_OXM:
512 value->be32 = ofputil_port_to_ofp11(flow->in_port.ofp_port);
514 case MFF_ACTSET_OUTPUT:
515 value->be32 = ofputil_port_to_ofp11(flow->actset_output);
518 case MFF_SKB_PRIORITY:
519 value->be32 = htonl(flow->skb_priority);
523 value->be32 = htonl(flow->pkt_mark);
527 value->be32 = htonl(flow->regs[mf->id - MFF_REG0]);
531 value->be64 = htonll(flow_get_xreg(flow, mf->id - MFF_XREG0));
535 memcpy(value->mac, flow->dl_src, ETH_ADDR_LEN);
539 memcpy(value->mac, flow->dl_dst, ETH_ADDR_LEN);
543 value->be16 = flow->dl_type;
547 value->be16 = flow->vlan_tci;
551 value->be16 = flow->vlan_tci & htons(VLAN_VID_MASK);
554 value->be16 = flow->vlan_tci & htons(VLAN_VID_MASK | VLAN_CFI);
557 case MFF_DL_VLAN_PCP:
559 value->u8 = vlan_tci_to_pcp(flow->vlan_tci);
563 value->be32 = htonl(mpls_lse_to_label(flow->mpls_lse[0]));
567 value->u8 = mpls_lse_to_tc(flow->mpls_lse[0]);
571 value->u8 = mpls_lse_to_bos(flow->mpls_lse[0]);
575 value->be32 = flow->nw_src;
579 value->be32 = flow->nw_dst;
583 value->ipv6 = flow->ipv6_src;
587 value->ipv6 = flow->ipv6_dst;
591 value->be32 = flow->ipv6_label;
595 value->u8 = flow->nw_proto;
599 value->u8 = flow->nw_tos & IP_DSCP_MASK;
602 case MFF_IP_DSCP_SHIFTED:
603 value->u8 = flow->nw_tos >> 2;
607 value->u8 = flow->nw_tos & IP_ECN_MASK;
611 value->u8 = flow->nw_ttl;
615 value->u8 = flow->nw_frag;
619 value->be16 = htons(flow->nw_proto);
623 value->be32 = flow->nw_src;
627 value->be32 = flow->nw_dst;
632 memcpy(value->mac, flow->arp_sha, ETH_ADDR_LEN);
637 memcpy(value->mac, flow->arp_tha, ETH_ADDR_LEN);
643 value->be16 = flow->tp_src;
649 value->be16 = flow->tp_dst;
653 value->be16 = flow->tcp_flags;
656 case MFF_ICMPV4_TYPE:
657 case MFF_ICMPV6_TYPE:
658 value->u8 = ntohs(flow->tp_src);
661 case MFF_ICMPV4_CODE:
662 case MFF_ICMPV6_CODE:
663 value->u8 = ntohs(flow->tp_dst);
667 value->ipv6 = flow->nd_target;
676 /* Makes 'match' match field 'mf' exactly, with the value matched taken from
677 * 'value'. The caller is responsible for ensuring that 'match' meets 'mf''s
680 mf_set_value(const struct mf_field *mf,
681 const union mf_value *value, struct match *match)
685 match_set_dp_hash(match, ntohl(value->be32));
688 match_set_recirc_id(match, ntohl(value->be32));
691 match_set_conj_id(match, ntohl(value->be32));
694 match_set_tun_id(match, value->be64);
697 match_set_tun_src(match, value->be32);
700 match_set_tun_dst(match, value->be32);
703 match_set_tun_flags(match, ntohs(value->be16));
706 match_set_tun_gbp_id(match, value->be16);
708 case MFF_TUN_GBP_FLAGS:
709 match_set_tun_gbp_flags(match, value->u8);
712 match_set_tun_tos(match, value->u8);
715 match_set_tun_ttl(match, value->u8);
719 match_set_metadata(match, value->be64);
723 match_set_in_port(match, u16_to_ofp(ntohs(value->be16)));
726 case MFF_IN_PORT_OXM: {
728 ofputil_port_from_ofp11(value->be32, &port);
729 match_set_in_port(match, port);
732 case MFF_ACTSET_OUTPUT: {
734 ofputil_port_from_ofp11(value->be32, &port);
735 match_set_actset_output(match, port);
739 case MFF_SKB_PRIORITY:
740 match_set_skb_priority(match, ntohl(value->be32));
744 match_set_pkt_mark(match, ntohl(value->be32));
748 match_set_reg(match, mf->id - MFF_REG0, ntohl(value->be32));
752 match_set_xreg(match, mf->id - MFF_XREG0, ntohll(value->be64));
756 match_set_dl_src(match, value->mac);
760 match_set_dl_dst(match, value->mac);
764 match_set_dl_type(match, value->be16);
768 match_set_dl_tci(match, value->be16);
772 match_set_dl_vlan(match, value->be16);
775 match_set_vlan_vid(match, value->be16);
778 case MFF_DL_VLAN_PCP:
780 match_set_dl_vlan_pcp(match, value->u8);
784 match_set_mpls_label(match, 0, value->be32);
788 match_set_mpls_tc(match, 0, value->u8);
792 match_set_mpls_bos(match, 0, value->u8);
796 match_set_nw_src(match, value->be32);
800 match_set_nw_dst(match, value->be32);
804 match_set_ipv6_src(match, &value->ipv6);
808 match_set_ipv6_dst(match, &value->ipv6);
812 match_set_ipv6_label(match, value->be32);
816 match_set_nw_proto(match, value->u8);
820 match_set_nw_dscp(match, value->u8);
823 case MFF_IP_DSCP_SHIFTED:
824 match_set_nw_dscp(match, value->u8 << 2);
828 match_set_nw_ecn(match, value->u8);
832 match_set_nw_ttl(match, value->u8);
836 match_set_nw_frag(match, value->u8);
840 match_set_nw_proto(match, ntohs(value->be16));
844 match_set_nw_src(match, value->be32);
848 match_set_nw_dst(match, value->be32);
853 match_set_arp_sha(match, value->mac);
858 match_set_arp_tha(match, value->mac);
864 match_set_tp_src(match, value->be16);
870 match_set_tp_dst(match, value->be16);
874 match_set_tcp_flags(match, value->be16);
877 case MFF_ICMPV4_TYPE:
878 case MFF_ICMPV6_TYPE:
879 match_set_icmp_type(match, value->u8);
882 case MFF_ICMPV4_CODE:
883 case MFF_ICMPV6_CODE:
884 match_set_icmp_code(match, value->u8);
888 match_set_nd_target(match, &value->ipv6);
897 /* Unwildcard 'mask' member field described by 'mf'. The caller is
898 * responsible for ensuring that 'mask' meets 'mf''s prerequisites. */
900 mf_mask_field(const struct mf_field *mf, struct flow *mask)
902 static const union mf_value exact_match_mask = MF_EXACT_MASK_INITIALIZER;
904 /* For MFF_DL_VLAN, we cannot send a all 1's to flow_set_dl_vlan()
905 * as that will be considered as OFP10_VLAN_NONE. So consider it as a
906 * special case. For the rest, calling mf_set_flow_value() is good
908 if (mf->id == MFF_DL_VLAN) {
909 flow_set_dl_vlan(mask, htons(VLAN_VID_MASK));
911 mf_set_flow_value(mf, &exact_match_mask, mask);
915 /* Sets 'flow' member field described by 'mf' to 'value'. The caller is
916 * responsible for ensuring that 'flow' meets 'mf''s prerequisites.*/
918 mf_set_flow_value(const struct mf_field *mf,
919 const union mf_value *value, struct flow *flow)
923 flow->dp_hash = ntohl(value->be32);
926 flow->recirc_id = ntohl(value->be32);
929 flow->conj_id = ntohl(value->be32);
932 flow->tunnel.tun_id = value->be64;
935 flow->tunnel.ip_src = value->be32;
938 flow->tunnel.ip_dst = value->be32;
941 flow->tunnel.flags = ntohs(value->be16);
944 flow->tunnel.gbp_id = value->be16;
946 case MFF_TUN_GBP_FLAGS:
947 flow->tunnel.gbp_flags = value->u8;
950 flow->tunnel.ip_tos = value->u8;
953 flow->tunnel.ip_ttl = value->u8;
957 flow->metadata = value->be64;
961 flow->in_port.ofp_port = u16_to_ofp(ntohs(value->be16));
964 case MFF_IN_PORT_OXM:
965 ofputil_port_from_ofp11(value->be32, &flow->in_port.ofp_port);
967 case MFF_ACTSET_OUTPUT:
968 ofputil_port_from_ofp11(value->be32, &flow->actset_output);
971 case MFF_SKB_PRIORITY:
972 flow->skb_priority = ntohl(value->be32);
976 flow->pkt_mark = ntohl(value->be32);
980 flow->regs[mf->id - MFF_REG0] = ntohl(value->be32);
984 flow_set_xreg(flow, mf->id - MFF_XREG0, ntohll(value->be64));
988 memcpy(flow->dl_src, value->mac, ETH_ADDR_LEN);
992 memcpy(flow->dl_dst, value->mac, ETH_ADDR_LEN);
996 flow->dl_type = value->be16;
1000 flow->vlan_tci = value->be16;
1004 flow_set_dl_vlan(flow, value->be16);
1007 flow_set_vlan_vid(flow, value->be16);
1010 case MFF_DL_VLAN_PCP:
1012 flow_set_vlan_pcp(flow, value->u8);
1015 case MFF_MPLS_LABEL:
1016 flow_set_mpls_label(flow, 0, value->be32);
1020 flow_set_mpls_tc(flow, 0, value->u8);
1024 flow_set_mpls_bos(flow, 0, value->u8);
1028 flow->nw_src = value->be32;
1032 flow->nw_dst = value->be32;
1036 flow->ipv6_src = value->ipv6;
1040 flow->ipv6_dst = value->ipv6;
1043 case MFF_IPV6_LABEL:
1044 flow->ipv6_label = value->be32 & htonl(IPV6_LABEL_MASK);
1048 flow->nw_proto = value->u8;
1052 flow->nw_tos &= ~IP_DSCP_MASK;
1053 flow->nw_tos |= value->u8 & IP_DSCP_MASK;
1056 case MFF_IP_DSCP_SHIFTED:
1057 flow->nw_tos &= ~IP_DSCP_MASK;
1058 flow->nw_tos |= value->u8 << 2;
1062 flow->nw_tos &= ~IP_ECN_MASK;
1063 flow->nw_tos |= value->u8 & IP_ECN_MASK;
1067 flow->nw_ttl = value->u8;
1071 flow->nw_frag = value->u8 & FLOW_NW_FRAG_MASK;
1075 flow->nw_proto = ntohs(value->be16);
1079 flow->nw_src = value->be32;
1083 flow->nw_dst = value->be32;
1088 memcpy(flow->arp_sha, value->mac, ETH_ADDR_LEN);
1093 memcpy(flow->arp_tha, value->mac, ETH_ADDR_LEN);
1099 flow->tp_src = value->be16;
1105 flow->tp_dst = value->be16;
1109 flow->tcp_flags = value->be16;
1112 case MFF_ICMPV4_TYPE:
1113 case MFF_ICMPV6_TYPE:
1114 flow->tp_src = htons(value->u8);
1117 case MFF_ICMPV4_CODE:
1118 case MFF_ICMPV6_CODE:
1119 flow->tp_dst = htons(value->u8);
1123 flow->nd_target = value->ipv6;
1132 /* Consider each of 'src', 'mask', and 'dst' as if they were arrays of 8*n
1133 * bits. Then, for each 0 <= i < 8 * n such that mask[i] == 1, sets dst[i] =
1136 apply_mask(const uint8_t *src, const uint8_t *mask, uint8_t *dst, size_t n)
1140 for (i = 0; i < n; i++) {
1141 dst[i] = (src[i] & mask[i]) | (dst[i] & ~mask[i]);
1145 /* Sets 'flow' member field described by 'field' to 'value', except that bits
1146 * for which 'mask' has a 0-bit keep their existing values. The caller is
1147 * responsible for ensuring that 'flow' meets 'field''s prerequisites.*/
1149 mf_set_flow_value_masked(const struct mf_field *field,
1150 const union mf_value *value,
1151 const union mf_value *mask,
1156 mf_get_value(field, flow, &tmp);
1157 apply_mask((const uint8_t *) value, (const uint8_t *) mask,
1158 (uint8_t *) &tmp, field->n_bytes);
1159 mf_set_flow_value(field, &tmp, flow);
1162 /* Returns true if 'mf' has a zero value in 'flow', false if it is nonzero.
1164 * The caller is responsible for ensuring that 'flow' meets 'mf''s
1167 mf_is_zero(const struct mf_field *mf, const struct flow *flow)
1169 union mf_value value;
1171 mf_get_value(mf, flow, &value);
1172 return is_all_zeros(&value, mf->n_bytes);
1175 /* Makes 'match' wildcard field 'mf'.
1177 * The caller is responsible for ensuring that 'match' meets 'mf''s
1180 mf_set_wild(const struct mf_field *mf, struct match *match)
1184 match->flow.dp_hash = 0;
1185 match->wc.masks.dp_hash = 0;
1188 match->flow.recirc_id = 0;
1189 match->wc.masks.recirc_id = 0;
1192 match->flow.conj_id = 0;
1193 match->wc.masks.conj_id = 0;
1196 match_set_tun_id_masked(match, htonll(0), htonll(0));
1199 match_set_tun_src_masked(match, htonl(0), htonl(0));
1202 match_set_tun_dst_masked(match, htonl(0), htonl(0));
1205 match_set_tun_flags_masked(match, 0, 0);
1207 case MFF_TUN_GBP_ID:
1208 match_set_tun_gbp_id_masked(match, 0, 0);
1210 case MFF_TUN_GBP_FLAGS:
1211 match_set_tun_gbp_flags_masked(match, 0, 0);
1214 match_set_tun_tos_masked(match, 0, 0);
1217 match_set_tun_ttl_masked(match, 0, 0);
1221 match_set_metadata_masked(match, htonll(0), htonll(0));
1225 case MFF_IN_PORT_OXM:
1226 match->flow.in_port.ofp_port = 0;
1227 match->wc.masks.in_port.ofp_port = 0;
1229 case MFF_ACTSET_OUTPUT:
1230 match->flow.actset_output = 0;
1231 match->wc.masks.actset_output = 0;
1234 case MFF_SKB_PRIORITY:
1235 match->flow.skb_priority = 0;
1236 match->wc.masks.skb_priority = 0;
1240 match->flow.pkt_mark = 0;
1241 match->wc.masks.pkt_mark = 0;
1245 match_set_reg_masked(match, mf->id - MFF_REG0, 0, 0);
1249 match_set_xreg_masked(match, mf->id - MFF_XREG0, 0, 0);
1253 memset(match->flow.dl_src, 0, ETH_ADDR_LEN);
1254 memset(match->wc.masks.dl_src, 0, ETH_ADDR_LEN);
1258 memset(match->flow.dl_dst, 0, ETH_ADDR_LEN);
1259 memset(match->wc.masks.dl_dst, 0, ETH_ADDR_LEN);
1263 match->flow.dl_type = htons(0);
1264 match->wc.masks.dl_type = htons(0);
1268 match_set_dl_tci_masked(match, htons(0), htons(0));
1273 match_set_any_vid(match);
1276 case MFF_DL_VLAN_PCP:
1278 match_set_any_pcp(match);
1281 case MFF_MPLS_LABEL:
1282 match_set_any_mpls_label(match, 0);
1286 match_set_any_mpls_tc(match, 0);
1290 match_set_any_mpls_bos(match, 0);
1295 match_set_nw_src_masked(match, htonl(0), htonl(0));
1300 match_set_nw_dst_masked(match, htonl(0), htonl(0));
1304 memset(&match->wc.masks.ipv6_src, 0, sizeof match->wc.masks.ipv6_src);
1305 memset(&match->flow.ipv6_src, 0, sizeof match->flow.ipv6_src);
1309 memset(&match->wc.masks.ipv6_dst, 0, sizeof match->wc.masks.ipv6_dst);
1310 memset(&match->flow.ipv6_dst, 0, sizeof match->flow.ipv6_dst);
1313 case MFF_IPV6_LABEL:
1314 match->wc.masks.ipv6_label = htonl(0);
1315 match->flow.ipv6_label = htonl(0);
1319 match->wc.masks.nw_proto = 0;
1320 match->flow.nw_proto = 0;
1324 case MFF_IP_DSCP_SHIFTED:
1325 match->wc.masks.nw_tos &= ~IP_DSCP_MASK;
1326 match->flow.nw_tos &= ~IP_DSCP_MASK;
1330 match->wc.masks.nw_tos &= ~IP_ECN_MASK;
1331 match->flow.nw_tos &= ~IP_ECN_MASK;
1335 match->wc.masks.nw_ttl = 0;
1336 match->flow.nw_ttl = 0;
1340 match->wc.masks.nw_frag |= FLOW_NW_FRAG_MASK;
1341 match->flow.nw_frag &= ~FLOW_NW_FRAG_MASK;
1345 match->wc.masks.nw_proto = 0;
1346 match->flow.nw_proto = 0;
1351 memset(match->flow.arp_sha, 0, ETH_ADDR_LEN);
1352 memset(match->wc.masks.arp_sha, 0, ETH_ADDR_LEN);
1357 memset(match->flow.arp_tha, 0, ETH_ADDR_LEN);
1358 memset(match->wc.masks.arp_tha, 0, ETH_ADDR_LEN);
1364 case MFF_ICMPV4_TYPE:
1365 case MFF_ICMPV6_TYPE:
1366 match->wc.masks.tp_src = htons(0);
1367 match->flow.tp_src = htons(0);
1373 case MFF_ICMPV4_CODE:
1374 case MFF_ICMPV6_CODE:
1375 match->wc.masks.tp_dst = htons(0);
1376 match->flow.tp_dst = htons(0);
1380 match->wc.masks.tcp_flags = htons(0);
1381 match->flow.tcp_flags = htons(0);
1385 memset(&match->wc.masks.nd_target, 0,
1386 sizeof match->wc.masks.nd_target);
1387 memset(&match->flow.nd_target, 0, sizeof match->flow.nd_target);
1396 /* Makes 'match' match field 'mf' with the specified 'value' and 'mask'.
1397 * 'value' specifies a value to match and 'mask' specifies a wildcard pattern,
1398 * with a 1-bit indicating that the corresponding value bit must match and a
1399 * 0-bit indicating a don't-care.
1401 * If 'mask' is NULL or points to all-1-bits, then this call is equivalent to
1402 * mf_set_value(mf, value, match). If 'mask' points to all-0-bits, then this
1403 * call is equivalent to mf_set_wild(mf, match).
1405 * 'mask' must be a valid mask for 'mf' (see mf_is_mask_valid()). The caller
1406 * is responsible for ensuring that 'match' meets 'mf''s prerequisites. */
1407 enum ofputil_protocol
1408 mf_set(const struct mf_field *mf,
1409 const union mf_value *value, const union mf_value *mask,
1410 struct match *match)
1412 if (!mask || is_all_ones(mask, mf->n_bytes)) {
1413 mf_set_value(mf, value, match);
1414 return mf->usable_protocols_exact;
1415 } else if (is_all_zeros(mask, mf->n_bytes)) {
1416 mf_set_wild(mf, match);
1417 return OFPUTIL_P_ANY;
1424 case MFF_IN_PORT_OXM:
1425 case MFF_ACTSET_OUTPUT:
1426 case MFF_SKB_PRIORITY:
1429 case MFF_DL_VLAN_PCP:
1431 case MFF_MPLS_LABEL:
1437 case MFF_IP_DSCP_SHIFTED:
1440 case MFF_ICMPV4_TYPE:
1441 case MFF_ICMPV4_CODE:
1442 case MFF_ICMPV6_TYPE:
1443 case MFF_ICMPV6_CODE:
1444 return OFPUTIL_P_NONE;
1447 match_set_dp_hash_masked(match, ntohl(value->be32), ntohl(mask->be32));
1450 match_set_tun_id_masked(match, value->be64, mask->be64);
1453 match_set_tun_src_masked(match, value->be32, mask->be32);
1456 match_set_tun_dst_masked(match, value->be32, mask->be32);
1459 match_set_tun_flags_masked(match, ntohs(value->be16), ntohs(mask->be16));
1461 case MFF_TUN_GBP_ID:
1462 match_set_tun_gbp_id_masked(match, value->be16, mask->be16);
1464 case MFF_TUN_GBP_FLAGS:
1465 match_set_tun_gbp_flags_masked(match, value->u8, mask->u8);
1468 match_set_tun_ttl_masked(match, value->u8, mask->u8);
1471 match_set_tun_tos_masked(match, value->u8, mask->u8);
1475 match_set_metadata_masked(match, value->be64, mask->be64);
1479 match_set_reg_masked(match, mf->id - MFF_REG0,
1480 ntohl(value->be32), ntohl(mask->be32));
1484 match_set_xreg_masked(match, mf->id - MFF_XREG0,
1485 ntohll(value->be64), ntohll(mask->be64));
1489 match_set_pkt_mark_masked(match, ntohl(value->be32),
1494 match_set_dl_dst_masked(match, value->mac, mask->mac);
1498 match_set_dl_src_masked(match, value->mac, mask->mac);
1503 match_set_arp_sha_masked(match, value->mac, mask->mac);
1508 match_set_arp_tha_masked(match, value->mac, mask->mac);
1512 match_set_dl_tci_masked(match, value->be16, mask->be16);
1516 match_set_vlan_vid_masked(match, value->be16, mask->be16);
1520 match_set_nw_src_masked(match, value->be32, mask->be32);
1524 match_set_nw_dst_masked(match, value->be32, mask->be32);
1528 match_set_ipv6_src_masked(match, &value->ipv6, &mask->ipv6);
1532 match_set_ipv6_dst_masked(match, &value->ipv6, &mask->ipv6);
1535 case MFF_IPV6_LABEL:
1536 if ((mask->be32 & htonl(IPV6_LABEL_MASK)) == htonl(IPV6_LABEL_MASK)) {
1537 mf_set_value(mf, value, match);
1539 match_set_ipv6_label_masked(match, value->be32, mask->be32);
1544 match_set_nd_target_masked(match, &value->ipv6, &mask->ipv6);
1548 match_set_nw_frag_masked(match, value->u8, mask->u8);
1552 match_set_nw_src_masked(match, value->be32, mask->be32);
1556 match_set_nw_dst_masked(match, value->be32, mask->be32);
1562 match_set_tp_src_masked(match, value->be16, mask->be16);
1568 match_set_tp_dst_masked(match, value->be16, mask->be16);
1572 match_set_tcp_flags_masked(match, value->be16, mask->be16);
1580 return ((mf->usable_protocols_bitwise == mf->usable_protocols_cidr
1581 || ip_is_cidr(mask->be32))
1582 ? mf->usable_protocols_cidr
1583 : mf->usable_protocols_bitwise);
1587 mf_check__(const struct mf_subfield *sf, const struct flow *flow,
1591 VLOG_WARN_RL(&rl, "unknown %s field", type);
1592 return OFPERR_OFPBAC_BAD_SET_TYPE;
1593 } else if (!sf->n_bits) {
1594 VLOG_WARN_RL(&rl, "zero bit %s field %s", type, sf->field->name);
1595 return OFPERR_OFPBAC_BAD_SET_LEN;
1596 } else if (sf->ofs >= sf->field->n_bits) {
1597 VLOG_WARN_RL(&rl, "bit offset %d exceeds %d-bit width of %s field %s",
1598 sf->ofs, sf->field->n_bits, type, sf->field->name);
1599 return OFPERR_OFPBAC_BAD_SET_LEN;
1600 } else if (sf->ofs + sf->n_bits > sf->field->n_bits) {
1601 VLOG_WARN_RL(&rl, "bit offset %d and width %d exceeds %d-bit width "
1602 "of %s field %s", sf->ofs, sf->n_bits,
1603 sf->field->n_bits, type, sf->field->name);
1604 return OFPERR_OFPBAC_BAD_SET_LEN;
1605 } else if (flow && !mf_are_prereqs_ok(sf->field, flow)) {
1606 VLOG_WARN_RL(&rl, "%s field %s lacks correct prerequisites",
1607 type, sf->field->name);
1608 return OFPERR_OFPBAC_MATCH_INCONSISTENT;
1614 /* Checks whether 'sf' is valid for reading a subfield out of 'flow'. Returns
1615 * 0 if so, otherwise an OpenFlow error code (e.g. as returned by
1618 mf_check_src(const struct mf_subfield *sf, const struct flow *flow)
1620 return mf_check__(sf, flow, "source");
1623 /* Checks whether 'sf' is valid for writing a subfield into 'flow'. Returns 0
1624 * if so, otherwise an OpenFlow error code (e.g. as returned by
1627 mf_check_dst(const struct mf_subfield *sf, const struct flow *flow)
1629 int error = mf_check__(sf, flow, "destination");
1630 if (!error && !sf->field->writable) {
1631 VLOG_WARN_RL(&rl, "destination field %s is not writable",
1633 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
1638 /* Copies the value and wildcard bit pattern for 'mf' from 'match' into the
1639 * 'value' and 'mask', respectively. */
1641 mf_get(const struct mf_field *mf, const struct match *match,
1642 union mf_value *value, union mf_value *mask)
1644 mf_get_value(mf, &match->flow, value);
1645 mf_get_mask(mf, &match->wc, mask);
1649 mf_from_integer_string(const struct mf_field *mf, const char *s,
1650 uint8_t *valuep, uint8_t *maskp)
1652 unsigned long long int integer, mask;
1657 integer = strtoull(s, &tail, 0);
1658 if (errno || (*tail != '\0' && *tail != '/')) {
1663 mask = strtoull(tail + 1, &tail, 0);
1664 if (errno || *tail != '\0') {
1671 for (i = mf->n_bytes - 1; i >= 0; i--) {
1672 valuep[i] = integer;
1678 return xasprintf("%s: value too large for %u-byte field %s",
1679 s, mf->n_bytes, mf->name);
1684 return xasprintf("%s: bad syntax for %s", s, mf->name);
1688 mf_from_ethernet_string(const struct mf_field *mf, const char *s,
1689 uint8_t mac[ETH_ADDR_LEN],
1690 uint8_t mask[ETH_ADDR_LEN])
1694 ovs_assert(mf->n_bytes == ETH_ADDR_LEN);
1697 if (ovs_scan(s, ETH_ADDR_SCAN_FMT"%n", ETH_ADDR_SCAN_ARGS(mac), &n)
1698 && n == strlen(s)) {
1699 memset(mask, 0xff, ETH_ADDR_LEN);
1704 if (ovs_scan(s, ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT"%n",
1705 ETH_ADDR_SCAN_ARGS(mac), ETH_ADDR_SCAN_ARGS(mask), &n)
1706 && n == strlen(s)) {
1710 return xasprintf("%s: invalid Ethernet address", s);
1714 mf_from_ipv4_string(const struct mf_field *mf, const char *s,
1715 ovs_be32 *ip, ovs_be32 *mask)
1719 ovs_assert(mf->n_bytes == sizeof *ip);
1721 if (ovs_scan(s, IP_SCAN_FMT"/"IP_SCAN_FMT,
1722 IP_SCAN_ARGS(ip), IP_SCAN_ARGS(mask))) {
1724 } else if (ovs_scan(s, IP_SCAN_FMT"/%d", IP_SCAN_ARGS(ip), &prefix)) {
1725 if (prefix <= 0 || prefix > 32) {
1726 return xasprintf("%s: network prefix bits not between 0 and "
1729 *mask = be32_prefix_mask(prefix);
1730 } else if (ovs_scan(s, IP_SCAN_FMT, IP_SCAN_ARGS(ip))) {
1731 *mask = OVS_BE32_MAX;
1733 return xasprintf("%s: invalid IP address", s);
1739 mf_from_ipv6_string(const struct mf_field *mf, const char *s,
1740 struct in6_addr *value, struct in6_addr *mask)
1742 char *str = xstrdup(s);
1743 char *save_ptr = NULL;
1744 const char *name, *netmask;
1747 ovs_assert(mf->n_bytes == sizeof *value);
1749 name = strtok_r(str, "/", &save_ptr);
1750 retval = name ? lookup_ipv6(name, value) : EINVAL;
1754 err = xasprintf("%s: could not convert to IPv6 address", str);
1760 netmask = strtok_r(NULL, "/", &save_ptr);
1762 if (inet_pton(AF_INET6, netmask, mask) != 1) {
1763 int prefix = atoi(netmask);
1764 if (prefix <= 0 || prefix > 128) {
1766 return xasprintf("%s: prefix bits not between 1 and 128", s);
1768 *mask = ipv6_create_mask(prefix);
1772 *mask = in6addr_exact;
1780 mf_from_ofp_port_string(const struct mf_field *mf, const char *s,
1781 ovs_be16 *valuep, ovs_be16 *maskp)
1785 ovs_assert(mf->n_bytes == sizeof(ovs_be16));
1787 if (ofputil_port_from_string(s, &port)) {
1788 *valuep = htons(ofp_to_u16(port));
1789 *maskp = OVS_BE16_MAX;
1792 return xasprintf("%s: port value out of range for %s", s, mf->name);
1796 mf_from_ofp_port_string32(const struct mf_field *mf, const char *s,
1797 ovs_be32 *valuep, ovs_be32 *maskp)
1801 ovs_assert(mf->n_bytes == sizeof(ovs_be32));
1802 if (ofputil_port_from_string(s, &port)) {
1803 *valuep = ofputil_port_to_ofp11(port);
1804 *maskp = OVS_BE32_MAX;
1807 return xasprintf("%s: port value out of range for %s", s, mf->name);
1810 struct frag_handling {
1816 static const struct frag_handling all_frags[] = {
1817 #define A FLOW_NW_FRAG_ANY
1818 #define L FLOW_NW_FRAG_LATER
1819 /* name mask value */
1822 { "first", A|L, A },
1823 { "later", A|L, A|L },
1828 { "not_later", L, 0 },
1835 mf_from_frag_string(const char *s, uint8_t *valuep, uint8_t *maskp)
1837 const struct frag_handling *h;
1839 for (h = all_frags; h < &all_frags[ARRAY_SIZE(all_frags)]; h++) {
1840 if (!strcasecmp(s, h->name)) {
1841 /* We force the upper bits of the mask on to make mf_parse_value()
1842 * happy (otherwise it will never think it's an exact match.) */
1843 *maskp = h->mask | ~FLOW_NW_FRAG_MASK;
1849 return xasprintf("%s: unknown fragment type (valid types are \"no\", "
1850 "\"yes\", \"first\", \"later\", \"not_first\"", s);
1854 parse_flow_tun_flags(const char *s_, const char *(*bit_to_string)(uint32_t),
1857 uint32_t result = 0;
1858 char *save_ptr = NULL;
1861 char *s = xstrdup(s_);
1863 for (name = strtok_r((char *)s, " |", &save_ptr); name;
1864 name = strtok_r(NULL, " |", &save_ptr)) {
1866 unsigned long long int flags;
1869 if (ovs_scan(name, "%lli", &flags)) {
1873 name_len = strlen(name);
1874 for (bit = 1; bit; bit <<= 1) {
1875 const char *fname = bit_to_string(bit);
1882 len = strlen(fname);
1883 if (len != name_len) {
1886 if (!strncmp(name, fname, len)) {
1898 *res = htons(result);
1905 mf_from_tun_flags_string(const char *s, ovs_be16 *valuep, ovs_be16 *maskp)
1907 if (!parse_flow_tun_flags(s, flow_tun_flag_to_string, valuep)) {
1908 *maskp = OVS_BE16_MAX;
1912 return xasprintf("%s: unknown tunnel flags (valid flags are \"df\", "
1913 "\"csum\", \"key\")", s);
1917 mf_from_tcp_flags_string(const char *s, ovs_be16 *flagsp, ovs_be16 *maskp)
1924 if (ovs_scan(s, "%"SCNi16"/%"SCNi16"%n", &flags, &mask, &n) && !s[n]) {
1925 *flagsp = htons(flags);
1926 *maskp = htons(mask);
1929 if (ovs_scan(s, "%"SCNi16"%n", &flags, &n) && !s[n]) {
1930 *flagsp = htons(flags);
1931 *maskp = OVS_BE16_MAX;
1935 while (*s != '\0') {
1947 return xasprintf("%s: TCP flag must be preceded by '+' (for SET) "
1948 "or '-' (NOT SET)", s);
1952 name_len = strcspn(s,"+-");
1954 for (bit = 1; bit; bit <<= 1) {
1955 const char *fname = packet_tcp_flag_to_string(bit);
1962 len = strlen(fname);
1963 if (len != name_len) {
1966 if (!strncmp(s, fname, len)) {
1968 return xasprintf("%s: Each TCP flag can be specified only "
1980 return xasprintf("%s: unknown TCP flag(s)", s);
1985 *flagsp = htons(flags);
1986 *maskp = htons(mask);
1991 /* Parses 's', a string value for field 'mf', into 'value' and 'mask'. Returns
1992 * NULL if successful, otherwise a malloc()'d string describing the error. */
1994 mf_parse(const struct mf_field *mf, const char *s,
1995 union mf_value *value, union mf_value *mask)
1999 if (!strcmp(s, "*")) {
2000 memset(value, 0, mf->n_bytes);
2001 memset(mask, 0, mf->n_bytes);
2005 switch (mf->string) {
2007 case MFS_HEXADECIMAL:
2008 error = mf_from_integer_string(mf, s,
2009 (uint8_t *) value, (uint8_t *) mask);
2013 error = mf_from_ethernet_string(mf, s, value->mac, mask->mac);
2017 error = mf_from_ipv4_string(mf, s, &value->be32, &mask->be32);
2021 error = mf_from_ipv6_string(mf, s, &value->ipv6, &mask->ipv6);
2025 error = mf_from_ofp_port_string(mf, s, &value->be16, &mask->be16);
2028 case MFS_OFP_PORT_OXM:
2029 error = mf_from_ofp_port_string32(mf, s, &value->be32, &mask->be32);
2033 error = mf_from_frag_string(s, &value->u8, &mask->u8);
2037 ovs_assert(mf->n_bytes == sizeof(ovs_be16));
2038 error = mf_from_tun_flags_string(s, &value->be16, &mask->be16);
2042 ovs_assert(mf->n_bytes == sizeof(ovs_be16));
2043 error = mf_from_tcp_flags_string(s, &value->be16, &mask->be16);
2050 if (!error && !mf_is_mask_valid(mf, mask)) {
2051 error = xasprintf("%s: invalid mask for field %s", s, mf->name);
2056 /* Parses 's', a string value for field 'mf', into 'value'. Returns NULL if
2057 * successful, otherwise a malloc()'d string describing the error. */
2059 mf_parse_value(const struct mf_field *mf, const char *s, union mf_value *value)
2061 union mf_value mask;
2064 error = mf_parse(mf, s, value, &mask);
2069 if (!is_all_ones((const uint8_t *) &mask, mf->n_bytes)) {
2070 return xasprintf("%s: wildcards not allowed here", s);
2076 mf_format_integer_string(const struct mf_field *mf, const uint8_t *valuep,
2077 const uint8_t *maskp, struct ds *s)
2079 unsigned long long int integer;
2082 ovs_assert(mf->n_bytes <= 8);
2085 for (i = 0; i < mf->n_bytes; i++) {
2086 integer = (integer << 8) | valuep[i];
2088 if (mf->string == MFS_HEXADECIMAL) {
2089 ds_put_format(s, "%#llx", integer);
2091 ds_put_format(s, "%lld", integer);
2095 unsigned long long int mask;
2098 for (i = 0; i < mf->n_bytes; i++) {
2099 mask = (mask << 8) | maskp[i];
2102 /* I guess we could write the mask in decimal for MFS_DECIMAL but I'm
2103 * not sure that that a bit-mask written in decimal is ever easier to
2104 * understand than the same bit-mask written in hexadecimal. */
2105 ds_put_format(s, "/%#llx", mask);
2110 mf_format_frag_string(uint8_t value, uint8_t mask, struct ds *s)
2112 const struct frag_handling *h;
2114 mask &= FLOW_NW_FRAG_MASK;
2117 for (h = all_frags; h < &all_frags[ARRAY_SIZE(all_frags)]; h++) {
2118 if (value == h->value && mask == h->mask) {
2119 ds_put_cstr(s, h->name);
2123 ds_put_cstr(s, "<error>");
2127 mf_format_tnl_flags_string(const ovs_be16 *valuep, struct ds *s)
2129 format_flags(s, flow_tun_flag_to_string, ntohs(*valuep), '|');
2133 mf_format_tcp_flags_string(ovs_be16 value, ovs_be16 mask, struct ds *s)
2135 format_flags_masked(s, NULL, packet_tcp_flag_to_string, ntohs(value),
2139 /* Appends to 's' a string representation of field 'mf' whose value is in
2140 * 'value' and 'mask'. 'mask' may be NULL to indicate an exact match. */
2142 mf_format(const struct mf_field *mf,
2143 const union mf_value *value, const union mf_value *mask,
2147 if (is_all_zeros(mask, mf->n_bytes)) {
2148 ds_put_cstr(s, "ANY");
2150 } else if (is_all_ones(mask, mf->n_bytes)) {
2155 switch (mf->string) {
2156 case MFS_OFP_PORT_OXM:
2159 ofputil_port_from_ofp11(value->be32, &port);
2160 ofputil_format_port(port, s);
2166 ofputil_format_port(u16_to_ofp(ntohs(value->be16)), s);
2171 case MFS_HEXADECIMAL:
2172 mf_format_integer_string(mf, (uint8_t *) value, (uint8_t *) mask, s);
2176 eth_format_masked(value->mac, mask->mac, s);
2180 ip_format_masked(value->be32, mask ? mask->be32 : OVS_BE32_MAX, s);
2184 print_ipv6_masked(s, &value->ipv6, mask ? &mask->ipv6 : NULL);
2188 mf_format_frag_string(value->u8, mask ? mask->u8 : UINT8_MAX, s);
2192 mf_format_tnl_flags_string(&value->be16, s);
2196 mf_format_tcp_flags_string(value->be16,
2197 mask ? mask->be16 : OVS_BE16_MAX, s);
2205 /* Makes subfield 'sf' within 'flow' exactly match the 'sf->n_bits'
2206 * least-significant bits in 'x'.
2209 mf_write_subfield_flow(const struct mf_subfield *sf,
2210 const union mf_subvalue *x, struct flow *flow)
2212 const struct mf_field *field = sf->field;
2213 union mf_value value;
2215 mf_get_value(field, flow, &value);
2216 bitwise_copy(x, sizeof *x, 0, &value, field->n_bytes,
2217 sf->ofs, sf->n_bits);
2218 mf_set_flow_value(field, &value, flow);
2221 /* Makes subfield 'sf' within 'match' exactly match the 'sf->n_bits'
2222 * least-significant bits in 'x'.
2225 mf_write_subfield(const struct mf_subfield *sf, const union mf_subvalue *x,
2226 struct match *match)
2228 const struct mf_field *field = sf->field;
2229 union mf_value value, mask;
2231 mf_get(field, match, &value, &mask);
2232 bitwise_copy(x, sizeof *x, 0, &value, field->n_bytes, sf->ofs, sf->n_bits);
2233 bitwise_one ( &mask, field->n_bytes, sf->ofs, sf->n_bits);
2234 mf_set(field, &value, &mask, match);
2237 /* Initializes 'x' to the value of 'sf' within 'flow'. 'sf' must be valid for
2238 * reading 'flow', e.g. as checked by mf_check_src(). */
2240 mf_read_subfield(const struct mf_subfield *sf, const struct flow *flow,
2241 union mf_subvalue *x)
2243 union mf_value value;
2245 mf_get_value(sf->field, flow, &value);
2247 memset(x, 0, sizeof *x);
2248 bitwise_copy(&value, sf->field->n_bytes, sf->ofs,
2253 /* Returns the value of 'sf' within 'flow'. 'sf' must be valid for reading
2254 * 'flow', e.g. as checked by mf_check_src() and sf->n_bits must be 64 or
2257 mf_get_subfield(const struct mf_subfield *sf, const struct flow *flow)
2259 union mf_value value;
2261 mf_get_value(sf->field, flow, &value);
2262 return bitwise_get(&value, sf->field->n_bytes, sf->ofs, sf->n_bits);
2266 mf_format_subvalue(const union mf_subvalue *subvalue, struct ds *s)
2270 for (i = 0; i < ARRAY_SIZE(subvalue->u8); i++) {
2271 if (subvalue->u8[i]) {
2272 ds_put_format(s, "0x%"PRIx8, subvalue->u8[i]);
2273 for (i++; i < ARRAY_SIZE(subvalue->u8); i++) {
2274 ds_put_format(s, "%02"PRIx8, subvalue->u8[i]);
2279 ds_put_char(s, '0');