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"
28 #include "ofp-errors.h"
30 #include "ovs-thread.h"
34 #include "socket-util.h"
35 #include "unaligned.h"
38 VLOG_DEFINE_THIS_MODULE(meta_flow);
40 #define FLOW_U32OFS(FIELD) \
41 offsetof(struct flow, FIELD) % 4 ? -1 : offsetof(struct flow, FIELD) / 4
43 #define MF_FIELD_SIZES(MEMBER) \
44 sizeof ((union mf_value *)0)->MEMBER, \
45 8 * sizeof ((union mf_value *)0)->MEMBER
47 extern const struct mf_field mf_fields[MFF_N_IDS]; /* Silence a warning. */
49 const struct mf_field mf_fields[MFF_N_IDS] = {
50 #include "meta-flow.inc"
53 /* Maps an NXM or OXM header value to an mf_field. */
55 struct hmap_node hmap_node; /* In 'all_fields' hmap. */
56 uint32_t header; /* NXM or OXM header value. */
57 const struct mf_field *mf;
60 /* Contains 'struct nxm_field's. */
61 static struct hmap all_fields;
63 /* Maps from an mf_field's 'name' or 'extra_name' to the mf_field. */
64 static struct shash mf_by_name;
66 /* Rate limit for parse errors. These always indicate a bug in an OpenFlow
67 * controller and so there's not much point in showing a lot of them. */
68 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
70 const struct mf_field *mf_from_nxm_header__(uint32_t header);
71 static void nxm_init(void);
73 /* Returns the field with the given 'name', or a null pointer if no field has
75 const struct mf_field *
76 mf_from_name(const char *name)
79 return shash_find_data(&mf_by_name, name);
83 add_nxm_field(uint32_t header, const struct mf_field *mf)
87 f = xmalloc(sizeof *f);
88 hmap_insert(&all_fields, &f->hmap_node, hash_int(header, 0));
94 nxm_init_add_field(const struct mf_field *mf, uint32_t header)
97 ovs_assert(!mf_from_nxm_header__(header));
98 add_nxm_field(header, mf);
99 if (mf->maskable != MFM_NONE) {
100 add_nxm_field(NXM_MAKE_WILD_HEADER(header), mf);
110 hmap_init(&all_fields);
111 shash_init(&mf_by_name);
112 for (i = 0; i < MFF_N_IDS; i++) {
113 const struct mf_field *mf = &mf_fields[i];
115 ovs_assert(mf->id == i); /* Fields must be in the enum order. */
117 nxm_init_add_field(mf, mf->nxm_header);
118 if (mf->oxm_header != mf->nxm_header) {
119 nxm_init_add_field(mf, mf->oxm_header);
122 shash_add_once(&mf_by_name, mf->name, mf);
123 if (mf->extra_name) {
124 shash_add_once(&mf_by_name, mf->extra_name, mf);
132 static pthread_once_t once = PTHREAD_ONCE_INIT;
133 pthread_once(&once, nxm_do_init);
136 const struct mf_field *
137 mf_from_nxm_header(uint32_t header)
140 return mf_from_nxm_header__(header);
143 const struct mf_field *
144 mf_from_nxm_header__(uint32_t header)
146 const struct nxm_field *f;
148 HMAP_FOR_EACH_IN_BUCKET (f, hmap_node, hash_int(header, 0), &all_fields) {
149 if (f->header == header) {
158 mf_oxm_header(enum mf_field_id id, enum ofp_version oxm_version)
160 const struct mf_field *field = mf_from_id(id);
162 return (oxm_version >= field->oxm_version
164 : field->nxm_header);
167 /* Returns true if 'wc' wildcards all the bits in field 'mf', false if 'wc'
168 * specifies at least one bit in the field.
170 * The caller is responsible for ensuring that 'wc' corresponds to a flow that
171 * meets 'mf''s prerequisites. */
173 mf_is_all_wild(const struct mf_field *mf, const struct flow_wildcards *wc)
177 return !wc->masks.dp_hash;
179 return !wc->masks.recirc_id;
181 return !wc->masks.tunnel.ip_src;
183 return !wc->masks.tunnel.ip_dst;
188 return !wc->masks.tunnel.tun_id;
190 return !wc->masks.metadata;
192 case MFF_IN_PORT_OXM:
193 return !wc->masks.in_port.ofp_port;
194 case MFF_SKB_PRIORITY:
195 return !wc->masks.skb_priority;
197 return !wc->masks.pkt_mark;
199 return !wc->masks.regs[mf->id - MFF_REG0];
201 return !flow_get_xreg(&wc->masks, mf->id - MFF_XREG0);
204 return eth_addr_is_zero(wc->masks.dl_src);
206 return eth_addr_is_zero(wc->masks.dl_dst);
208 return !wc->masks.dl_type;
212 return eth_addr_is_zero(wc->masks.arp_sha);
216 return eth_addr_is_zero(wc->masks.arp_tha);
219 return !wc->masks.vlan_tci;
221 return !(wc->masks.vlan_tci & htons(VLAN_VID_MASK));
223 return !(wc->masks.vlan_tci & htons(VLAN_VID_MASK | VLAN_CFI));
224 case MFF_DL_VLAN_PCP:
226 return !(wc->masks.vlan_tci & htons(VLAN_PCP_MASK));
229 return !(wc->masks.mpls_lse[0] & htonl(MPLS_LABEL_MASK));
231 return !(wc->masks.mpls_lse[0] & htonl(MPLS_TC_MASK));
233 return !(wc->masks.mpls_lse[0] & htonl(MPLS_BOS_MASK));
236 return !wc->masks.nw_src;
238 return !wc->masks.nw_dst;
241 return ipv6_mask_is_any(&wc->masks.ipv6_src);
243 return ipv6_mask_is_any(&wc->masks.ipv6_dst);
246 return !wc->masks.ipv6_label;
249 return !wc->masks.nw_proto;
251 case MFF_IP_DSCP_SHIFTED:
252 return !(wc->masks.nw_tos & IP_DSCP_MASK);
254 return !(wc->masks.nw_tos & IP_ECN_MASK);
256 return !wc->masks.nw_ttl;
259 return ipv6_mask_is_any(&wc->masks.nd_target);
262 return !(wc->masks.nw_frag & FLOW_NW_FRAG_MASK);
265 return !wc->masks.nw_proto;
267 return !wc->masks.nw_src;
269 return !wc->masks.nw_dst;
274 case MFF_ICMPV4_TYPE:
275 case MFF_ICMPV6_TYPE:
276 return !wc->masks.tp_src;
280 case MFF_ICMPV4_CODE:
281 case MFF_ICMPV6_CODE:
282 return !wc->masks.tp_dst;
284 return !wc->masks.tcp_flags;
292 /* Initializes 'mask' with the wildcard bit pattern for field 'mf' within 'wc'.
293 * Each bit in 'mask' will be set to 1 if the bit is significant for matching
294 * purposes, or to 0 if it is wildcarded.
296 * The caller is responsible for ensuring that 'wc' corresponds to a flow that
297 * meets 'mf''s prerequisites. */
299 mf_get_mask(const struct mf_field *mf, const struct flow_wildcards *wc,
300 union mf_value *mask)
302 mf_get_value(mf, &wc->masks, mask);
305 /* Tests whether 'mask' is a valid wildcard bit pattern for 'mf'. Returns true
306 * if the mask is valid, false otherwise. */
308 mf_is_mask_valid(const struct mf_field *mf, const union mf_value *mask)
310 switch (mf->maskable) {
312 return (is_all_zeros(mask, mf->n_bytes) ||
313 is_all_ones(mask, mf->n_bytes));
322 /* Returns true if 'flow' meets the prerequisites for 'mf', false otherwise. */
324 mf_are_prereqs_ok(const struct mf_field *mf, const struct flow *flow)
326 switch (mf->prereqs) {
331 return (flow->dl_type == htons(ETH_TYPE_ARP) ||
332 flow->dl_type == htons(ETH_TYPE_RARP));
334 return flow->dl_type == htons(ETH_TYPE_IP);
336 return flow->dl_type == htons(ETH_TYPE_IPV6);
338 return (flow->vlan_tci & htons(VLAN_CFI)) != 0;
340 return eth_type_mpls(flow->dl_type);
342 return is_ip_any(flow);
345 return is_ip_any(flow) && flow->nw_proto == IPPROTO_TCP;
347 return is_ip_any(flow) && flow->nw_proto == IPPROTO_UDP;
349 return is_ip_any(flow) && flow->nw_proto == IPPROTO_SCTP;
351 return is_icmpv4(flow);
353 return is_icmpv6(flow);
356 return (is_icmpv6(flow)
357 && flow->tp_dst == htons(0)
358 && (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT) ||
359 flow->tp_src == htons(ND_NEIGHBOR_ADVERT)));
361 return (is_icmpv6(flow)
362 && flow->tp_dst == htons(0)
363 && (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT)));
365 return (is_icmpv6(flow)
366 && flow->tp_dst == htons(0)
367 && (flow->tp_src == htons(ND_NEIGHBOR_ADVERT)));
373 /* Set field and it's prerequisities in the mask.
374 * This is only ever called for writeable 'mf's, but we do not make the
375 * distinction here. */
377 mf_mask_field_and_prereqs(const struct mf_field *mf, struct flow *mask)
379 static const union mf_value exact_match_mask = MF_EXACT_MASK_INITIALIZER;
381 mf_set_flow_value(mf, &exact_match_mask, mask);
383 switch (mf->prereqs) {
387 mask->tp_src = OVS_BE16_MAX;
388 mask->tp_dst = OVS_BE16_MAX;
395 mask->nw_proto = 0xff;
402 mask->dl_type = OVS_BE16_MAX;
405 mask->vlan_tci |= htons(VLAN_CFI);
413 /* Returns true if 'value' may be a valid value *as part of a masked match*,
416 * A value is not rejected just because it is not valid for the field in
417 * question, but only if it doesn't make sense to test the bits in question at
418 * all. For example, the MFF_VLAN_TCI field will never have a nonzero value
419 * without the VLAN_CFI bit being set, but we can't reject those values because
420 * it is still legitimate to test just for those bits (see the documentation
421 * for NXM_OF_VLAN_TCI in nicira-ext.h). On the other hand, there is never a
422 * reason to set the low bit of MFF_IP_DSCP to 1, so we reject that. */
424 mf_is_value_valid(const struct mf_field *mf, const union mf_value *value)
437 case MFF_SKB_PRIORITY:
461 case MFF_ICMPV4_TYPE:
462 case MFF_ICMPV4_CODE:
463 case MFF_ICMPV6_TYPE:
464 case MFF_ICMPV6_CODE:
470 case MFF_IN_PORT_OXM: {
472 return !ofputil_port_from_ofp11(value->be32, &port);
476 return !(value->u8 & ~IP_DSCP_MASK);
477 case MFF_IP_DSCP_SHIFTED:
478 return !(value->u8 & (~IP_DSCP_MASK >> 2));
480 return !(value->u8 & ~IP_ECN_MASK);
482 return !(value->u8 & ~FLOW_NW_FRAG_MASK);
484 return !(value->be16 & ~htons(0x0fff));
487 return !(value->be16 & htons(0xff00));
490 return !(value->be16 & htons(VLAN_CFI | VLAN_PCP_MASK));
492 return !(value->be16 & htons(VLAN_PCP_MASK));
494 case MFF_DL_VLAN_PCP:
496 return !(value->u8 & ~(VLAN_PCP_MASK >> VLAN_PCP_SHIFT));
499 return !(value->be32 & ~htonl(IPV6_LABEL_MASK));
502 return !(value->be32 & ~htonl(MPLS_LABEL_MASK >> MPLS_LABEL_SHIFT));
505 return !(value->u8 & ~(MPLS_TC_MASK >> MPLS_TC_SHIFT));
508 return !(value->u8 & ~(MPLS_BOS_MASK >> MPLS_BOS_SHIFT));
516 /* Copies the value of field 'mf' from 'flow' into 'value'. The caller is
517 * responsible for ensuring that 'flow' meets 'mf''s prerequisites. */
519 mf_get_value(const struct mf_field *mf, const struct flow *flow,
520 union mf_value *value)
524 value->be32 = htonl(flow->dp_hash);
527 value->be32 = htonl(flow->recirc_id);
530 value->be64 = flow->tunnel.tun_id;
533 value->be32 = flow->tunnel.ip_src;
536 value->be32 = flow->tunnel.ip_dst;
539 value->be16 = htons(flow->tunnel.flags);
542 value->u8 = flow->tunnel.ip_ttl;
545 value->u8 = flow->tunnel.ip_tos;
549 value->be64 = flow->metadata;
553 value->be16 = htons(ofp_to_u16(flow->in_port.ofp_port));
555 case MFF_IN_PORT_OXM:
556 value->be32 = ofputil_port_to_ofp11(flow->in_port.ofp_port);
559 case MFF_SKB_PRIORITY:
560 value->be32 = htonl(flow->skb_priority);
564 value->be32 = htonl(flow->pkt_mark);
568 value->be32 = htonl(flow->regs[mf->id - MFF_REG0]);
572 value->be64 = htonll(flow_get_xreg(flow, mf->id - MFF_XREG0));
576 memcpy(value->mac, flow->dl_src, ETH_ADDR_LEN);
580 memcpy(value->mac, flow->dl_dst, ETH_ADDR_LEN);
584 value->be16 = flow->dl_type;
588 value->be16 = flow->vlan_tci;
592 value->be16 = flow->vlan_tci & htons(VLAN_VID_MASK);
595 value->be16 = flow->vlan_tci & htons(VLAN_VID_MASK | VLAN_CFI);
598 case MFF_DL_VLAN_PCP:
600 value->u8 = vlan_tci_to_pcp(flow->vlan_tci);
604 value->be32 = htonl(mpls_lse_to_label(flow->mpls_lse[0]));
608 value->u8 = mpls_lse_to_tc(flow->mpls_lse[0]);
612 value->u8 = mpls_lse_to_bos(flow->mpls_lse[0]);
616 value->be32 = flow->nw_src;
620 value->be32 = flow->nw_dst;
624 value->ipv6 = flow->ipv6_src;
628 value->ipv6 = flow->ipv6_dst;
632 value->be32 = flow->ipv6_label;
636 value->u8 = flow->nw_proto;
640 value->u8 = flow->nw_tos & IP_DSCP_MASK;
643 case MFF_IP_DSCP_SHIFTED:
644 value->u8 = flow->nw_tos >> 2;
648 value->u8 = flow->nw_tos & IP_ECN_MASK;
652 value->u8 = flow->nw_ttl;
656 value->u8 = flow->nw_frag;
660 value->be16 = htons(flow->nw_proto);
664 value->be32 = flow->nw_src;
668 value->be32 = flow->nw_dst;
673 memcpy(value->mac, flow->arp_sha, ETH_ADDR_LEN);
678 memcpy(value->mac, flow->arp_tha, ETH_ADDR_LEN);
684 value->be16 = flow->tp_src;
690 value->be16 = flow->tp_dst;
694 value->be16 = flow->tcp_flags;
697 case MFF_ICMPV4_TYPE:
698 case MFF_ICMPV6_TYPE:
699 value->u8 = ntohs(flow->tp_src);
702 case MFF_ICMPV4_CODE:
703 case MFF_ICMPV6_CODE:
704 value->u8 = ntohs(flow->tp_dst);
708 value->ipv6 = flow->nd_target;
717 /* Makes 'match' match field 'mf' exactly, with the value matched taken from
718 * 'value'. The caller is responsible for ensuring that 'match' meets 'mf''s
721 mf_set_value(const struct mf_field *mf,
722 const union mf_value *value, struct match *match)
726 match_set_dp_hash(match, ntohl(value->be32));
729 match_set_recirc_id(match, ntohl(value->be32));
732 match_set_tun_id(match, value->be64);
735 match_set_tun_src(match, value->be32);
738 match_set_tun_dst(match, value->be32);
741 match_set_tun_flags(match, ntohs(value->be16));
744 match_set_tun_tos(match, value->u8);
747 match_set_tun_ttl(match, value->u8);
751 match_set_metadata(match, value->be64);
755 match_set_in_port(match, u16_to_ofp(ntohs(value->be16)));
758 case MFF_IN_PORT_OXM: {
760 ofputil_port_from_ofp11(value->be32, &port);
761 match_set_in_port(match, port);
765 case MFF_SKB_PRIORITY:
766 match_set_skb_priority(match, ntohl(value->be32));
770 match_set_pkt_mark(match, ntohl(value->be32));
774 match_set_reg(match, mf->id - MFF_REG0, ntohl(value->be32));
778 match_set_xreg(match, mf->id - MFF_XREG0, ntohll(value->be64));
782 match_set_dl_src(match, value->mac);
786 match_set_dl_dst(match, value->mac);
790 match_set_dl_type(match, value->be16);
794 match_set_dl_tci(match, value->be16);
798 match_set_dl_vlan(match, value->be16);
801 match_set_vlan_vid(match, value->be16);
804 case MFF_DL_VLAN_PCP:
806 match_set_dl_vlan_pcp(match, value->u8);
810 match_set_mpls_label(match, 0, value->be32);
814 match_set_mpls_tc(match, 0, value->u8);
818 match_set_mpls_bos(match, 0, value->u8);
822 match_set_nw_src(match, value->be32);
826 match_set_nw_dst(match, value->be32);
830 match_set_ipv6_src(match, &value->ipv6);
834 match_set_ipv6_dst(match, &value->ipv6);
838 match_set_ipv6_label(match, value->be32);
842 match_set_nw_proto(match, value->u8);
846 match_set_nw_dscp(match, value->u8);
849 case MFF_IP_DSCP_SHIFTED:
850 match_set_nw_dscp(match, value->u8 << 2);
854 match_set_nw_ecn(match, value->u8);
858 match_set_nw_ttl(match, value->u8);
862 match_set_nw_frag(match, value->u8);
866 match_set_nw_proto(match, ntohs(value->be16));
870 match_set_nw_src(match, value->be32);
874 match_set_nw_dst(match, value->be32);
879 match_set_arp_sha(match, value->mac);
884 match_set_arp_tha(match, value->mac);
890 match_set_tp_src(match, value->be16);
896 match_set_tp_dst(match, value->be16);
900 match_set_tcp_flags(match, value->be16);
903 case MFF_ICMPV4_TYPE:
904 case MFF_ICMPV6_TYPE:
905 match_set_icmp_type(match, value->u8);
908 case MFF_ICMPV4_CODE:
909 case MFF_ICMPV6_CODE:
910 match_set_icmp_code(match, value->u8);
914 match_set_nd_target(match, &value->ipv6);
923 /* Unwildcard 'mask' member field described by 'mf'. The caller is
924 * responsible for ensuring that 'mask' meets 'mf''s prerequisites. */
926 mf_mask_field(const struct mf_field *mf, struct flow *mask)
928 static const union mf_value exact_match_mask = MF_EXACT_MASK_INITIALIZER;
930 /* For MFF_DL_VLAN, we cannot send a all 1's to flow_set_dl_vlan()
931 * as that will be considered as OFP10_VLAN_NONE. So consider it as a
932 * special case. For the rest, calling mf_set_flow_value() is good
934 if (mf->id == MFF_DL_VLAN) {
935 flow_set_dl_vlan(mask, htons(VLAN_VID_MASK));
937 mf_set_flow_value(mf, &exact_match_mask, mask);
941 /* Sets 'flow' member field described by 'mf' to 'value'. The caller is
942 * responsible for ensuring that 'flow' meets 'mf''s prerequisites.*/
944 mf_set_flow_value(const struct mf_field *mf,
945 const union mf_value *value, struct flow *flow)
949 flow->dp_hash = ntohl(value->be32);
952 flow->recirc_id = ntohl(value->be32);
955 flow->tunnel.tun_id = value->be64;
958 flow->tunnel.ip_src = value->be32;
961 flow->tunnel.ip_dst = value->be32;
964 flow->tunnel.flags = ntohs(value->be16);
967 flow->tunnel.ip_tos = value->u8;
970 flow->tunnel.ip_ttl = value->u8;
974 flow->metadata = value->be64;
978 flow->in_port.ofp_port = u16_to_ofp(ntohs(value->be16));
981 case MFF_IN_PORT_OXM: {
983 ofputil_port_from_ofp11(value->be32, &port);
984 flow->in_port.ofp_port = port;
988 case MFF_SKB_PRIORITY:
989 flow->skb_priority = ntohl(value->be32);
993 flow->pkt_mark = ntohl(value->be32);
997 flow->regs[mf->id - MFF_REG0] = ntohl(value->be32);
1001 flow_set_xreg(flow, mf->id - MFF_XREG0, ntohll(value->be64));
1005 memcpy(flow->dl_src, value->mac, ETH_ADDR_LEN);
1009 memcpy(flow->dl_dst, value->mac, ETH_ADDR_LEN);
1013 flow->dl_type = value->be16;
1017 flow->vlan_tci = value->be16;
1021 flow_set_dl_vlan(flow, value->be16);
1024 flow_set_vlan_vid(flow, value->be16);
1027 case MFF_DL_VLAN_PCP:
1029 flow_set_vlan_pcp(flow, value->u8);
1032 case MFF_MPLS_LABEL:
1033 flow_set_mpls_label(flow, 0, value->be32);
1037 flow_set_mpls_tc(flow, 0, value->u8);
1041 flow_set_mpls_bos(flow, 0, value->u8);
1045 flow->nw_src = value->be32;
1049 flow->nw_dst = value->be32;
1053 flow->ipv6_src = value->ipv6;
1057 flow->ipv6_dst = value->ipv6;
1060 case MFF_IPV6_LABEL:
1061 flow->ipv6_label = value->be32 & ~htonl(IPV6_LABEL_MASK);
1065 flow->nw_proto = value->u8;
1069 flow->nw_tos &= ~IP_DSCP_MASK;
1070 flow->nw_tos |= value->u8 & IP_DSCP_MASK;
1073 case MFF_IP_DSCP_SHIFTED:
1074 flow->nw_tos &= ~IP_DSCP_MASK;
1075 flow->nw_tos |= value->u8 << 2;
1079 flow->nw_tos &= ~IP_ECN_MASK;
1080 flow->nw_tos |= value->u8 & IP_ECN_MASK;
1084 flow->nw_ttl = value->u8;
1088 flow->nw_frag = value->u8 & FLOW_NW_FRAG_MASK;
1092 flow->nw_proto = ntohs(value->be16);
1096 flow->nw_src = value->be32;
1100 flow->nw_dst = value->be32;
1105 memcpy(flow->arp_sha, value->mac, ETH_ADDR_LEN);
1110 memcpy(flow->arp_tha, value->mac, ETH_ADDR_LEN);
1116 flow->tp_src = value->be16;
1122 flow->tp_dst = value->be16;
1126 flow->tcp_flags = value->be16;
1129 case MFF_ICMPV4_TYPE:
1130 case MFF_ICMPV6_TYPE:
1131 flow->tp_src = htons(value->u8);
1134 case MFF_ICMPV4_CODE:
1135 case MFF_ICMPV6_CODE:
1136 flow->tp_dst = htons(value->u8);
1140 flow->nd_target = value->ipv6;
1149 /* Returns true if 'mf' has a zero value in 'flow', false if it is nonzero.
1151 * The caller is responsible for ensuring that 'flow' meets 'mf''s
1154 mf_is_zero(const struct mf_field *mf, const struct flow *flow)
1156 union mf_value value;
1158 mf_get_value(mf, flow, &value);
1159 return is_all_zeros(&value, mf->n_bytes);
1162 /* Makes 'match' wildcard field 'mf'.
1164 * The caller is responsible for ensuring that 'match' meets 'mf''s
1167 mf_set_wild(const struct mf_field *mf, struct match *match)
1171 match->flow.dp_hash = 0;
1172 match->wc.masks.dp_hash = 0;
1175 match->flow.recirc_id = 0;
1176 match->wc.masks.recirc_id = 0;
1179 match_set_tun_id_masked(match, htonll(0), htonll(0));
1182 match_set_tun_src_masked(match, htonl(0), htonl(0));
1185 match_set_tun_dst_masked(match, htonl(0), htonl(0));
1188 match_set_tun_flags_masked(match, 0, 0);
1191 match_set_tun_tos_masked(match, 0, 0);
1194 match_set_tun_ttl_masked(match, 0, 0);
1198 match_set_metadata_masked(match, htonll(0), htonll(0));
1202 case MFF_IN_PORT_OXM:
1203 match->flow.in_port.ofp_port = 0;
1204 match->wc.masks.in_port.ofp_port = 0;
1207 case MFF_SKB_PRIORITY:
1208 match->flow.skb_priority = 0;
1209 match->wc.masks.skb_priority = 0;
1213 match->flow.pkt_mark = 0;
1214 match->wc.masks.pkt_mark = 0;
1218 match_set_reg_masked(match, mf->id - MFF_REG0, 0, 0);
1222 match_set_xreg_masked(match, mf->id - MFF_XREG0, 0, 0);
1226 memset(match->flow.dl_src, 0, ETH_ADDR_LEN);
1227 memset(match->wc.masks.dl_src, 0, ETH_ADDR_LEN);
1231 memset(match->flow.dl_dst, 0, ETH_ADDR_LEN);
1232 memset(match->wc.masks.dl_dst, 0, ETH_ADDR_LEN);
1236 match->flow.dl_type = htons(0);
1237 match->wc.masks.dl_type = htons(0);
1241 match_set_dl_tci_masked(match, htons(0), htons(0));
1246 match_set_any_vid(match);
1249 case MFF_DL_VLAN_PCP:
1251 match_set_any_pcp(match);
1254 case MFF_MPLS_LABEL:
1255 match_set_any_mpls_label(match, 0);
1259 match_set_any_mpls_tc(match, 0);
1263 match_set_any_mpls_bos(match, 0);
1268 match_set_nw_src_masked(match, htonl(0), htonl(0));
1273 match_set_nw_dst_masked(match, htonl(0), htonl(0));
1277 memset(&match->wc.masks.ipv6_src, 0, sizeof match->wc.masks.ipv6_src);
1278 memset(&match->flow.ipv6_src, 0, sizeof match->flow.ipv6_src);
1282 memset(&match->wc.masks.ipv6_dst, 0, sizeof match->wc.masks.ipv6_dst);
1283 memset(&match->flow.ipv6_dst, 0, sizeof match->flow.ipv6_dst);
1286 case MFF_IPV6_LABEL:
1287 match->wc.masks.ipv6_label = htonl(0);
1288 match->flow.ipv6_label = htonl(0);
1292 match->wc.masks.nw_proto = 0;
1293 match->flow.nw_proto = 0;
1297 case MFF_IP_DSCP_SHIFTED:
1298 match->wc.masks.nw_tos &= ~IP_DSCP_MASK;
1299 match->flow.nw_tos &= ~IP_DSCP_MASK;
1303 match->wc.masks.nw_tos &= ~IP_ECN_MASK;
1304 match->flow.nw_tos &= ~IP_ECN_MASK;
1308 match->wc.masks.nw_ttl = 0;
1309 match->flow.nw_ttl = 0;
1313 match->wc.masks.nw_frag |= FLOW_NW_FRAG_MASK;
1314 match->flow.nw_frag &= ~FLOW_NW_FRAG_MASK;
1318 match->wc.masks.nw_proto = 0;
1319 match->flow.nw_proto = 0;
1324 memset(match->flow.arp_sha, 0, ETH_ADDR_LEN);
1325 memset(match->wc.masks.arp_sha, 0, ETH_ADDR_LEN);
1330 memset(match->flow.arp_tha, 0, ETH_ADDR_LEN);
1331 memset(match->wc.masks.arp_tha, 0, ETH_ADDR_LEN);
1337 case MFF_ICMPV4_TYPE:
1338 case MFF_ICMPV6_TYPE:
1339 match->wc.masks.tp_src = htons(0);
1340 match->flow.tp_src = htons(0);
1346 case MFF_ICMPV4_CODE:
1347 case MFF_ICMPV6_CODE:
1348 match->wc.masks.tp_dst = htons(0);
1349 match->flow.tp_dst = htons(0);
1353 match->wc.masks.tcp_flags = htons(0);
1354 match->flow.tcp_flags = htons(0);
1358 memset(&match->wc.masks.nd_target, 0,
1359 sizeof match->wc.masks.nd_target);
1360 memset(&match->flow.nd_target, 0, sizeof match->flow.nd_target);
1369 /* Makes 'match' match field 'mf' with the specified 'value' and 'mask'.
1370 * 'value' specifies a value to match and 'mask' specifies a wildcard pattern,
1371 * with a 1-bit indicating that the corresponding value bit must match and a
1372 * 0-bit indicating a don't-care.
1374 * If 'mask' is NULL or points to all-1-bits, then this call is equivalent to
1375 * mf_set_value(mf, value, match). If 'mask' points to all-0-bits, then this
1376 * call is equivalent to mf_set_wild(mf, match).
1378 * 'mask' must be a valid mask for 'mf' (see mf_is_mask_valid()). The caller
1379 * is responsible for ensuring that 'match' meets 'mf''s prerequisites. */
1380 enum ofputil_protocol
1381 mf_set(const struct mf_field *mf,
1382 const union mf_value *value, const union mf_value *mask,
1383 struct match *match)
1385 if (!mask || is_all_ones(mask, mf->n_bytes)) {
1386 mf_set_value(mf, value, match);
1387 return mf->usable_protocols_exact;
1388 } else if (is_all_zeros(mask, mf->n_bytes)) {
1389 mf_set_wild(mf, match);
1390 return OFPUTIL_P_ANY;
1396 case MFF_IN_PORT_OXM:
1397 case MFF_SKB_PRIORITY:
1400 case MFF_DL_VLAN_PCP:
1402 case MFF_MPLS_LABEL:
1408 case MFF_IP_DSCP_SHIFTED:
1411 case MFF_ICMPV4_TYPE:
1412 case MFF_ICMPV4_CODE:
1413 case MFF_ICMPV6_TYPE:
1414 case MFF_ICMPV6_CODE:
1415 return OFPUTIL_P_NONE;
1418 match_set_dp_hash_masked(match, ntohl(value->be32), ntohl(mask->be32));
1421 match_set_tun_id_masked(match, value->be64, mask->be64);
1424 match_set_tun_src_masked(match, value->be32, mask->be32);
1427 match_set_tun_dst_masked(match, value->be32, mask->be32);
1430 match_set_tun_flags_masked(match, ntohs(value->be16), ntohs(mask->be16));
1433 match_set_tun_ttl_masked(match, value->u8, mask->u8);
1436 match_set_tun_tos_masked(match, value->u8, mask->u8);
1440 match_set_metadata_masked(match, value->be64, mask->be64);
1444 match_set_reg_masked(match, mf->id - MFF_REG0,
1445 ntohl(value->be32), ntohl(mask->be32));
1449 match_set_xreg_masked(match, mf->id - MFF_XREG0,
1450 ntohll(value->be64), ntohll(mask->be64));
1454 match_set_pkt_mark_masked(match, ntohl(value->be32),
1459 match_set_dl_dst_masked(match, value->mac, mask->mac);
1463 match_set_dl_src_masked(match, value->mac, mask->mac);
1468 match_set_arp_sha_masked(match, value->mac, mask->mac);
1473 match_set_arp_tha_masked(match, value->mac, mask->mac);
1477 match_set_dl_tci_masked(match, value->be16, mask->be16);
1481 match_set_vlan_vid_masked(match, value->be16, mask->be16);
1485 match_set_nw_src_masked(match, value->be32, mask->be32);
1489 match_set_nw_dst_masked(match, value->be32, mask->be32);
1493 match_set_ipv6_src_masked(match, &value->ipv6, &mask->ipv6);
1497 match_set_ipv6_dst_masked(match, &value->ipv6, &mask->ipv6);
1500 case MFF_IPV6_LABEL:
1501 if ((mask->be32 & htonl(IPV6_LABEL_MASK)) == htonl(IPV6_LABEL_MASK)) {
1502 mf_set_value(mf, value, match);
1504 match_set_ipv6_label_masked(match, value->be32, mask->be32);
1509 match_set_nd_target_masked(match, &value->ipv6, &mask->ipv6);
1513 match_set_nw_frag_masked(match, value->u8, mask->u8);
1517 match_set_nw_src_masked(match, value->be32, mask->be32);
1521 match_set_nw_dst_masked(match, value->be32, mask->be32);
1527 match_set_tp_src_masked(match, value->be16, mask->be16);
1533 match_set_tp_dst_masked(match, value->be16, mask->be16);
1537 match_set_tcp_flags_masked(match, value->be16, mask->be16);
1545 return ((mf->usable_protocols_bitwise == mf->usable_protocols_cidr
1546 || ip_is_cidr(mask->be32))
1547 ? mf->usable_protocols_cidr
1548 : mf->usable_protocols_bitwise);
1552 mf_check__(const struct mf_subfield *sf, const struct flow *flow,
1556 VLOG_WARN_RL(&rl, "unknown %s field", type);
1557 return OFPERR_OFPBAC_BAD_SET_TYPE;
1558 } else if (!sf->n_bits) {
1559 VLOG_WARN_RL(&rl, "zero bit %s field %s", type, sf->field->name);
1560 return OFPERR_OFPBAC_BAD_SET_LEN;
1561 } else if (sf->ofs >= sf->field->n_bits) {
1562 VLOG_WARN_RL(&rl, "bit offset %d exceeds %d-bit width of %s field %s",
1563 sf->ofs, sf->field->n_bits, type, sf->field->name);
1564 return OFPERR_OFPBAC_BAD_SET_LEN;
1565 } else if (sf->ofs + sf->n_bits > sf->field->n_bits) {
1566 VLOG_WARN_RL(&rl, "bit offset %d and width %d exceeds %d-bit width "
1567 "of %s field %s", sf->ofs, sf->n_bits,
1568 sf->field->n_bits, type, sf->field->name);
1569 return OFPERR_OFPBAC_BAD_SET_LEN;
1570 } else if (flow && !mf_are_prereqs_ok(sf->field, flow)) {
1571 VLOG_WARN_RL(&rl, "%s field %s lacks correct prerequisites",
1572 type, sf->field->name);
1573 return OFPERR_OFPBAC_MATCH_INCONSISTENT;
1579 /* Checks whether 'sf' is valid for reading a subfield out of 'flow'. Returns
1580 * 0 if so, otherwise an OpenFlow error code (e.g. as returned by
1583 mf_check_src(const struct mf_subfield *sf, const struct flow *flow)
1585 return mf_check__(sf, flow, "source");
1588 /* Checks whether 'sf' is valid for writing a subfield into 'flow'. Returns 0
1589 * if so, otherwise an OpenFlow error code (e.g. as returned by
1592 mf_check_dst(const struct mf_subfield *sf, const struct flow *flow)
1594 int error = mf_check__(sf, flow, "destination");
1595 if (!error && !sf->field->writable) {
1596 VLOG_WARN_RL(&rl, "destination field %s is not writable",
1598 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
1603 /* Copies the value and wildcard bit pattern for 'mf' from 'match' into the
1604 * 'value' and 'mask', respectively. */
1606 mf_get(const struct mf_field *mf, const struct match *match,
1607 union mf_value *value, union mf_value *mask)
1609 mf_get_value(mf, &match->flow, value);
1610 mf_get_mask(mf, &match->wc, mask);
1614 mf_from_integer_string(const struct mf_field *mf, const char *s,
1615 uint8_t *valuep, uint8_t *maskp)
1617 unsigned long long int integer, mask;
1622 integer = strtoull(s, &tail, 0);
1623 if (errno || (*tail != '\0' && *tail != '/')) {
1628 mask = strtoull(tail + 1, &tail, 0);
1629 if (errno || *tail != '\0') {
1636 for (i = mf->n_bytes - 1; i >= 0; i--) {
1637 valuep[i] = integer;
1643 return xasprintf("%s: value too large for %u-byte field %s",
1644 s, mf->n_bytes, mf->name);
1649 return xasprintf("%s: bad syntax for %s", s, mf->name);
1653 mf_from_ethernet_string(const struct mf_field *mf, const char *s,
1654 uint8_t mac[ETH_ADDR_LEN],
1655 uint8_t mask[ETH_ADDR_LEN])
1659 ovs_assert(mf->n_bytes == ETH_ADDR_LEN);
1662 if (ovs_scan(s, ETH_ADDR_SCAN_FMT"%n", ETH_ADDR_SCAN_ARGS(mac), &n)
1663 && n == strlen(s)) {
1664 memset(mask, 0xff, ETH_ADDR_LEN);
1669 if (ovs_scan(s, ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT"%n",
1670 ETH_ADDR_SCAN_ARGS(mac), ETH_ADDR_SCAN_ARGS(mask), &n)
1671 && n == strlen(s)) {
1675 return xasprintf("%s: invalid Ethernet address", s);
1679 mf_from_ipv4_string(const struct mf_field *mf, const char *s,
1680 ovs_be32 *ip, ovs_be32 *mask)
1684 ovs_assert(mf->n_bytes == sizeof *ip);
1686 if (ovs_scan(s, IP_SCAN_FMT"/"IP_SCAN_FMT,
1687 IP_SCAN_ARGS(ip), IP_SCAN_ARGS(mask))) {
1689 } else if (ovs_scan(s, IP_SCAN_FMT"/%d", IP_SCAN_ARGS(ip), &prefix)) {
1690 if (prefix <= 0 || prefix > 32) {
1691 return xasprintf("%s: network prefix bits not between 1 and "
1693 } else if (prefix == 32) {
1694 *mask = OVS_BE32_MAX;
1696 *mask = htonl(((1u << prefix) - 1) << (32 - prefix));
1698 } else if (ovs_scan(s, IP_SCAN_FMT, IP_SCAN_ARGS(ip))) {
1699 *mask = OVS_BE32_MAX;
1701 return xasprintf("%s: invalid IP address", s);
1707 mf_from_ipv6_string(const struct mf_field *mf, const char *s,
1708 struct in6_addr *value, struct in6_addr *mask)
1710 char *str = xstrdup(s);
1711 char *save_ptr = NULL;
1712 const char *name, *netmask;
1715 ovs_assert(mf->n_bytes == sizeof *value);
1717 name = strtok_r(str, "/", &save_ptr);
1718 retval = name ? lookup_ipv6(name, value) : EINVAL;
1722 err = xasprintf("%s: could not convert to IPv6 address", str);
1728 netmask = strtok_r(NULL, "/", &save_ptr);
1730 if (inet_pton(AF_INET6, netmask, mask) != 1) {
1731 int prefix = atoi(netmask);
1732 if (prefix <= 0 || prefix > 128) {
1734 return xasprintf("%s: prefix bits not between 1 and 128", s);
1736 *mask = ipv6_create_mask(prefix);
1740 *mask = in6addr_exact;
1748 mf_from_ofp_port_string(const struct mf_field *mf, const char *s,
1749 ovs_be16 *valuep, ovs_be16 *maskp)
1753 ovs_assert(mf->n_bytes == sizeof(ovs_be16));
1755 if (ofputil_port_from_string(s, &port)) {
1756 *valuep = htons(ofp_to_u16(port));
1757 *maskp = OVS_BE16_MAX;
1760 return xasprintf("%s: port value out of range for %s", s, mf->name);
1764 mf_from_ofp_port_string32(const struct mf_field *mf, const char *s,
1765 ovs_be32 *valuep, ovs_be32 *maskp)
1769 ovs_assert(mf->n_bytes == sizeof(ovs_be32));
1770 if (ofputil_port_from_string(s, &port)) {
1771 *valuep = ofputil_port_to_ofp11(port);
1772 *maskp = OVS_BE32_MAX;
1775 return xasprintf("%s: port value out of range for %s", s, mf->name);
1778 struct frag_handling {
1784 static const struct frag_handling all_frags[] = {
1785 #define A FLOW_NW_FRAG_ANY
1786 #define L FLOW_NW_FRAG_LATER
1787 /* name mask value */
1790 { "first", A|L, A },
1791 { "later", A|L, A|L },
1796 { "not_later", L, 0 },
1803 mf_from_frag_string(const char *s, uint8_t *valuep, uint8_t *maskp)
1805 const struct frag_handling *h;
1807 for (h = all_frags; h < &all_frags[ARRAY_SIZE(all_frags)]; h++) {
1808 if (!strcasecmp(s, h->name)) {
1809 /* We force the upper bits of the mask on to make mf_parse_value()
1810 * happy (otherwise it will never think it's an exact match.) */
1811 *maskp = h->mask | ~FLOW_NW_FRAG_MASK;
1817 return xasprintf("%s: unknown fragment type (valid types are \"no\", "
1818 "\"yes\", \"first\", \"later\", \"not_first\"", s);
1822 parse_flow_tun_flags(const char *s_, const char *(*bit_to_string)(uint32_t),
1825 uint32_t result = 0;
1826 char *save_ptr = NULL;
1829 char *s = xstrdup(s_);
1831 for (name = strtok_r((char *)s, " |", &save_ptr); name;
1832 name = strtok_r(NULL, " |", &save_ptr)) {
1834 unsigned long long int flags;
1837 if (ovs_scan(name, "%lli", &flags)) {
1841 name_len = strlen(name);
1842 for (bit = 1; bit; bit <<= 1) {
1843 const char *fname = bit_to_string(bit);
1850 len = strlen(fname);
1851 if (len != name_len) {
1854 if (!strncmp(name, fname, len)) {
1866 *res = htons(result);
1873 mf_from_tun_flags_string(const char *s, ovs_be16 *valuep, ovs_be16 *maskp)
1875 if (!parse_flow_tun_flags(s, flow_tun_flag_to_string, valuep)) {
1876 *maskp = OVS_BE16_MAX;
1880 return xasprintf("%s: unknown tunnel flags (valid flags are \"df\", "
1881 "\"csum\", \"key\")", s);
1885 mf_from_tcp_flags_string(const char *s, ovs_be16 *flagsp, ovs_be16 *maskp)
1892 if (ovs_scan(s, "%"SCNi16"/%"SCNi16"%n", &flags, &mask, &n) && !s[n]) {
1893 *flagsp = htons(flags);
1894 *maskp = htons(mask);
1897 if (ovs_scan(s, "%"SCNi16"%n", &flags, &n) && !s[n]) {
1898 *flagsp = htons(flags);
1899 *maskp = OVS_BE16_MAX;
1903 while (*s != '\0') {
1915 return xasprintf("%s: TCP flag must be preceded by '+' (for SET) "
1916 "or '-' (NOT SET)", s);
1920 name_len = strcspn(s,"+-");
1922 for (bit = 1; bit; bit <<= 1) {
1923 const char *fname = packet_tcp_flag_to_string(bit);
1930 len = strlen(fname);
1931 if (len != name_len) {
1934 if (!strncmp(s, fname, len)) {
1936 return xasprintf("%s: Each TCP flag can be specified only "
1948 return xasprintf("%s: unknown TCP flag(s)", s);
1953 *flagsp = htons(flags);
1954 *maskp = htons(mask);
1959 /* Parses 's', a string value for field 'mf', into 'value' and 'mask'. Returns
1960 * NULL if successful, otherwise a malloc()'d string describing the error. */
1962 mf_parse(const struct mf_field *mf, const char *s,
1963 union mf_value *value, union mf_value *mask)
1967 if (!strcmp(s, "*")) {
1968 memset(value, 0, mf->n_bytes);
1969 memset(mask, 0, mf->n_bytes);
1973 switch (mf->string) {
1975 case MFS_HEXADECIMAL:
1976 error = mf_from_integer_string(mf, s,
1977 (uint8_t *) value, (uint8_t *) mask);
1981 error = mf_from_ethernet_string(mf, s, value->mac, mask->mac);
1985 error = mf_from_ipv4_string(mf, s, &value->be32, &mask->be32);
1989 error = mf_from_ipv6_string(mf, s, &value->ipv6, &mask->ipv6);
1993 error = mf_from_ofp_port_string(mf, s, &value->be16, &mask->be16);
1996 case MFS_OFP_PORT_OXM:
1997 error = mf_from_ofp_port_string32(mf, s, &value->be32, &mask->be32);
2001 error = mf_from_frag_string(s, &value->u8, &mask->u8);
2005 ovs_assert(mf->n_bytes == sizeof(ovs_be16));
2006 error = mf_from_tun_flags_string(s, &value->be16, &mask->be16);
2010 ovs_assert(mf->n_bytes == sizeof(ovs_be16));
2011 error = mf_from_tcp_flags_string(s, &value->be16, &mask->be16);
2018 if (!error && !mf_is_mask_valid(mf, mask)) {
2019 error = xasprintf("%s: invalid mask for field %s", s, mf->name);
2024 /* Parses 's', a string value for field 'mf', into 'value'. Returns NULL if
2025 * successful, otherwise a malloc()'d string describing the error. */
2027 mf_parse_value(const struct mf_field *mf, const char *s, union mf_value *value)
2029 union mf_value mask;
2032 error = mf_parse(mf, s, value, &mask);
2037 if (!is_all_ones((const uint8_t *) &mask, mf->n_bytes)) {
2038 return xasprintf("%s: wildcards not allowed here", s);
2044 mf_format_integer_string(const struct mf_field *mf, const uint8_t *valuep,
2045 const uint8_t *maskp, struct ds *s)
2047 unsigned long long int integer;
2050 ovs_assert(mf->n_bytes <= 8);
2053 for (i = 0; i < mf->n_bytes; i++) {
2054 integer = (integer << 8) | valuep[i];
2056 if (mf->string == MFS_HEXADECIMAL) {
2057 ds_put_format(s, "%#llx", integer);
2059 ds_put_format(s, "%lld", integer);
2063 unsigned long long int mask;
2066 for (i = 0; i < mf->n_bytes; i++) {
2067 mask = (mask << 8) | maskp[i];
2070 /* I guess we could write the mask in decimal for MFS_DECIMAL but I'm
2071 * not sure that that a bit-mask written in decimal is ever easier to
2072 * understand than the same bit-mask written in hexadecimal. */
2073 ds_put_format(s, "/%#llx", mask);
2078 mf_format_frag_string(uint8_t value, uint8_t mask, struct ds *s)
2080 const struct frag_handling *h;
2082 mask &= FLOW_NW_FRAG_MASK;
2085 for (h = all_frags; h < &all_frags[ARRAY_SIZE(all_frags)]; h++) {
2086 if (value == h->value && mask == h->mask) {
2087 ds_put_cstr(s, h->name);
2091 ds_put_cstr(s, "<error>");
2095 mf_format_tnl_flags_string(const ovs_be16 *valuep, struct ds *s)
2097 format_flags(s, flow_tun_flag_to_string, ntohs(*valuep), '|');
2101 mf_format_tcp_flags_string(ovs_be16 value, ovs_be16 mask, struct ds *s)
2103 format_flags_masked(s, NULL, packet_tcp_flag_to_string, ntohs(value),
2107 /* Appends to 's' a string representation of field 'mf' whose value is in
2108 * 'value' and 'mask'. 'mask' may be NULL to indicate an exact match. */
2110 mf_format(const struct mf_field *mf,
2111 const union mf_value *value, const union mf_value *mask,
2115 if (is_all_zeros(mask, mf->n_bytes)) {
2116 ds_put_cstr(s, "ANY");
2118 } else if (is_all_ones(mask, mf->n_bytes)) {
2123 switch (mf->string) {
2124 case MFS_OFP_PORT_OXM:
2127 ofputil_port_from_ofp11(value->be32, &port);
2128 ofputil_format_port(port, s);
2134 ofputil_format_port(u16_to_ofp(ntohs(value->be16)), s);
2139 case MFS_HEXADECIMAL:
2140 mf_format_integer_string(mf, (uint8_t *) value, (uint8_t *) mask, s);
2144 eth_format_masked(value->mac, mask->mac, s);
2148 ip_format_masked(value->be32, mask ? mask->be32 : OVS_BE32_MAX, s);
2152 print_ipv6_masked(s, &value->ipv6, mask ? &mask->ipv6 : NULL);
2156 mf_format_frag_string(value->u8, mask ? mask->u8 : UINT8_MAX, s);
2160 mf_format_tnl_flags_string(&value->be16, s);
2164 mf_format_tcp_flags_string(value->be16,
2165 mask ? mask->be16 : OVS_BE16_MAX, s);
2173 /* Makes subfield 'sf' within 'flow' exactly match the 'sf->n_bits'
2174 * least-significant bits in 'x'.
2177 mf_write_subfield_flow(const struct mf_subfield *sf,
2178 const union mf_subvalue *x, struct flow *flow)
2180 const struct mf_field *field = sf->field;
2181 union mf_value value;
2183 mf_get_value(field, flow, &value);
2184 bitwise_copy(x, sizeof *x, 0, &value, field->n_bytes,
2185 sf->ofs, sf->n_bits);
2186 mf_set_flow_value(field, &value, flow);
2189 /* Makes subfield 'sf' within 'match' exactly match the 'sf->n_bits'
2190 * least-significant bits in 'x'.
2193 mf_write_subfield(const struct mf_subfield *sf, const union mf_subvalue *x,
2194 struct match *match)
2196 const struct mf_field *field = sf->field;
2197 union mf_value value, mask;
2199 mf_get(field, match, &value, &mask);
2200 bitwise_copy(x, sizeof *x, 0, &value, field->n_bytes, sf->ofs, sf->n_bits);
2201 bitwise_one ( &mask, field->n_bytes, sf->ofs, sf->n_bits);
2202 mf_set(field, &value, &mask, match);
2205 /* Initializes 'x' to the value of 'sf' within 'flow'. 'sf' must be valid for
2206 * reading 'flow', e.g. as checked by mf_check_src(). */
2208 mf_read_subfield(const struct mf_subfield *sf, const struct flow *flow,
2209 union mf_subvalue *x)
2211 union mf_value value;
2213 mf_get_value(sf->field, flow, &value);
2215 memset(x, 0, sizeof *x);
2216 bitwise_copy(&value, sf->field->n_bytes, sf->ofs,
2221 /* Returns the value of 'sf' within 'flow'. 'sf' must be valid for reading
2222 * 'flow', e.g. as checked by mf_check_src() and sf->n_bits must be 64 or
2225 mf_get_subfield(const struct mf_subfield *sf, const struct flow *flow)
2227 union mf_value value;
2229 mf_get_value(sf->field, flow, &value);
2230 return bitwise_get(&value, sf->field->n_bytes, sf->ofs, sf->n_bits);
2233 /* Formats 'sf' into 's' in a format normally acceptable to
2234 * mf_parse_subfield(). (It won't be acceptable if sf->field is NULL or if
2235 * sf->field has no NXM name.) */
2237 mf_format_subfield(const struct mf_subfield *sf, struct ds *s)
2240 ds_put_cstr(s, "<unknown>");
2241 } else if (sf->field->nxm_name) {
2242 ds_put_cstr(s, sf->field->nxm_name);
2243 } else if (sf->field->nxm_header) {
2244 uint32_t header = sf->field->nxm_header;
2245 ds_put_format(s, "%d:%d", NXM_VENDOR(header), NXM_FIELD(header));
2247 ds_put_cstr(s, sf->field->name);
2250 if (sf->field && sf->ofs == 0 && sf->n_bits == sf->field->n_bits) {
2251 ds_put_cstr(s, "[]");
2252 } else if (sf->n_bits == 1) {
2253 ds_put_format(s, "[%d]", sf->ofs);
2255 ds_put_format(s, "[%d..%d]", sf->ofs, sf->ofs + sf->n_bits - 1);
2259 static const struct mf_field *
2260 mf_parse_subfield_name(const char *name, int name_len, bool *wild)
2264 *wild = name_len > 2 && !memcmp(&name[name_len - 2], "_W", 2);
2269 for (i = 0; i < MFF_N_IDS; i++) {
2270 const struct mf_field *mf = mf_from_id(i);
2273 && !strncmp(mf->nxm_name, name, name_len)
2274 && mf->nxm_name[name_len] == '\0') {
2278 && !strncmp(mf->oxm_name, name, name_len)
2279 && mf->oxm_name[name_len] == '\0') {
2287 /* Parses a subfield from the beginning of '*sp' into 'sf'. If successful,
2288 * returns NULL and advances '*sp' to the first byte following the parsed
2289 * string. On failure, returns a malloc()'d error message, does not modify
2290 * '*sp', and does not properly initialize 'sf'.
2292 * The syntax parsed from '*sp' takes the form "header[start..end]" where
2293 * 'header' is the name of an NXM field and 'start' and 'end' are (inclusive)
2294 * bit indexes. "..end" may be omitted to indicate a single bit. "start..end"
2295 * may both be omitted (the [] are still required) to indicate an entire
2297 char * WARN_UNUSED_RESULT
2298 mf_parse_subfield__(struct mf_subfield *sf, const char **sp)
2300 const struct mf_field *field;
2309 name_len = strcspn(s, "[");
2310 if (s[name_len] != '[') {
2311 return xasprintf("%s: missing [ looking for field name", *sp);
2314 field = mf_parse_subfield_name(name, name_len, &wild);
2316 return xasprintf("%s: unknown field `%.*s'", *sp, name_len, s);
2320 if (ovs_scan(s, "[%d..%d]", &start, &end)) {
2321 /* Nothing to do. */
2322 } else if (ovs_scan(s, "[%d]", &start)) {
2324 } else if (!strncmp(s, "[]", 2)) {
2326 end = field->n_bits - 1;
2328 return xasprintf("%s: syntax error expecting [] or [<bit>] or "
2329 "[<start>..<end>]", *sp);
2331 s = strchr(s, ']') + 1;
2334 return xasprintf("%s: starting bit %d is after ending bit %d",
2336 } else if (start >= field->n_bits) {
2337 return xasprintf("%s: starting bit %d is not valid because field is "
2338 "only %d bits wide", *sp, start, field->n_bits);
2339 } else if (end >= field->n_bits){
2340 return xasprintf("%s: ending bit %d is not valid because field is "
2341 "only %d bits wide", *sp, end, field->n_bits);
2346 sf->n_bits = end - start + 1;
2352 /* Parses a subfield from the entirety of 's' into 'sf'. Returns NULL if
2353 * successful, otherwise a malloc()'d string describing the error. The caller
2354 * is responsible for freeing the returned string.
2356 * The syntax parsed from 's' takes the form "header[start..end]" where
2357 * 'header' is the name of an NXM field and 'start' and 'end' are (inclusive)
2358 * bit indexes. "..end" may be omitted to indicate a single bit. "start..end"
2359 * may both be omitted (the [] are still required) to indicate an entire
2361 char * WARN_UNUSED_RESULT
2362 mf_parse_subfield(struct mf_subfield *sf, const char *s)
2364 char *error = mf_parse_subfield__(sf, &s);
2365 if (!error && s[0]) {
2366 error = xstrdup("unexpected input following field syntax");
2372 mf_format_subvalue(const union mf_subvalue *subvalue, struct ds *s)
2376 for (i = 0; i < ARRAY_SIZE(subvalue->u8); i++) {
2377 if (subvalue->u8[i]) {
2378 ds_put_format(s, "0x%"PRIx8, subvalue->u8[i]);
2379 for (i++; i < ARRAY_SIZE(subvalue->u8); i++) {
2380 ds_put_format(s, "%02"PRIx8, subvalue->u8[i]);
2385 ds_put_char(s, '0');