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);
355 /* Set bits of 'bm' corresponding to the field 'mf' and it's prerequisities. */
357 mf_bitmap_set_field_and_prereqs(const struct mf_field *mf, struct mf_bitmap *bm)
359 bitmap_set1(bm->bm, mf->id);
361 switch (mf->prereqs) {
365 bitmap_set1(bm->bm, MFF_TCP_SRC);
366 bitmap_set1(bm->bm, MFF_TCP_DST);
373 /* nw_frag always unwildcarded. */
374 bitmap_set1(bm->bm, MFF_IP_PROTO);
381 bitmap_set1(bm->bm, MFF_ETH_TYPE);
384 bitmap_set1(bm->bm, MFF_VLAN_TCI);
391 /* Returns true if 'value' may be a valid value *as part of a masked match*,
394 * A value is not rejected just because it is not valid for the field in
395 * question, but only if it doesn't make sense to test the bits in question at
396 * all. For example, the MFF_VLAN_TCI field will never have a nonzero value
397 * without the VLAN_CFI bit being set, but we can't reject those values because
398 * it is still legitimate to test just for those bits (see the documentation
399 * for NXM_OF_VLAN_TCI in nicira-ext.h). On the other hand, there is never a
400 * reason to set the low bit of MFF_IP_DSCP to 1, so we reject that. */
402 mf_is_value_valid(const struct mf_field *mf, const union mf_value *value)
415 case MFF_TUN_GBP_FLAGS:
418 case MFF_SKB_PRIORITY:
442 case MFF_ICMPV4_TYPE:
443 case MFF_ICMPV4_CODE:
444 case MFF_ICMPV6_TYPE:
445 case MFF_ICMPV6_CODE:
451 case MFF_IN_PORT_OXM:
452 case MFF_ACTSET_OUTPUT: {
454 return !ofputil_port_from_ofp11(value->be32, &port);
458 return !(value->u8 & ~IP_DSCP_MASK);
459 case MFF_IP_DSCP_SHIFTED:
460 return !(value->u8 & (~IP_DSCP_MASK >> 2));
462 return !(value->u8 & ~IP_ECN_MASK);
464 return !(value->u8 & ~FLOW_NW_FRAG_MASK);
466 return !(value->be16 & ~htons(0x0fff));
469 return !(value->be16 & htons(0xff00));
472 return !(value->be16 & htons(VLAN_CFI | VLAN_PCP_MASK));
474 return !(value->be16 & htons(VLAN_PCP_MASK));
476 case MFF_DL_VLAN_PCP:
478 return !(value->u8 & ~(VLAN_PCP_MASK >> VLAN_PCP_SHIFT));
481 return !(value->be32 & ~htonl(IPV6_LABEL_MASK));
484 return !(value->be32 & ~htonl(MPLS_LABEL_MASK >> MPLS_LABEL_SHIFT));
487 return !(value->u8 & ~(MPLS_TC_MASK >> MPLS_TC_SHIFT));
490 return !(value->u8 & ~(MPLS_BOS_MASK >> MPLS_BOS_SHIFT));
498 /* Copies the value of field 'mf' from 'flow' into 'value'. The caller is
499 * responsible for ensuring that 'flow' meets 'mf''s prerequisites. */
501 mf_get_value(const struct mf_field *mf, const struct flow *flow,
502 union mf_value *value)
506 value->be32 = htonl(flow->dp_hash);
509 value->be32 = htonl(flow->recirc_id);
512 value->be32 = htonl(flow->conj_id);
515 value->be64 = flow->tunnel.tun_id;
518 value->be32 = flow->tunnel.ip_src;
521 value->be32 = flow->tunnel.ip_dst;
524 value->be16 = htons(flow->tunnel.flags);
527 value->be16 = flow->tunnel.gbp_id;
529 case MFF_TUN_GBP_FLAGS:
530 value->u8 = flow->tunnel.gbp_flags;
533 value->u8 = flow->tunnel.ip_ttl;
536 value->u8 = flow->tunnel.ip_tos;
540 value->be64 = flow->metadata;
544 value->be16 = htons(ofp_to_u16(flow->in_port.ofp_port));
546 case MFF_IN_PORT_OXM:
547 value->be32 = ofputil_port_to_ofp11(flow->in_port.ofp_port);
549 case MFF_ACTSET_OUTPUT:
550 value->be32 = ofputil_port_to_ofp11(flow->actset_output);
553 case MFF_SKB_PRIORITY:
554 value->be32 = htonl(flow->skb_priority);
558 value->be32 = htonl(flow->pkt_mark);
562 value->be32 = htonl(flow->regs[mf->id - MFF_REG0]);
566 value->be64 = htonll(flow_get_xreg(flow, mf->id - MFF_XREG0));
570 memcpy(value->mac, flow->dl_src, ETH_ADDR_LEN);
574 memcpy(value->mac, flow->dl_dst, ETH_ADDR_LEN);
578 value->be16 = flow->dl_type;
582 value->be16 = flow->vlan_tci;
586 value->be16 = flow->vlan_tci & htons(VLAN_VID_MASK);
589 value->be16 = flow->vlan_tci & htons(VLAN_VID_MASK | VLAN_CFI);
592 case MFF_DL_VLAN_PCP:
594 value->u8 = vlan_tci_to_pcp(flow->vlan_tci);
598 value->be32 = htonl(mpls_lse_to_label(flow->mpls_lse[0]));
602 value->u8 = mpls_lse_to_tc(flow->mpls_lse[0]);
606 value->u8 = mpls_lse_to_bos(flow->mpls_lse[0]);
610 value->be32 = flow->nw_src;
614 value->be32 = flow->nw_dst;
618 value->ipv6 = flow->ipv6_src;
622 value->ipv6 = flow->ipv6_dst;
626 value->be32 = flow->ipv6_label;
630 value->u8 = flow->nw_proto;
634 value->u8 = flow->nw_tos & IP_DSCP_MASK;
637 case MFF_IP_DSCP_SHIFTED:
638 value->u8 = flow->nw_tos >> 2;
642 value->u8 = flow->nw_tos & IP_ECN_MASK;
646 value->u8 = flow->nw_ttl;
650 value->u8 = flow->nw_frag;
654 value->be16 = htons(flow->nw_proto);
658 value->be32 = flow->nw_src;
662 value->be32 = flow->nw_dst;
667 memcpy(value->mac, flow->arp_sha, ETH_ADDR_LEN);
672 memcpy(value->mac, flow->arp_tha, ETH_ADDR_LEN);
678 value->be16 = flow->tp_src;
684 value->be16 = flow->tp_dst;
688 value->be16 = flow->tcp_flags;
691 case MFF_ICMPV4_TYPE:
692 case MFF_ICMPV6_TYPE:
693 value->u8 = ntohs(flow->tp_src);
696 case MFF_ICMPV4_CODE:
697 case MFF_ICMPV6_CODE:
698 value->u8 = ntohs(flow->tp_dst);
702 value->ipv6 = flow->nd_target;
711 /* Makes 'match' match field 'mf' exactly, with the value matched taken from
712 * 'value'. The caller is responsible for ensuring that 'match' meets 'mf''s
715 mf_set_value(const struct mf_field *mf,
716 const union mf_value *value, struct match *match)
720 match_set_dp_hash(match, ntohl(value->be32));
723 match_set_recirc_id(match, ntohl(value->be32));
726 match_set_conj_id(match, ntohl(value->be32));
729 match_set_tun_id(match, value->be64);
732 match_set_tun_src(match, value->be32);
735 match_set_tun_dst(match, value->be32);
738 match_set_tun_flags(match, ntohs(value->be16));
741 match_set_tun_gbp_id(match, value->be16);
743 case MFF_TUN_GBP_FLAGS:
744 match_set_tun_gbp_flags(match, value->u8);
747 match_set_tun_tos(match, value->u8);
750 match_set_tun_ttl(match, value->u8);
754 match_set_metadata(match, value->be64);
758 match_set_in_port(match, u16_to_ofp(ntohs(value->be16)));
761 case MFF_IN_PORT_OXM: {
763 ofputil_port_from_ofp11(value->be32, &port);
764 match_set_in_port(match, port);
767 case MFF_ACTSET_OUTPUT: {
769 ofputil_port_from_ofp11(value->be32, &port);
770 match_set_actset_output(match, port);
774 case MFF_SKB_PRIORITY:
775 match_set_skb_priority(match, ntohl(value->be32));
779 match_set_pkt_mark(match, ntohl(value->be32));
783 match_set_reg(match, mf->id - MFF_REG0, ntohl(value->be32));
787 match_set_xreg(match, mf->id - MFF_XREG0, ntohll(value->be64));
791 match_set_dl_src(match, value->mac);
795 match_set_dl_dst(match, value->mac);
799 match_set_dl_type(match, value->be16);
803 match_set_dl_tci(match, value->be16);
807 match_set_dl_vlan(match, value->be16);
810 match_set_vlan_vid(match, value->be16);
813 case MFF_DL_VLAN_PCP:
815 match_set_dl_vlan_pcp(match, value->u8);
819 match_set_mpls_label(match, 0, value->be32);
823 match_set_mpls_tc(match, 0, value->u8);
827 match_set_mpls_bos(match, 0, value->u8);
831 match_set_nw_src(match, value->be32);
835 match_set_nw_dst(match, value->be32);
839 match_set_ipv6_src(match, &value->ipv6);
843 match_set_ipv6_dst(match, &value->ipv6);
847 match_set_ipv6_label(match, value->be32);
851 match_set_nw_proto(match, value->u8);
855 match_set_nw_dscp(match, value->u8);
858 case MFF_IP_DSCP_SHIFTED:
859 match_set_nw_dscp(match, value->u8 << 2);
863 match_set_nw_ecn(match, value->u8);
867 match_set_nw_ttl(match, value->u8);
871 match_set_nw_frag(match, value->u8);
875 match_set_nw_proto(match, ntohs(value->be16));
879 match_set_nw_src(match, value->be32);
883 match_set_nw_dst(match, value->be32);
888 match_set_arp_sha(match, value->mac);
893 match_set_arp_tha(match, value->mac);
899 match_set_tp_src(match, value->be16);
905 match_set_tp_dst(match, value->be16);
909 match_set_tcp_flags(match, value->be16);
912 case MFF_ICMPV4_TYPE:
913 case MFF_ICMPV6_TYPE:
914 match_set_icmp_type(match, value->u8);
917 case MFF_ICMPV4_CODE:
918 case MFF_ICMPV6_CODE:
919 match_set_icmp_code(match, value->u8);
923 match_set_nd_target(match, &value->ipv6);
932 /* Unwildcard 'mask' member field described by 'mf'. The caller is
933 * responsible for ensuring that 'mask' meets 'mf''s prerequisites. */
935 mf_mask_field(const struct mf_field *mf, struct flow *mask)
937 static const union mf_value exact_match_mask = MF_EXACT_MASK_INITIALIZER;
939 /* For MFF_DL_VLAN, we cannot send a all 1's to flow_set_dl_vlan()
940 * as that will be considered as OFP10_VLAN_NONE. So consider it as a
941 * special case. For the rest, calling mf_set_flow_value() is good
943 if (mf->id == MFF_DL_VLAN) {
944 flow_set_dl_vlan(mask, htons(VLAN_VID_MASK));
946 mf_set_flow_value(mf, &exact_match_mask, mask);
950 /* Sets 'flow' member field described by 'mf' to 'value'. The caller is
951 * responsible for ensuring that 'flow' meets 'mf''s prerequisites.*/
953 mf_set_flow_value(const struct mf_field *mf,
954 const union mf_value *value, struct flow *flow)
958 flow->dp_hash = ntohl(value->be32);
961 flow->recirc_id = ntohl(value->be32);
964 flow->conj_id = ntohl(value->be32);
967 flow->tunnel.tun_id = value->be64;
970 flow->tunnel.ip_src = value->be32;
973 flow->tunnel.ip_dst = value->be32;
976 flow->tunnel.flags = ntohs(value->be16);
979 flow->tunnel.gbp_id = value->be16;
981 case MFF_TUN_GBP_FLAGS:
982 flow->tunnel.gbp_flags = value->u8;
985 flow->tunnel.ip_tos = value->u8;
988 flow->tunnel.ip_ttl = value->u8;
992 flow->metadata = value->be64;
996 flow->in_port.ofp_port = u16_to_ofp(ntohs(value->be16));
999 case MFF_IN_PORT_OXM:
1000 ofputil_port_from_ofp11(value->be32, &flow->in_port.ofp_port);
1002 case MFF_ACTSET_OUTPUT:
1003 ofputil_port_from_ofp11(value->be32, &flow->actset_output);
1006 case MFF_SKB_PRIORITY:
1007 flow->skb_priority = ntohl(value->be32);
1011 flow->pkt_mark = ntohl(value->be32);
1015 flow->regs[mf->id - MFF_REG0] = ntohl(value->be32);
1019 flow_set_xreg(flow, mf->id - MFF_XREG0, ntohll(value->be64));
1023 memcpy(flow->dl_src, value->mac, ETH_ADDR_LEN);
1027 memcpy(flow->dl_dst, value->mac, ETH_ADDR_LEN);
1031 flow->dl_type = value->be16;
1035 flow->vlan_tci = value->be16;
1039 flow_set_dl_vlan(flow, value->be16);
1042 flow_set_vlan_vid(flow, value->be16);
1045 case MFF_DL_VLAN_PCP:
1047 flow_set_vlan_pcp(flow, value->u8);
1050 case MFF_MPLS_LABEL:
1051 flow_set_mpls_label(flow, 0, value->be32);
1055 flow_set_mpls_tc(flow, 0, value->u8);
1059 flow_set_mpls_bos(flow, 0, value->u8);
1063 flow->nw_src = value->be32;
1067 flow->nw_dst = value->be32;
1071 flow->ipv6_src = value->ipv6;
1075 flow->ipv6_dst = value->ipv6;
1078 case MFF_IPV6_LABEL:
1079 flow->ipv6_label = value->be32 & htonl(IPV6_LABEL_MASK);
1083 flow->nw_proto = value->u8;
1087 flow->nw_tos &= ~IP_DSCP_MASK;
1088 flow->nw_tos |= value->u8 & IP_DSCP_MASK;
1091 case MFF_IP_DSCP_SHIFTED:
1092 flow->nw_tos &= ~IP_DSCP_MASK;
1093 flow->nw_tos |= value->u8 << 2;
1097 flow->nw_tos &= ~IP_ECN_MASK;
1098 flow->nw_tos |= value->u8 & IP_ECN_MASK;
1102 flow->nw_ttl = value->u8;
1106 flow->nw_frag = value->u8 & FLOW_NW_FRAG_MASK;
1110 flow->nw_proto = ntohs(value->be16);
1114 flow->nw_src = value->be32;
1118 flow->nw_dst = value->be32;
1123 memcpy(flow->arp_sha, value->mac, ETH_ADDR_LEN);
1128 memcpy(flow->arp_tha, value->mac, ETH_ADDR_LEN);
1134 flow->tp_src = value->be16;
1140 flow->tp_dst = value->be16;
1144 flow->tcp_flags = value->be16;
1147 case MFF_ICMPV4_TYPE:
1148 case MFF_ICMPV6_TYPE:
1149 flow->tp_src = htons(value->u8);
1152 case MFF_ICMPV4_CODE:
1153 case MFF_ICMPV6_CODE:
1154 flow->tp_dst = htons(value->u8);
1158 flow->nd_target = value->ipv6;
1167 /* Consider each of 'src', 'mask', and 'dst' as if they were arrays of 8*n
1168 * bits. Then, for each 0 <= i < 8 * n such that mask[i] == 1, sets dst[i] =
1171 apply_mask(const uint8_t *src, const uint8_t *mask, uint8_t *dst, size_t n)
1175 for (i = 0; i < n; i++) {
1176 dst[i] = (src[i] & mask[i]) | (dst[i] & ~mask[i]);
1180 /* Sets 'flow' member field described by 'field' to 'value', except that bits
1181 * for which 'mask' has a 0-bit keep their existing values. The caller is
1182 * responsible for ensuring that 'flow' meets 'field''s prerequisites.*/
1184 mf_set_flow_value_masked(const struct mf_field *field,
1185 const union mf_value *value,
1186 const union mf_value *mask,
1191 mf_get_value(field, flow, &tmp);
1192 apply_mask((const uint8_t *) value, (const uint8_t *) mask,
1193 (uint8_t *) &tmp, field->n_bytes);
1194 mf_set_flow_value(field, &tmp, flow);
1197 /* Returns true if 'mf' has a zero value in 'flow', false if it is nonzero.
1199 * The caller is responsible for ensuring that 'flow' meets 'mf''s
1202 mf_is_zero(const struct mf_field *mf, const struct flow *flow)
1204 union mf_value value;
1206 mf_get_value(mf, flow, &value);
1207 return is_all_zeros(&value, mf->n_bytes);
1210 /* Makes 'match' wildcard field 'mf'.
1212 * The caller is responsible for ensuring that 'match' meets 'mf''s
1215 mf_set_wild(const struct mf_field *mf, struct match *match)
1219 match->flow.dp_hash = 0;
1220 match->wc.masks.dp_hash = 0;
1223 match->flow.recirc_id = 0;
1224 match->wc.masks.recirc_id = 0;
1227 match->flow.conj_id = 0;
1228 match->wc.masks.conj_id = 0;
1231 match_set_tun_id_masked(match, htonll(0), htonll(0));
1234 match_set_tun_src_masked(match, htonl(0), htonl(0));
1237 match_set_tun_dst_masked(match, htonl(0), htonl(0));
1240 match_set_tun_flags_masked(match, 0, 0);
1242 case MFF_TUN_GBP_ID:
1243 match_set_tun_gbp_id_masked(match, 0, 0);
1245 case MFF_TUN_GBP_FLAGS:
1246 match_set_tun_gbp_flags_masked(match, 0, 0);
1249 match_set_tun_tos_masked(match, 0, 0);
1252 match_set_tun_ttl_masked(match, 0, 0);
1256 match_set_metadata_masked(match, htonll(0), htonll(0));
1260 case MFF_IN_PORT_OXM:
1261 match->flow.in_port.ofp_port = 0;
1262 match->wc.masks.in_port.ofp_port = 0;
1264 case MFF_ACTSET_OUTPUT:
1265 match->flow.actset_output = 0;
1266 match->wc.masks.actset_output = 0;
1269 case MFF_SKB_PRIORITY:
1270 match->flow.skb_priority = 0;
1271 match->wc.masks.skb_priority = 0;
1275 match->flow.pkt_mark = 0;
1276 match->wc.masks.pkt_mark = 0;
1280 match_set_reg_masked(match, mf->id - MFF_REG0, 0, 0);
1284 match_set_xreg_masked(match, mf->id - MFF_XREG0, 0, 0);
1288 memset(match->flow.dl_src, 0, ETH_ADDR_LEN);
1289 memset(match->wc.masks.dl_src, 0, ETH_ADDR_LEN);
1293 memset(match->flow.dl_dst, 0, ETH_ADDR_LEN);
1294 memset(match->wc.masks.dl_dst, 0, ETH_ADDR_LEN);
1298 match->flow.dl_type = htons(0);
1299 match->wc.masks.dl_type = htons(0);
1303 match_set_dl_tci_masked(match, htons(0), htons(0));
1308 match_set_any_vid(match);
1311 case MFF_DL_VLAN_PCP:
1313 match_set_any_pcp(match);
1316 case MFF_MPLS_LABEL:
1317 match_set_any_mpls_label(match, 0);
1321 match_set_any_mpls_tc(match, 0);
1325 match_set_any_mpls_bos(match, 0);
1330 match_set_nw_src_masked(match, htonl(0), htonl(0));
1335 match_set_nw_dst_masked(match, htonl(0), htonl(0));
1339 memset(&match->wc.masks.ipv6_src, 0, sizeof match->wc.masks.ipv6_src);
1340 memset(&match->flow.ipv6_src, 0, sizeof match->flow.ipv6_src);
1344 memset(&match->wc.masks.ipv6_dst, 0, sizeof match->wc.masks.ipv6_dst);
1345 memset(&match->flow.ipv6_dst, 0, sizeof match->flow.ipv6_dst);
1348 case MFF_IPV6_LABEL:
1349 match->wc.masks.ipv6_label = htonl(0);
1350 match->flow.ipv6_label = htonl(0);
1354 match->wc.masks.nw_proto = 0;
1355 match->flow.nw_proto = 0;
1359 case MFF_IP_DSCP_SHIFTED:
1360 match->wc.masks.nw_tos &= ~IP_DSCP_MASK;
1361 match->flow.nw_tos &= ~IP_DSCP_MASK;
1365 match->wc.masks.nw_tos &= ~IP_ECN_MASK;
1366 match->flow.nw_tos &= ~IP_ECN_MASK;
1370 match->wc.masks.nw_ttl = 0;
1371 match->flow.nw_ttl = 0;
1375 match->wc.masks.nw_frag |= FLOW_NW_FRAG_MASK;
1376 match->flow.nw_frag &= ~FLOW_NW_FRAG_MASK;
1380 match->wc.masks.nw_proto = 0;
1381 match->flow.nw_proto = 0;
1386 memset(match->flow.arp_sha, 0, ETH_ADDR_LEN);
1387 memset(match->wc.masks.arp_sha, 0, ETH_ADDR_LEN);
1392 memset(match->flow.arp_tha, 0, ETH_ADDR_LEN);
1393 memset(match->wc.masks.arp_tha, 0, ETH_ADDR_LEN);
1399 case MFF_ICMPV4_TYPE:
1400 case MFF_ICMPV6_TYPE:
1401 match->wc.masks.tp_src = htons(0);
1402 match->flow.tp_src = htons(0);
1408 case MFF_ICMPV4_CODE:
1409 case MFF_ICMPV6_CODE:
1410 match->wc.masks.tp_dst = htons(0);
1411 match->flow.tp_dst = htons(0);
1415 match->wc.masks.tcp_flags = htons(0);
1416 match->flow.tcp_flags = htons(0);
1420 memset(&match->wc.masks.nd_target, 0,
1421 sizeof match->wc.masks.nd_target);
1422 memset(&match->flow.nd_target, 0, sizeof match->flow.nd_target);
1431 /* Makes 'match' match field 'mf' with the specified 'value' and 'mask'.
1432 * 'value' specifies a value to match and 'mask' specifies a wildcard pattern,
1433 * with a 1-bit indicating that the corresponding value bit must match and a
1434 * 0-bit indicating a don't-care.
1436 * If 'mask' is NULL or points to all-1-bits, then this call is equivalent to
1437 * mf_set_value(mf, value, match). If 'mask' points to all-0-bits, then this
1438 * call is equivalent to mf_set_wild(mf, match).
1440 * 'mask' must be a valid mask for 'mf' (see mf_is_mask_valid()). The caller
1441 * is responsible for ensuring that 'match' meets 'mf''s prerequisites. */
1442 enum ofputil_protocol
1443 mf_set(const struct mf_field *mf,
1444 const union mf_value *value, const union mf_value *mask,
1445 struct match *match)
1447 if (!mask || is_all_ones(mask, mf->n_bytes)) {
1448 mf_set_value(mf, value, match);
1449 return mf->usable_protocols_exact;
1450 } else if (is_all_zeros(mask, mf->n_bytes)) {
1451 mf_set_wild(mf, match);
1452 return OFPUTIL_P_ANY;
1459 case MFF_IN_PORT_OXM:
1460 case MFF_ACTSET_OUTPUT:
1461 case MFF_SKB_PRIORITY:
1464 case MFF_DL_VLAN_PCP:
1466 case MFF_MPLS_LABEL:
1472 case MFF_IP_DSCP_SHIFTED:
1475 case MFF_ICMPV4_TYPE:
1476 case MFF_ICMPV4_CODE:
1477 case MFF_ICMPV6_TYPE:
1478 case MFF_ICMPV6_CODE:
1479 return OFPUTIL_P_NONE;
1482 match_set_dp_hash_masked(match, ntohl(value->be32), ntohl(mask->be32));
1485 match_set_tun_id_masked(match, value->be64, mask->be64);
1488 match_set_tun_src_masked(match, value->be32, mask->be32);
1491 match_set_tun_dst_masked(match, value->be32, mask->be32);
1494 match_set_tun_flags_masked(match, ntohs(value->be16), ntohs(mask->be16));
1496 case MFF_TUN_GBP_ID:
1497 match_set_tun_gbp_id_masked(match, value->be16, mask->be16);
1499 case MFF_TUN_GBP_FLAGS:
1500 match_set_tun_gbp_flags_masked(match, value->u8, mask->u8);
1503 match_set_tun_ttl_masked(match, value->u8, mask->u8);
1506 match_set_tun_tos_masked(match, value->u8, mask->u8);
1510 match_set_metadata_masked(match, value->be64, mask->be64);
1514 match_set_reg_masked(match, mf->id - MFF_REG0,
1515 ntohl(value->be32), ntohl(mask->be32));
1519 match_set_xreg_masked(match, mf->id - MFF_XREG0,
1520 ntohll(value->be64), ntohll(mask->be64));
1524 match_set_pkt_mark_masked(match, ntohl(value->be32),
1529 match_set_dl_dst_masked(match, value->mac, mask->mac);
1533 match_set_dl_src_masked(match, value->mac, mask->mac);
1538 match_set_arp_sha_masked(match, value->mac, mask->mac);
1543 match_set_arp_tha_masked(match, value->mac, mask->mac);
1547 match_set_dl_tci_masked(match, value->be16, mask->be16);
1551 match_set_vlan_vid_masked(match, value->be16, mask->be16);
1555 match_set_nw_src_masked(match, value->be32, mask->be32);
1559 match_set_nw_dst_masked(match, value->be32, mask->be32);
1563 match_set_ipv6_src_masked(match, &value->ipv6, &mask->ipv6);
1567 match_set_ipv6_dst_masked(match, &value->ipv6, &mask->ipv6);
1570 case MFF_IPV6_LABEL:
1571 if ((mask->be32 & htonl(IPV6_LABEL_MASK)) == htonl(IPV6_LABEL_MASK)) {
1572 mf_set_value(mf, value, match);
1574 match_set_ipv6_label_masked(match, value->be32, mask->be32);
1579 match_set_nd_target_masked(match, &value->ipv6, &mask->ipv6);
1583 match_set_nw_frag_masked(match, value->u8, mask->u8);
1587 match_set_nw_src_masked(match, value->be32, mask->be32);
1591 match_set_nw_dst_masked(match, value->be32, mask->be32);
1597 match_set_tp_src_masked(match, value->be16, mask->be16);
1603 match_set_tp_dst_masked(match, value->be16, mask->be16);
1607 match_set_tcp_flags_masked(match, value->be16, mask->be16);
1615 return ((mf->usable_protocols_bitwise == mf->usable_protocols_cidr
1616 || ip_is_cidr(mask->be32))
1617 ? mf->usable_protocols_cidr
1618 : mf->usable_protocols_bitwise);
1622 mf_check__(const struct mf_subfield *sf, const struct flow *flow,
1626 VLOG_WARN_RL(&rl, "unknown %s field", type);
1627 return OFPERR_OFPBAC_BAD_SET_TYPE;
1628 } else if (!sf->n_bits) {
1629 VLOG_WARN_RL(&rl, "zero bit %s field %s", type, sf->field->name);
1630 return OFPERR_OFPBAC_BAD_SET_LEN;
1631 } else if (sf->ofs >= sf->field->n_bits) {
1632 VLOG_WARN_RL(&rl, "bit offset %d exceeds %d-bit width of %s field %s",
1633 sf->ofs, sf->field->n_bits, type, sf->field->name);
1634 return OFPERR_OFPBAC_BAD_SET_LEN;
1635 } else if (sf->ofs + sf->n_bits > sf->field->n_bits) {
1636 VLOG_WARN_RL(&rl, "bit offset %d and width %d exceeds %d-bit width "
1637 "of %s field %s", sf->ofs, sf->n_bits,
1638 sf->field->n_bits, type, sf->field->name);
1639 return OFPERR_OFPBAC_BAD_SET_LEN;
1640 } else if (flow && !mf_are_prereqs_ok(sf->field, flow)) {
1641 VLOG_WARN_RL(&rl, "%s field %s lacks correct prerequisites",
1642 type, sf->field->name);
1643 return OFPERR_OFPBAC_MATCH_INCONSISTENT;
1649 /* Checks whether 'sf' is valid for reading a subfield out of 'flow'. Returns
1650 * 0 if so, otherwise an OpenFlow error code (e.g. as returned by
1653 mf_check_src(const struct mf_subfield *sf, const struct flow *flow)
1655 return mf_check__(sf, flow, "source");
1658 /* Checks whether 'sf' is valid for writing a subfield into 'flow'. Returns 0
1659 * if so, otherwise an OpenFlow error code (e.g. as returned by
1662 mf_check_dst(const struct mf_subfield *sf, const struct flow *flow)
1664 int error = mf_check__(sf, flow, "destination");
1665 if (!error && !sf->field->writable) {
1666 VLOG_WARN_RL(&rl, "destination field %s is not writable",
1668 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
1673 /* Copies the value and wildcard bit pattern for 'mf' from 'match' into the
1674 * 'value' and 'mask', respectively. */
1676 mf_get(const struct mf_field *mf, const struct match *match,
1677 union mf_value *value, union mf_value *mask)
1679 mf_get_value(mf, &match->flow, value);
1680 mf_get_mask(mf, &match->wc, mask);
1684 mf_from_integer_string(const struct mf_field *mf, const char *s,
1685 uint8_t *valuep, uint8_t *maskp)
1687 unsigned long long int integer, mask;
1692 integer = strtoull(s, &tail, 0);
1693 if (errno || (*tail != '\0' && *tail != '/')) {
1698 mask = strtoull(tail + 1, &tail, 0);
1699 if (errno || *tail != '\0') {
1706 for (i = mf->n_bytes - 1; i >= 0; i--) {
1707 valuep[i] = integer;
1713 return xasprintf("%s: value too large for %u-byte field %s",
1714 s, mf->n_bytes, mf->name);
1719 return xasprintf("%s: bad syntax for %s", s, mf->name);
1723 mf_from_ethernet_string(const struct mf_field *mf, const char *s,
1724 uint8_t mac[ETH_ADDR_LEN],
1725 uint8_t mask[ETH_ADDR_LEN])
1729 ovs_assert(mf->n_bytes == ETH_ADDR_LEN);
1732 if (ovs_scan(s, ETH_ADDR_SCAN_FMT"%n", ETH_ADDR_SCAN_ARGS(mac), &n)
1733 && n == strlen(s)) {
1734 memset(mask, 0xff, ETH_ADDR_LEN);
1739 if (ovs_scan(s, ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT"%n",
1740 ETH_ADDR_SCAN_ARGS(mac), ETH_ADDR_SCAN_ARGS(mask), &n)
1741 && n == strlen(s)) {
1745 return xasprintf("%s: invalid Ethernet address", s);
1749 mf_from_ipv4_string(const struct mf_field *mf, const char *s,
1750 ovs_be32 *ip, ovs_be32 *mask)
1754 ovs_assert(mf->n_bytes == sizeof *ip);
1756 if (ovs_scan(s, IP_SCAN_FMT"/"IP_SCAN_FMT,
1757 IP_SCAN_ARGS(ip), IP_SCAN_ARGS(mask))) {
1759 } else if (ovs_scan(s, IP_SCAN_FMT"/%d", IP_SCAN_ARGS(ip), &prefix)) {
1760 if (prefix <= 0 || prefix > 32) {
1761 return xasprintf("%s: network prefix bits not between 0 and "
1764 *mask = be32_prefix_mask(prefix);
1765 } else if (ovs_scan(s, IP_SCAN_FMT, IP_SCAN_ARGS(ip))) {
1766 *mask = OVS_BE32_MAX;
1768 return xasprintf("%s: invalid IP address", s);
1774 mf_from_ipv6_string(const struct mf_field *mf, const char *s,
1775 struct in6_addr *value, struct in6_addr *mask)
1777 char *str = xstrdup(s);
1778 char *save_ptr = NULL;
1779 const char *name, *netmask;
1782 ovs_assert(mf->n_bytes == sizeof *value);
1784 name = strtok_r(str, "/", &save_ptr);
1785 retval = name ? lookup_ipv6(name, value) : EINVAL;
1789 err = xasprintf("%s: could not convert to IPv6 address", str);
1795 netmask = strtok_r(NULL, "/", &save_ptr);
1797 if (inet_pton(AF_INET6, netmask, mask) != 1) {
1798 int prefix = atoi(netmask);
1799 if (prefix <= 0 || prefix > 128) {
1801 return xasprintf("%s: prefix bits not between 1 and 128", s);
1803 *mask = ipv6_create_mask(prefix);
1807 *mask = in6addr_exact;
1815 mf_from_ofp_port_string(const struct mf_field *mf, const char *s,
1816 ovs_be16 *valuep, ovs_be16 *maskp)
1820 ovs_assert(mf->n_bytes == sizeof(ovs_be16));
1822 if (ofputil_port_from_string(s, &port)) {
1823 *valuep = htons(ofp_to_u16(port));
1824 *maskp = OVS_BE16_MAX;
1827 return xasprintf("%s: port value out of range for %s", s, mf->name);
1831 mf_from_ofp_port_string32(const struct mf_field *mf, const char *s,
1832 ovs_be32 *valuep, ovs_be32 *maskp)
1836 ovs_assert(mf->n_bytes == sizeof(ovs_be32));
1837 if (ofputil_port_from_string(s, &port)) {
1838 *valuep = ofputil_port_to_ofp11(port);
1839 *maskp = OVS_BE32_MAX;
1842 return xasprintf("%s: port value out of range for %s", s, mf->name);
1845 struct frag_handling {
1851 static const struct frag_handling all_frags[] = {
1852 #define A FLOW_NW_FRAG_ANY
1853 #define L FLOW_NW_FRAG_LATER
1854 /* name mask value */
1857 { "first", A|L, A },
1858 { "later", A|L, A|L },
1863 { "not_later", L, 0 },
1870 mf_from_frag_string(const char *s, uint8_t *valuep, uint8_t *maskp)
1872 const struct frag_handling *h;
1874 for (h = all_frags; h < &all_frags[ARRAY_SIZE(all_frags)]; h++) {
1875 if (!strcasecmp(s, h->name)) {
1876 /* We force the upper bits of the mask on to make mf_parse_value()
1877 * happy (otherwise it will never think it's an exact match.) */
1878 *maskp = h->mask | ~FLOW_NW_FRAG_MASK;
1884 return xasprintf("%s: unknown fragment type (valid types are \"no\", "
1885 "\"yes\", \"first\", \"later\", \"not_first\"", s);
1889 parse_flow_tun_flags(const char *s_, const char *(*bit_to_string)(uint32_t),
1892 uint32_t result = 0;
1893 char *save_ptr = NULL;
1896 char *s = xstrdup(s_);
1898 for (name = strtok_r((char *)s, " |", &save_ptr); name;
1899 name = strtok_r(NULL, " |", &save_ptr)) {
1901 unsigned long long int flags;
1904 if (ovs_scan(name, "%lli", &flags)) {
1908 name_len = strlen(name);
1909 for (bit = 1; bit; bit <<= 1) {
1910 const char *fname = bit_to_string(bit);
1917 len = strlen(fname);
1918 if (len != name_len) {
1921 if (!strncmp(name, fname, len)) {
1933 *res = htons(result);
1940 mf_from_tun_flags_string(const char *s, ovs_be16 *valuep, ovs_be16 *maskp)
1942 if (!parse_flow_tun_flags(s, flow_tun_flag_to_string, valuep)) {
1943 *maskp = OVS_BE16_MAX;
1947 return xasprintf("%s: unknown tunnel flags (valid flags are \"df\", "
1948 "\"csum\", \"key\")", s);
1952 mf_from_tcp_flags_string(const char *s, ovs_be16 *flagsp, ovs_be16 *maskp)
1959 if (ovs_scan(s, "%"SCNi16"/%"SCNi16"%n", &flags, &mask, &n) && !s[n]) {
1960 *flagsp = htons(flags);
1961 *maskp = htons(mask);
1964 if (ovs_scan(s, "%"SCNi16"%n", &flags, &n) && !s[n]) {
1965 *flagsp = htons(flags);
1966 *maskp = OVS_BE16_MAX;
1970 while (*s != '\0') {
1982 return xasprintf("%s: TCP flag must be preceded by '+' (for SET) "
1983 "or '-' (NOT SET)", s);
1987 name_len = strcspn(s,"+-");
1989 for (bit = 1; bit; bit <<= 1) {
1990 const char *fname = packet_tcp_flag_to_string(bit);
1997 len = strlen(fname);
1998 if (len != name_len) {
2001 if (!strncmp(s, fname, len)) {
2003 return xasprintf("%s: Each TCP flag can be specified only "
2015 return xasprintf("%s: unknown TCP flag(s)", s);
2020 *flagsp = htons(flags);
2021 *maskp = htons(mask);
2026 /* Parses 's', a string value for field 'mf', into 'value' and 'mask'. Returns
2027 * NULL if successful, otherwise a malloc()'d string describing the error. */
2029 mf_parse(const struct mf_field *mf, const char *s,
2030 union mf_value *value, union mf_value *mask)
2034 if (!strcmp(s, "*")) {
2035 memset(value, 0, mf->n_bytes);
2036 memset(mask, 0, mf->n_bytes);
2040 switch (mf->string) {
2042 case MFS_HEXADECIMAL:
2043 error = mf_from_integer_string(mf, s,
2044 (uint8_t *) value, (uint8_t *) mask);
2048 error = mf_from_ethernet_string(mf, s, value->mac, mask->mac);
2052 error = mf_from_ipv4_string(mf, s, &value->be32, &mask->be32);
2056 error = mf_from_ipv6_string(mf, s, &value->ipv6, &mask->ipv6);
2060 error = mf_from_ofp_port_string(mf, s, &value->be16, &mask->be16);
2063 case MFS_OFP_PORT_OXM:
2064 error = mf_from_ofp_port_string32(mf, s, &value->be32, &mask->be32);
2068 error = mf_from_frag_string(s, &value->u8, &mask->u8);
2072 ovs_assert(mf->n_bytes == sizeof(ovs_be16));
2073 error = mf_from_tun_flags_string(s, &value->be16, &mask->be16);
2077 ovs_assert(mf->n_bytes == sizeof(ovs_be16));
2078 error = mf_from_tcp_flags_string(s, &value->be16, &mask->be16);
2085 if (!error && !mf_is_mask_valid(mf, mask)) {
2086 error = xasprintf("%s: invalid mask for field %s", s, mf->name);
2091 /* Parses 's', a string value for field 'mf', into 'value'. Returns NULL if
2092 * successful, otherwise a malloc()'d string describing the error. */
2094 mf_parse_value(const struct mf_field *mf, const char *s, union mf_value *value)
2096 union mf_value mask;
2099 error = mf_parse(mf, s, value, &mask);
2104 if (!is_all_ones((const uint8_t *) &mask, mf->n_bytes)) {
2105 return xasprintf("%s: wildcards not allowed here", s);
2111 mf_format_integer_string(const struct mf_field *mf, const uint8_t *valuep,
2112 const uint8_t *maskp, struct ds *s)
2114 unsigned long long int integer;
2117 ovs_assert(mf->n_bytes <= 8);
2120 for (i = 0; i < mf->n_bytes; i++) {
2121 integer = (integer << 8) | valuep[i];
2123 if (mf->string == MFS_HEXADECIMAL) {
2124 ds_put_format(s, "%#llx", integer);
2126 ds_put_format(s, "%lld", integer);
2130 unsigned long long int mask;
2133 for (i = 0; i < mf->n_bytes; i++) {
2134 mask = (mask << 8) | maskp[i];
2137 /* I guess we could write the mask in decimal for MFS_DECIMAL but I'm
2138 * not sure that that a bit-mask written in decimal is ever easier to
2139 * understand than the same bit-mask written in hexadecimal. */
2140 ds_put_format(s, "/%#llx", mask);
2145 mf_format_frag_string(uint8_t value, uint8_t mask, struct ds *s)
2147 const struct frag_handling *h;
2149 mask &= FLOW_NW_FRAG_MASK;
2152 for (h = all_frags; h < &all_frags[ARRAY_SIZE(all_frags)]; h++) {
2153 if (value == h->value && mask == h->mask) {
2154 ds_put_cstr(s, h->name);
2158 ds_put_cstr(s, "<error>");
2162 mf_format_tnl_flags_string(const ovs_be16 *valuep, struct ds *s)
2164 format_flags(s, flow_tun_flag_to_string, ntohs(*valuep), '|');
2168 mf_format_tcp_flags_string(ovs_be16 value, ovs_be16 mask, struct ds *s)
2170 format_flags_masked(s, NULL, packet_tcp_flag_to_string, ntohs(value),
2174 /* Appends to 's' a string representation of field 'mf' whose value is in
2175 * 'value' and 'mask'. 'mask' may be NULL to indicate an exact match. */
2177 mf_format(const struct mf_field *mf,
2178 const union mf_value *value, const union mf_value *mask,
2182 if (is_all_zeros(mask, mf->n_bytes)) {
2183 ds_put_cstr(s, "ANY");
2185 } else if (is_all_ones(mask, mf->n_bytes)) {
2190 switch (mf->string) {
2191 case MFS_OFP_PORT_OXM:
2194 ofputil_port_from_ofp11(value->be32, &port);
2195 ofputil_format_port(port, s);
2201 ofputil_format_port(u16_to_ofp(ntohs(value->be16)), s);
2206 case MFS_HEXADECIMAL:
2207 mf_format_integer_string(mf, (uint8_t *) value, (uint8_t *) mask, s);
2211 eth_format_masked(value->mac, mask->mac, s);
2215 ip_format_masked(value->be32, mask ? mask->be32 : OVS_BE32_MAX, s);
2219 print_ipv6_masked(s, &value->ipv6, mask ? &mask->ipv6 : NULL);
2223 mf_format_frag_string(value->u8, mask ? mask->u8 : UINT8_MAX, s);
2227 mf_format_tnl_flags_string(&value->be16, s);
2231 mf_format_tcp_flags_string(value->be16,
2232 mask ? mask->be16 : OVS_BE16_MAX, s);
2240 /* Makes subfield 'sf' within 'flow' exactly match the 'sf->n_bits'
2241 * least-significant bits in 'x'.
2244 mf_write_subfield_flow(const struct mf_subfield *sf,
2245 const union mf_subvalue *x, struct flow *flow)
2247 const struct mf_field *field = sf->field;
2248 union mf_value value;
2250 mf_get_value(field, flow, &value);
2251 bitwise_copy(x, sizeof *x, 0, &value, field->n_bytes,
2252 sf->ofs, sf->n_bits);
2253 mf_set_flow_value(field, &value, flow);
2256 /* Makes subfield 'sf' within 'match' exactly match the 'sf->n_bits'
2257 * least-significant bits in 'x'.
2260 mf_write_subfield(const struct mf_subfield *sf, const union mf_subvalue *x,
2261 struct match *match)
2263 const struct mf_field *field = sf->field;
2264 union mf_value value, mask;
2266 mf_get(field, match, &value, &mask);
2267 bitwise_copy(x, sizeof *x, 0, &value, field->n_bytes, sf->ofs, sf->n_bits);
2268 bitwise_one ( &mask, field->n_bytes, sf->ofs, sf->n_bits);
2269 mf_set(field, &value, &mask, match);
2272 /* Initializes 'x' to the value of 'sf' within 'flow'. 'sf' must be valid for
2273 * reading 'flow', e.g. as checked by mf_check_src(). */
2275 mf_read_subfield(const struct mf_subfield *sf, const struct flow *flow,
2276 union mf_subvalue *x)
2278 union mf_value value;
2280 mf_get_value(sf->field, flow, &value);
2282 memset(x, 0, sizeof *x);
2283 bitwise_copy(&value, sf->field->n_bytes, sf->ofs,
2288 /* Returns the value of 'sf' within 'flow'. 'sf' must be valid for reading
2289 * 'flow', e.g. as checked by mf_check_src() and sf->n_bits must be 64 or
2292 mf_get_subfield(const struct mf_subfield *sf, const struct flow *flow)
2294 union mf_value value;
2296 mf_get_value(sf->field, flow, &value);
2297 return bitwise_get(&value, sf->field->n_bytes, sf->ofs, sf->n_bits);
2301 mf_format_subvalue(const union mf_subvalue *subvalue, struct ds *s)
2305 for (i = 0; i < ARRAY_SIZE(subvalue->u8); i++) {
2306 if (subvalue->u8[i]) {
2307 ds_put_format(s, "0x%"PRIx8, subvalue->u8[i]);
2308 for (i++; i < ARRAY_SIZE(subvalue->u8); i++) {
2309 ds_put_format(s, "%02"PRIx8, subvalue->u8[i]);
2314 ds_put_char(s, '0');
2318 field_array_set(enum mf_field_id id, const union mf_value *value,
2319 struct field_array *fa)
2321 ovs_assert(id < MFF_N_IDS);
2322 bitmap_set1(fa->used.bm, id);
2323 fa->value[id] = *value;