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] = {
55 MFF_DP_HASH, "dp_hash", NULL,
61 NXM_NX_DP_HASH, "NXM_NX_DP_HASH",
62 NXM_NX_DP_HASH, "NXM_NX_DP_HASH", 0,
63 OFPUTIL_P_NXM_OXM_ANY,
64 OFPUTIL_P_NXM_OXM_ANY,
67 MFF_RECIRC_ID, "recirc_id", NULL,
73 NXM_NX_RECIRC_ID, "NXM_NX_RECIRC_ID",
74 NXM_NX_RECIRC_ID, "NXM_NX_RECIRC_ID", 0,
75 OFPUTIL_P_NXM_OXM_ANY,
76 OFPUTIL_P_NXM_OXM_ANY,
79 MFF_TUN_ID, "tun_id", "tunnel_id",
85 NXM_NX_TUN_ID, "NXM_NX_TUN_ID",
86 OXM_OF_TUNNEL_ID, "OXM_OF_TUNNEL_ID", OFP13_VERSION,
87 OFPUTIL_P_NXM_OXM_ANY,
88 OFPUTIL_P_NXM_OXM_ANY,
89 FLOW_U32OFS(tunnel.tun_id),
91 MFF_TUN_SRC, "tun_src", NULL,
97 NXM_NX_TUN_IPV4_SRC, "NXM_NX_TUN_IPV4_SRC",
98 NXM_NX_TUN_IPV4_SRC, "NXM_NX_TUN_IPV4_SRC", 0,
99 OFPUTIL_P_NXM_OXM_ANY,
100 OFPUTIL_P_NXM_OXM_ANY,
101 FLOW_U32OFS(tunnel.ip_src),
103 MFF_TUN_DST, "tun_dst", NULL,
104 MF_FIELD_SIZES(be32),
109 NXM_NX_TUN_IPV4_DST, "NXM_NX_TUN_IPV4_DST",
110 NXM_NX_TUN_IPV4_DST, "NXM_NX_TUN_IPV4_DST", 0,
111 OFPUTIL_P_NXM_OXM_ANY,
112 OFPUTIL_P_NXM_OXM_ANY,
113 FLOW_U32OFS(tunnel.ip_dst),
115 MFF_TUN_FLAGS, "tun_flags", NULL,
116 MF_FIELD_SIZES(be16),
127 MFF_TUN_TTL, "tun_ttl", NULL,
139 MFF_TUN_TOS, "tun_tos", NULL,
151 MFF_METADATA, "metadata", NULL,
152 MF_FIELD_SIZES(be64),
157 OXM_OF_METADATA, "OXM_OF_METADATA",
158 OXM_OF_METADATA, "OXM_OF_METADATA", OFP12_VERSION,
159 OFPUTIL_P_NXM_OF11_UP,
160 OFPUTIL_P_NXM_OF11_UP,
163 MFF_IN_PORT, "in_port", NULL,
164 MF_FIELD_SIZES(be16),
169 NXM_OF_IN_PORT, "NXM_OF_IN_PORT",
170 NXM_OF_IN_PORT, "NXM_OF_IN_PORT", 0,
171 OFPUTIL_P_ANY, /* OF11+ via mapping to 32 bits. */
175 MFF_IN_PORT_OXM, "in_port_oxm", NULL,
176 MF_FIELD_SIZES(be32),
181 OXM_OF_IN_PORT, "OXM_OF_IN_PORT",
182 OXM_OF_IN_PORT, "OXM_OF_IN_PORT", OFP12_VERSION,
187 MFF_SKB_PRIORITY, "skb_priority", NULL,
188 MF_FIELD_SIZES(be32),
199 MFF_PKT_MARK, "pkt_mark", NULL,
200 MF_FIELD_SIZES(be32),
205 NXM_NX_PKT_MARK, "NXM_NX_PKT_MARK",
206 NXM_NX_PKT_MARK, "NXM_NX_PKT_MARK", 0,
207 OFPUTIL_P_NXM_OXM_ANY,
208 OFPUTIL_P_NXM_OXM_ANY,
212 #define REGISTER(IDX) \
214 MFF_REG##IDX, "reg" #IDX, NULL, \
215 MF_FIELD_SIZES(be32), \
220 NXM_NX_REG(IDX), "NXM_NX_REG" #IDX, \
221 NXM_NX_REG(IDX), "NXM_NX_REG" #IDX, 0, \
222 OFPUTIL_P_NXM_OXM_ANY, \
223 OFPUTIL_P_NXM_OXM_ANY, \
236 #error "Need to update mf_fields[] to match FLOW_N_REGS"
239 #define XREGISTER(IDX) \
241 MFF_XREG##IDX, "xreg" #IDX, NULL, \
242 MF_FIELD_SIZES(be64), \
247 OXM_OF_PKT_REG(IDX), "OXM_OF_PKT_REG" #IDX, \
248 OXM_OF_PKT_REG(IDX), "OXM_OF_PKT_REG" #IDX, OFP15_VERSION, \
249 OFPUTIL_P_NXM_OXM_ANY, \
250 OFPUTIL_P_NXM_OXM_ANY, \
253 #if FLOW_N_XREGS == 4
259 #error "Need to update mf_fields[] to match FLOW_N_XREGS"
267 MFF_ETH_SRC, "eth_src", "dl_src",
273 NXM_OF_ETH_SRC, "NXM_OF_ETH_SRC",
274 OXM_OF_ETH_SRC, "OXM_OF_ETH_SRC", OFP12_VERSION,
276 OFPUTIL_P_NXM_OF11_UP, /* Bitwise masking only with NXM and OF11+! */
279 MFF_ETH_DST, "eth_dst", "dl_dst",
285 NXM_OF_ETH_DST, "NXM_OF_ETH_DST",
286 OXM_OF_ETH_DST, "OXM_OF_ETH_DST", OFP12_VERSION,
288 OFPUTIL_P_NXM_OF11_UP, /* Bitwise masking only with NXM and OF11+! */
291 MFF_ETH_TYPE, "eth_type", "dl_type",
292 MF_FIELD_SIZES(be16),
297 NXM_OF_ETH_TYPE, "NXM_OF_ETH_TYPE",
298 OXM_OF_ETH_TYPE, "OXM_OF_ETH_TYPE", OFP12_VERSION,
305 MFF_VLAN_TCI, "vlan_tci", NULL,
306 MF_FIELD_SIZES(be16),
311 NXM_OF_VLAN_TCI, "NXM_OF_VLAN_TCI",
312 NXM_OF_VLAN_TCI, "NXM_OF_VLAN_TCI", 0,
314 OFPUTIL_P_NXM_OXM_ANY,
317 MFF_DL_VLAN, "dl_vlan", NULL,
318 sizeof(ovs_be16), 12,
326 OFPUTIL_P_NXM_OXM_ANY,
329 MFF_VLAN_VID, "vlan_vid", NULL,
330 sizeof(ovs_be16), 12,
335 OXM_OF_VLAN_VID, "OXM_OF_VLAN_VID",
336 OXM_OF_VLAN_VID, "OXM_OF_VLAN_VID", OFP12_VERSION,
338 OFPUTIL_P_NXM_OXM_ANY,
341 MFF_DL_VLAN_PCP, "dl_vlan_pcp", NULL,
349 OFPUTIL_P_ANY, /* Will be mapped to NXM and OXM. */
353 MFF_VLAN_PCP, "vlan_pcp", NULL,
359 OXM_OF_VLAN_PCP, "OXM_OF_VLAN_PCP",
360 OXM_OF_VLAN_PCP, "OXM_OF_VLAN_PCP", OFP12_VERSION,
361 OFPUTIL_P_ANY, /* Will be mapped to OF10 and NXM. */
370 MFF_MPLS_LABEL, "mpls_label", NULL,
376 OXM_OF_MPLS_LABEL, "OXM_OF_MPLS_LABEL",
377 OXM_OF_MPLS_LABEL, "OXM_OF_MPLS_LABEL", OFP12_VERSION,
378 OFPUTIL_P_NXM_OF11_UP,
382 MFF_MPLS_TC, "mpls_tc", NULL,
388 OXM_OF_MPLS_TC, "OXM_OF_MPLS_TC",
389 OXM_OF_MPLS_TC, "OXM_OF_MPLS_TC", OFP12_VERSION,
390 OFPUTIL_P_NXM_OF11_UP,
394 MFF_MPLS_BOS, "mpls_bos", NULL,
400 OXM_OF_MPLS_BOS, "OXM_OF_MPLS_BOS",
401 OXM_OF_MPLS_BOS, "OXM_OF_MPLS_BOS", OFP13_VERSION,
402 OFPUTIL_P_NXM_OXM_ANY,
412 MFF_IPV4_SRC, "ip_src", "nw_src",
413 MF_FIELD_SIZES(be32),
418 NXM_OF_IP_SRC, "NXM_OF_IP_SRC",
419 OXM_OF_IPV4_SRC, "OXM_OF_IPV4_SRC", OFP12_VERSION,
421 OFPUTIL_P_NXM_OF11_UP,
424 MFF_IPV4_DST, "ip_dst", "nw_dst",
425 MF_FIELD_SIZES(be32),
430 NXM_OF_IP_DST, "NXM_OF_IP_DST",
431 OXM_OF_IPV4_DST, "OXM_OF_IPV4_DST", OFP12_VERSION,
433 OFPUTIL_P_NXM_OF11_UP,
438 MFF_IPV6_SRC, "ipv6_src", NULL,
439 MF_FIELD_SIZES(ipv6),
444 NXM_NX_IPV6_SRC, "NXM_NX_IPV6_SRC",
445 OXM_OF_IPV6_SRC, "OXM_OF_IPV6_SRC", OFP12_VERSION,
446 OFPUTIL_P_NXM_OXM_ANY,
447 OFPUTIL_P_NXM_OXM_ANY,
448 FLOW_U32OFS(ipv6_src),
450 MFF_IPV6_DST, "ipv6_dst", NULL,
451 MF_FIELD_SIZES(ipv6),
456 NXM_NX_IPV6_DST, "NXM_NX_IPV6_DST",
457 OXM_OF_IPV6_DST, "OXM_OF_IPV6_DST", OFP12_VERSION,
458 OFPUTIL_P_NXM_OXM_ANY,
459 OFPUTIL_P_NXM_OXM_ANY,
460 FLOW_U32OFS(ipv6_dst),
463 MFF_IPV6_LABEL, "ipv6_label", NULL,
469 NXM_NX_IPV6_LABEL, "NXM_NX_IPV6_LABEL",
470 OXM_OF_IPV6_FLABEL, "OXM_OF_IPV6_FLABEL", OFP12_VERSION,
471 OFPUTIL_P_NXM_OXM_ANY,
472 OFPUTIL_P_NXM_OXM_ANY,
477 MFF_IP_PROTO, "nw_proto", "ip_proto",
483 NXM_OF_IP_PROTO, "NXM_OF_IP_PROTO",
484 OXM_OF_IP_PROTO, "OXM_OF_IP_PROTO", OFP12_VERSION,
489 MFF_IP_DSCP, "nw_tos", NULL,
495 NXM_OF_IP_TOS, "NXM_OF_IP_TOS",
496 NXM_OF_IP_TOS, "NXM_OF_IP_TOS", 0,
497 OFPUTIL_P_ANY, /* Will be shifted for OXM. */
501 MFF_IP_DSCP_SHIFTED, "ip_dscp", NULL,
507 OXM_OF_IP_DSCP, "OXM_OF_IP_DSCP",
508 OXM_OF_IP_DSCP, "OXM_OF_IP_DSCP", OFP12_VERSION,
509 OFPUTIL_P_ANY, /* Will be shifted for non-OXM. */
513 MFF_IP_ECN, "nw_ecn", "ip_ecn",
519 NXM_NX_IP_ECN, "NXM_NX_IP_ECN",
520 OXM_OF_IP_ECN, "OXM_OF_IP_ECN", OFP12_VERSION,
521 OFPUTIL_P_NXM_OXM_ANY,
525 MFF_IP_TTL, "nw_ttl", NULL,
531 NXM_NX_IP_TTL, "NXM_NX_IP_TTL",
532 NXM_NX_IP_TTL, "NXM_NX_IP_TTL", 0,
533 OFPUTIL_P_NXM_OXM_ANY,
537 MFF_IP_FRAG, "ip_frag", NULL,
543 NXM_NX_IP_FRAG, "NXM_NX_IP_FRAG",
544 NXM_NX_IP_FRAG, "NXM_NX_IP_FRAG", 0,
545 OFPUTIL_P_NXM_OXM_ANY,
546 OFPUTIL_P_NXM_OXM_ANY,
551 MFF_ARP_OP, "arp_op", NULL,
552 MF_FIELD_SIZES(be16),
557 NXM_OF_ARP_OP, "NXM_OF_ARP_OP",
558 OXM_OF_ARP_OP, "OXM_OF_ARP_OP", OFP12_VERSION,
563 MFF_ARP_SPA, "arp_spa", NULL,
564 MF_FIELD_SIZES(be32),
569 NXM_OF_ARP_SPA, "NXM_OF_ARP_SPA",
570 OXM_OF_ARP_SPA, "OXM_OF_ARP_SPA", OFP12_VERSION,
572 OFPUTIL_P_NXM_OF11_UP,
575 MFF_ARP_TPA, "arp_tpa", NULL,
576 MF_FIELD_SIZES(be32),
581 NXM_OF_ARP_TPA, "NXM_OF_ARP_TPA",
582 OXM_OF_ARP_TPA, "OXM_OF_ARP_TPA", OFP12_VERSION,
584 OFPUTIL_P_NXM_OF11_UP,
587 MFF_ARP_SHA, "arp_sha", NULL,
593 NXM_NX_ARP_SHA, "NXM_NX_ARP_SHA",
594 OXM_OF_ARP_SHA, "OXM_OF_ARP_SHA", OFP12_VERSION,
595 OFPUTIL_P_NXM_OXM_ANY,
596 OFPUTIL_P_NXM_OXM_ANY,
599 MFF_ARP_THA, "arp_tha", NULL,
605 NXM_NX_ARP_THA, "NXM_NX_ARP_THA",
606 OXM_OF_ARP_THA, "OXM_OF_ARP_THA", OFP12_VERSION,
607 OFPUTIL_P_NXM_OXM_ANY,
608 OFPUTIL_P_NXM_OXM_ANY,
617 MFF_TCP_SRC, "tcp_src", "tp_src",
618 MF_FIELD_SIZES(be16),
623 NXM_OF_TCP_SRC, "NXM_OF_TCP_SRC",
624 OXM_OF_TCP_SRC, "OXM_OF_TCP_SRC", OFP12_VERSION,
626 OFPUTIL_P_NXM_OXM_ANY,
629 MFF_TCP_DST, "tcp_dst", "tp_dst",
630 MF_FIELD_SIZES(be16),
635 NXM_OF_TCP_DST, "NXM_OF_TCP_DST",
636 OXM_OF_TCP_DST, "OXM_OF_TCP_DST", OFP12_VERSION,
638 OFPUTIL_P_NXM_OXM_ANY,
641 MFF_TCP_FLAGS, "tcp_flags", NULL,
647 NXM_NX_TCP_FLAGS, "NXM_NX_TCP_FLAGS",
648 OXM_OF_TCP_FLAGS, "OXM_OF_TCP_FLAGS", OFP15_VERSION,
649 OFPUTIL_P_NXM_OXM_ANY,
650 OFPUTIL_P_NXM_OXM_ANY,
655 MFF_UDP_SRC, "udp_src", NULL,
656 MF_FIELD_SIZES(be16),
661 NXM_OF_UDP_SRC, "NXM_OF_UDP_SRC",
662 OXM_OF_UDP_SRC, "OXM_OF_UDP_SRC", OFP12_VERSION,
664 OFPUTIL_P_NXM_OXM_ANY,
667 MFF_UDP_DST, "udp_dst", NULL,
668 MF_FIELD_SIZES(be16),
673 NXM_OF_UDP_DST, "NXM_OF_UDP_DST",
674 OXM_OF_UDP_DST, "OXM_OF_UDP_DST", OFP12_VERSION,
676 OFPUTIL_P_NXM_OXM_ANY,
681 MFF_SCTP_SRC, "sctp_src", NULL,
682 MF_FIELD_SIZES(be16),
687 OXM_OF_SCTP_SRC, "OXM_OF_SCTP_SRC",
688 OXM_OF_SCTP_SRC, "OXM_OF_SCTP_SRC", OFP12_VERSION,
689 OFPUTIL_P_NXM_OF11_UP,
690 OFPUTIL_P_NXM_OXM_ANY,
693 MFF_SCTP_DST, "sctp_dst", NULL,
694 MF_FIELD_SIZES(be16),
699 OXM_OF_SCTP_DST, "OXM_OF_SCTP_DST",
700 OXM_OF_SCTP_DST, "OXM_OF_SCTP_DST", OFP12_VERSION,
701 OFPUTIL_P_NXM_OF11_UP,
702 OFPUTIL_P_NXM_OXM_ANY,
707 MFF_ICMPV4_TYPE, "icmp_type", NULL,
713 NXM_OF_ICMP_TYPE, "NXM_OF_ICMP_TYPE",
714 OXM_OF_ICMPV4_TYPE, "OXM_OF_ICMPV4_TYPE", OFP12_VERSION,
719 MFF_ICMPV4_CODE, "icmp_code", NULL,
725 NXM_OF_ICMP_CODE, "NXM_OF_ICMP_CODE",
726 OXM_OF_ICMPV4_CODE, "OXM_OF_ICMPV4_CODE", OFP12_VERSION,
733 MFF_ICMPV6_TYPE, "icmpv6_type", NULL,
739 NXM_NX_ICMPV6_TYPE, "NXM_NX_ICMPV6_TYPE",
740 OXM_OF_ICMPV6_TYPE, "OXM_OF_ICMPV6_TYPE", OFP12_VERSION,
741 OFPUTIL_P_NXM_OXM_ANY,
745 MFF_ICMPV6_CODE, "icmpv6_code", NULL,
751 NXM_NX_ICMPV6_CODE, "NXM_NX_ICMPV6_CODE",
752 OXM_OF_ICMPV6_CODE, "OXM_OF_ICMPV6_CODE", OFP12_VERSION,
753 OFPUTIL_P_NXM_OXM_ANY,
763 MFF_ND_TARGET, "nd_target", NULL,
764 MF_FIELD_SIZES(ipv6),
769 NXM_NX_ND_TARGET, "NXM_NX_ND_TARGET",
770 OXM_OF_IPV6_ND_TARGET, "OXM_OF_IPV6_ND_TARGET", OFP12_VERSION,
771 OFPUTIL_P_NXM_OXM_ANY,
772 OFPUTIL_P_NXM_OXM_ANY,
775 MFF_ND_SLL, "nd_sll", NULL,
781 NXM_NX_ND_SLL, "NXM_NX_ND_SLL",
782 OXM_OF_IPV6_ND_SLL, "OXM_OF_IPV6_ND_SLL", OFP12_VERSION,
783 OFPUTIL_P_NXM_OXM_ANY,
784 OFPUTIL_P_NXM_OXM_ANY,
787 MFF_ND_TLL, "nd_tll", NULL,
793 NXM_NX_ND_TLL, "NXM_NX_ND_TLL",
794 OXM_OF_IPV6_ND_TLL, "OXM_OF_IPV6_ND_TLL", OFP12_VERSION,
795 OFPUTIL_P_NXM_OXM_ANY,
796 OFPUTIL_P_NXM_OXM_ANY,
801 /* Maps an NXM or OXM header value to an mf_field. */
803 struct hmap_node hmap_node; /* In 'all_fields' hmap. */
804 uint32_t header; /* NXM or OXM header value. */
805 const struct mf_field *mf;
808 /* Contains 'struct nxm_field's. */
809 static struct hmap all_fields;
811 /* Maps from an mf_field's 'name' or 'extra_name' to the mf_field. */
812 static struct shash mf_by_name;
814 /* Rate limit for parse errors. These always indicate a bug in an OpenFlow
815 * controller and so there's not much point in showing a lot of them. */
816 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
818 const struct mf_field *mf_from_nxm_header__(uint32_t header);
819 static void nxm_init(void);
821 /* Returns the field with the given 'name', or a null pointer if no field has
823 const struct mf_field *
824 mf_from_name(const char *name)
827 return shash_find_data(&mf_by_name, name);
831 add_nxm_field(uint32_t header, const struct mf_field *mf)
835 f = xmalloc(sizeof *f);
836 hmap_insert(&all_fields, &f->hmap_node, hash_int(header, 0));
842 nxm_init_add_field(const struct mf_field *mf, uint32_t header)
845 ovs_assert(!mf_from_nxm_header__(header));
846 add_nxm_field(header, mf);
847 if (mf->maskable != MFM_NONE) {
848 add_nxm_field(NXM_MAKE_WILD_HEADER(header), mf);
858 hmap_init(&all_fields);
859 shash_init(&mf_by_name);
860 for (i = 0; i < MFF_N_IDS; i++) {
861 const struct mf_field *mf = &mf_fields[i];
863 ovs_assert(mf->id == i); /* Fields must be in the enum order. */
865 nxm_init_add_field(mf, mf->nxm_header);
866 if (mf->oxm_header != mf->nxm_header) {
867 nxm_init_add_field(mf, mf->oxm_header);
870 shash_add_once(&mf_by_name, mf->name, mf);
871 if (mf->extra_name) {
872 shash_add_once(&mf_by_name, mf->extra_name, mf);
880 static pthread_once_t once = PTHREAD_ONCE_INIT;
881 pthread_once(&once, nxm_do_init);
884 const struct mf_field *
885 mf_from_nxm_header(uint32_t header)
888 return mf_from_nxm_header__(header);
891 const struct mf_field *
892 mf_from_nxm_header__(uint32_t header)
894 const struct nxm_field *f;
896 HMAP_FOR_EACH_IN_BUCKET (f, hmap_node, hash_int(header, 0), &all_fields) {
897 if (f->header == header) {
906 mf_oxm_header(enum mf_field_id id, enum ofp_version oxm_version)
908 const struct mf_field *field = mf_from_id(id);
910 return (oxm_version >= field->oxm_version
912 : field->nxm_header);
915 /* Returns true if 'wc' wildcards all the bits in field 'mf', false if 'wc'
916 * specifies at least one bit in the field.
918 * The caller is responsible for ensuring that 'wc' corresponds to a flow that
919 * meets 'mf''s prerequisites. */
921 mf_is_all_wild(const struct mf_field *mf, const struct flow_wildcards *wc)
925 return !wc->masks.dp_hash;
927 return !wc->masks.recirc_id;
929 return !wc->masks.tunnel.ip_src;
931 return !wc->masks.tunnel.ip_dst;
936 return !wc->masks.tunnel.tun_id;
938 return !wc->masks.metadata;
940 case MFF_IN_PORT_OXM:
941 return !wc->masks.in_port.ofp_port;
942 case MFF_SKB_PRIORITY:
943 return !wc->masks.skb_priority;
945 return !wc->masks.pkt_mark;
947 return !wc->masks.regs[mf->id - MFF_REG0];
949 return !flow_get_xreg(&wc->masks, mf->id - MFF_XREG0);
952 return eth_addr_is_zero(wc->masks.dl_src);
954 return eth_addr_is_zero(wc->masks.dl_dst);
956 return !wc->masks.dl_type;
960 return eth_addr_is_zero(wc->masks.arp_sha);
964 return eth_addr_is_zero(wc->masks.arp_tha);
967 return !wc->masks.vlan_tci;
969 return !(wc->masks.vlan_tci & htons(VLAN_VID_MASK));
971 return !(wc->masks.vlan_tci & htons(VLAN_VID_MASK | VLAN_CFI));
972 case MFF_DL_VLAN_PCP:
974 return !(wc->masks.vlan_tci & htons(VLAN_PCP_MASK));
977 return !(wc->masks.mpls_lse[0] & htonl(MPLS_LABEL_MASK));
979 return !(wc->masks.mpls_lse[0] & htonl(MPLS_TC_MASK));
981 return !(wc->masks.mpls_lse[0] & htonl(MPLS_BOS_MASK));
984 return !wc->masks.nw_src;
986 return !wc->masks.nw_dst;
989 return ipv6_mask_is_any(&wc->masks.ipv6_src);
991 return ipv6_mask_is_any(&wc->masks.ipv6_dst);
994 return !wc->masks.ipv6_label;
997 return !wc->masks.nw_proto;
999 case MFF_IP_DSCP_SHIFTED:
1000 return !(wc->masks.nw_tos & IP_DSCP_MASK);
1002 return !(wc->masks.nw_tos & IP_ECN_MASK);
1004 return !wc->masks.nw_ttl;
1007 return ipv6_mask_is_any(&wc->masks.nd_target);
1010 return !(wc->masks.nw_frag & FLOW_NW_FRAG_MASK);
1013 return !wc->masks.nw_proto;
1015 return !wc->masks.nw_src;
1017 return !wc->masks.nw_dst;
1022 case MFF_ICMPV4_TYPE:
1023 case MFF_ICMPV6_TYPE:
1024 return !wc->masks.tp_src;
1028 case MFF_ICMPV4_CODE:
1029 case MFF_ICMPV6_CODE:
1030 return !wc->masks.tp_dst;
1032 return !wc->masks.tcp_flags;
1040 /* Initializes 'mask' with the wildcard bit pattern for field 'mf' within 'wc'.
1041 * Each bit in 'mask' will be set to 1 if the bit is significant for matching
1042 * purposes, or to 0 if it is wildcarded.
1044 * The caller is responsible for ensuring that 'wc' corresponds to a flow that
1045 * meets 'mf''s prerequisites. */
1047 mf_get_mask(const struct mf_field *mf, const struct flow_wildcards *wc,
1048 union mf_value *mask)
1050 mf_get_value(mf, &wc->masks, mask);
1053 /* Tests whether 'mask' is a valid wildcard bit pattern for 'mf'. Returns true
1054 * if the mask is valid, false otherwise. */
1056 mf_is_mask_valid(const struct mf_field *mf, const union mf_value *mask)
1058 switch (mf->maskable) {
1060 return (is_all_zeros(mask, mf->n_bytes) ||
1061 is_all_ones(mask, mf->n_bytes));
1070 /* Returns true if 'flow' meets the prerequisites for 'mf', false otherwise. */
1072 mf_are_prereqs_ok(const struct mf_field *mf, const struct flow *flow)
1074 switch (mf->prereqs) {
1079 return (flow->dl_type == htons(ETH_TYPE_ARP) ||
1080 flow->dl_type == htons(ETH_TYPE_RARP));
1082 return flow->dl_type == htons(ETH_TYPE_IP);
1084 return flow->dl_type == htons(ETH_TYPE_IPV6);
1086 return (flow->vlan_tci & htons(VLAN_CFI)) != 0;
1088 return eth_type_mpls(flow->dl_type);
1090 return is_ip_any(flow);
1093 return is_ip_any(flow) && flow->nw_proto == IPPROTO_TCP;
1095 return is_ip_any(flow) && flow->nw_proto == IPPROTO_UDP;
1097 return is_ip_any(flow) && flow->nw_proto == IPPROTO_SCTP;
1099 return is_icmpv4(flow);
1101 return is_icmpv6(flow);
1104 return (is_icmpv6(flow)
1105 && flow->tp_dst == htons(0)
1106 && (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT) ||
1107 flow->tp_src == htons(ND_NEIGHBOR_ADVERT)));
1108 case MFP_ND_SOLICIT:
1109 return (is_icmpv6(flow)
1110 && flow->tp_dst == htons(0)
1111 && (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT)));
1113 return (is_icmpv6(flow)
1114 && flow->tp_dst == htons(0)
1115 && (flow->tp_src == htons(ND_NEIGHBOR_ADVERT)));
1121 /* Set field and it's prerequisities in the mask.
1122 * This is only ever called for writeable 'mf's, but we do not make the
1123 * distinction here. */
1125 mf_mask_field_and_prereqs(const struct mf_field *mf, struct flow *mask)
1127 static const union mf_value exact_match_mask = MF_EXACT_MASK_INITIALIZER;
1129 mf_set_flow_value(mf, &exact_match_mask, mask);
1131 switch (mf->prereqs) {
1133 case MFP_ND_SOLICIT:
1135 mask->tp_src = OVS_BE16_MAX;
1136 mask->tp_dst = OVS_BE16_MAX;
1143 mask->nw_proto = 0xff;
1150 mask->dl_type = OVS_BE16_MAX;
1153 mask->vlan_tci |= htons(VLAN_CFI);
1161 /* Returns true if 'value' may be a valid value *as part of a masked match*,
1164 * A value is not rejected just because it is not valid for the field in
1165 * question, but only if it doesn't make sense to test the bits in question at
1166 * all. For example, the MFF_VLAN_TCI field will never have a nonzero value
1167 * without the VLAN_CFI bit being set, but we can't reject those values because
1168 * it is still legitimate to test just for those bits (see the documentation
1169 * for NXM_OF_VLAN_TCI in nicira-ext.h). On the other hand, there is never a
1170 * reason to set the low bit of MFF_IP_DSCP to 1, so we reject that. */
1172 mf_is_value_valid(const struct mf_field *mf, const union mf_value *value)
1185 case MFF_SKB_PRIORITY:
1209 case MFF_ICMPV4_TYPE:
1210 case MFF_ICMPV4_CODE:
1211 case MFF_ICMPV6_TYPE:
1212 case MFF_ICMPV6_CODE:
1218 case MFF_IN_PORT_OXM: {
1220 return !ofputil_port_from_ofp11(value->be32, &port);
1224 return !(value->u8 & ~IP_DSCP_MASK);
1225 case MFF_IP_DSCP_SHIFTED:
1226 return !(value->u8 & (~IP_DSCP_MASK >> 2));
1228 return !(value->u8 & ~IP_ECN_MASK);
1230 return !(value->u8 & ~FLOW_NW_FRAG_MASK);
1232 return !(value->be16 & ~htons(0x0fff));
1235 return !(value->be16 & htons(0xff00));
1238 return !(value->be16 & htons(VLAN_CFI | VLAN_PCP_MASK));
1240 return !(value->be16 & htons(VLAN_PCP_MASK));
1242 case MFF_DL_VLAN_PCP:
1244 return !(value->u8 & ~(VLAN_PCP_MASK >> VLAN_PCP_SHIFT));
1246 case MFF_IPV6_LABEL:
1247 return !(value->be32 & ~htonl(IPV6_LABEL_MASK));
1249 case MFF_MPLS_LABEL:
1250 return !(value->be32 & ~htonl(MPLS_LABEL_MASK >> MPLS_LABEL_SHIFT));
1253 return !(value->u8 & ~(MPLS_TC_MASK >> MPLS_TC_SHIFT));
1256 return !(value->u8 & ~(MPLS_BOS_MASK >> MPLS_BOS_SHIFT));
1264 /* Copies the value of field 'mf' from 'flow' into 'value'. The caller is
1265 * responsible for ensuring that 'flow' meets 'mf''s prerequisites. */
1267 mf_get_value(const struct mf_field *mf, const struct flow *flow,
1268 union mf_value *value)
1272 value->be32 = htonl(flow->dp_hash);
1275 value->be32 = htonl(flow->recirc_id);
1278 value->be64 = flow->tunnel.tun_id;
1281 value->be32 = flow->tunnel.ip_src;
1284 value->be32 = flow->tunnel.ip_dst;
1287 value->be16 = htons(flow->tunnel.flags);
1290 value->u8 = flow->tunnel.ip_ttl;
1293 value->u8 = flow->tunnel.ip_tos;
1297 value->be64 = flow->metadata;
1301 value->be16 = htons(ofp_to_u16(flow->in_port.ofp_port));
1303 case MFF_IN_PORT_OXM:
1304 value->be32 = ofputil_port_to_ofp11(flow->in_port.ofp_port);
1307 case MFF_SKB_PRIORITY:
1308 value->be32 = htonl(flow->skb_priority);
1312 value->be32 = htonl(flow->pkt_mark);
1316 value->be32 = htonl(flow->regs[mf->id - MFF_REG0]);
1320 value->be64 = htonll(flow_get_xreg(flow, mf->id - MFF_XREG0));
1324 memcpy(value->mac, flow->dl_src, ETH_ADDR_LEN);
1328 memcpy(value->mac, flow->dl_dst, ETH_ADDR_LEN);
1332 value->be16 = flow->dl_type;
1336 value->be16 = flow->vlan_tci;
1340 value->be16 = flow->vlan_tci & htons(VLAN_VID_MASK);
1343 value->be16 = flow->vlan_tci & htons(VLAN_VID_MASK | VLAN_CFI);
1346 case MFF_DL_VLAN_PCP:
1348 value->u8 = vlan_tci_to_pcp(flow->vlan_tci);
1351 case MFF_MPLS_LABEL:
1352 value->be32 = htonl(mpls_lse_to_label(flow->mpls_lse[0]));
1356 value->u8 = mpls_lse_to_tc(flow->mpls_lse[0]);
1360 value->u8 = mpls_lse_to_bos(flow->mpls_lse[0]);
1364 value->be32 = flow->nw_src;
1368 value->be32 = flow->nw_dst;
1372 value->ipv6 = flow->ipv6_src;
1376 value->ipv6 = flow->ipv6_dst;
1379 case MFF_IPV6_LABEL:
1380 value->be32 = flow->ipv6_label;
1384 value->u8 = flow->nw_proto;
1388 value->u8 = flow->nw_tos & IP_DSCP_MASK;
1391 case MFF_IP_DSCP_SHIFTED:
1392 value->u8 = flow->nw_tos >> 2;
1396 value->u8 = flow->nw_tos & IP_ECN_MASK;
1400 value->u8 = flow->nw_ttl;
1404 value->u8 = flow->nw_frag;
1408 value->be16 = htons(flow->nw_proto);
1412 value->be32 = flow->nw_src;
1416 value->be32 = flow->nw_dst;
1421 memcpy(value->mac, flow->arp_sha, ETH_ADDR_LEN);
1426 memcpy(value->mac, flow->arp_tha, ETH_ADDR_LEN);
1432 value->be16 = flow->tp_src;
1438 value->be16 = flow->tp_dst;
1442 value->be16 = flow->tcp_flags;
1445 case MFF_ICMPV4_TYPE:
1446 case MFF_ICMPV6_TYPE:
1447 value->u8 = ntohs(flow->tp_src);
1450 case MFF_ICMPV4_CODE:
1451 case MFF_ICMPV6_CODE:
1452 value->u8 = ntohs(flow->tp_dst);
1456 value->ipv6 = flow->nd_target;
1465 /* Makes 'match' match field 'mf' exactly, with the value matched taken from
1466 * 'value'. The caller is responsible for ensuring that 'match' meets 'mf''s
1469 mf_set_value(const struct mf_field *mf,
1470 const union mf_value *value, struct match *match)
1474 match_set_dp_hash(match, ntohl(value->be32));
1477 match_set_recirc_id(match, ntohl(value->be32));
1480 match_set_tun_id(match, value->be64);
1483 match_set_tun_src(match, value->be32);
1486 match_set_tun_dst(match, value->be32);
1489 match_set_tun_flags(match, ntohs(value->be16));
1492 match_set_tun_tos(match, value->u8);
1495 match_set_tun_ttl(match, value->u8);
1499 match_set_metadata(match, value->be64);
1503 match_set_in_port(match, u16_to_ofp(ntohs(value->be16)));
1506 case MFF_IN_PORT_OXM: {
1508 ofputil_port_from_ofp11(value->be32, &port);
1509 match_set_in_port(match, port);
1513 case MFF_SKB_PRIORITY:
1514 match_set_skb_priority(match, ntohl(value->be32));
1518 match_set_pkt_mark(match, ntohl(value->be32));
1522 match_set_reg(match, mf->id - MFF_REG0, ntohl(value->be32));
1526 match_set_xreg(match, mf->id - MFF_XREG0, ntohll(value->be64));
1530 match_set_dl_src(match, value->mac);
1534 match_set_dl_dst(match, value->mac);
1538 match_set_dl_type(match, value->be16);
1542 match_set_dl_tci(match, value->be16);
1546 match_set_dl_vlan(match, value->be16);
1549 match_set_vlan_vid(match, value->be16);
1552 case MFF_DL_VLAN_PCP:
1554 match_set_dl_vlan_pcp(match, value->u8);
1557 case MFF_MPLS_LABEL:
1558 match_set_mpls_label(match, 0, value->be32);
1562 match_set_mpls_tc(match, 0, value->u8);
1566 match_set_mpls_bos(match, 0, value->u8);
1570 match_set_nw_src(match, value->be32);
1574 match_set_nw_dst(match, value->be32);
1578 match_set_ipv6_src(match, &value->ipv6);
1582 match_set_ipv6_dst(match, &value->ipv6);
1585 case MFF_IPV6_LABEL:
1586 match_set_ipv6_label(match, value->be32);
1590 match_set_nw_proto(match, value->u8);
1594 match_set_nw_dscp(match, value->u8);
1597 case MFF_IP_DSCP_SHIFTED:
1598 match_set_nw_dscp(match, value->u8 << 2);
1602 match_set_nw_ecn(match, value->u8);
1606 match_set_nw_ttl(match, value->u8);
1610 match_set_nw_frag(match, value->u8);
1614 match_set_nw_proto(match, ntohs(value->be16));
1618 match_set_nw_src(match, value->be32);
1622 match_set_nw_dst(match, value->be32);
1627 match_set_arp_sha(match, value->mac);
1632 match_set_arp_tha(match, value->mac);
1638 match_set_tp_src(match, value->be16);
1644 match_set_tp_dst(match, value->be16);
1648 match_set_tcp_flags(match, value->be16);
1651 case MFF_ICMPV4_TYPE:
1652 case MFF_ICMPV6_TYPE:
1653 match_set_icmp_type(match, value->u8);
1656 case MFF_ICMPV4_CODE:
1657 case MFF_ICMPV6_CODE:
1658 match_set_icmp_code(match, value->u8);
1662 match_set_nd_target(match, &value->ipv6);
1671 /* Unwildcard 'mask' member field described by 'mf'. The caller is
1672 * responsible for ensuring that 'mask' meets 'mf''s prerequisites. */
1674 mf_mask_field(const struct mf_field *mf, struct flow *mask)
1676 static const union mf_value exact_match_mask = MF_EXACT_MASK_INITIALIZER;
1678 /* For MFF_DL_VLAN, we cannot send a all 1's to flow_set_dl_vlan()
1679 * as that will be considered as OFP10_VLAN_NONE. So consider it as a
1680 * special case. For the rest, calling mf_set_flow_value() is good
1682 if (mf->id == MFF_DL_VLAN) {
1683 flow_set_dl_vlan(mask, htons(VLAN_VID_MASK));
1685 mf_set_flow_value(mf, &exact_match_mask, mask);
1689 /* Sets 'flow' member field described by 'mf' to 'value'. The caller is
1690 * responsible for ensuring that 'flow' meets 'mf''s prerequisites.*/
1692 mf_set_flow_value(const struct mf_field *mf,
1693 const union mf_value *value, struct flow *flow)
1697 flow->dp_hash = ntohl(value->be32);
1700 flow->recirc_id = ntohl(value->be32);
1703 flow->tunnel.tun_id = value->be64;
1706 flow->tunnel.ip_src = value->be32;
1709 flow->tunnel.ip_dst = value->be32;
1712 flow->tunnel.flags = ntohs(value->be16);
1715 flow->tunnel.ip_tos = value->u8;
1718 flow->tunnel.ip_ttl = value->u8;
1722 flow->metadata = value->be64;
1726 flow->in_port.ofp_port = u16_to_ofp(ntohs(value->be16));
1729 case MFF_IN_PORT_OXM: {
1731 ofputil_port_from_ofp11(value->be32, &port);
1732 flow->in_port.ofp_port = port;
1736 case MFF_SKB_PRIORITY:
1737 flow->skb_priority = ntohl(value->be32);
1741 flow->pkt_mark = ntohl(value->be32);
1745 flow->regs[mf->id - MFF_REG0] = ntohl(value->be32);
1749 flow_set_xreg(flow, mf->id - MFF_XREG0, ntohll(value->be64));
1753 memcpy(flow->dl_src, value->mac, ETH_ADDR_LEN);
1757 memcpy(flow->dl_dst, value->mac, ETH_ADDR_LEN);
1761 flow->dl_type = value->be16;
1765 flow->vlan_tci = value->be16;
1769 flow_set_dl_vlan(flow, value->be16);
1772 flow_set_vlan_vid(flow, value->be16);
1775 case MFF_DL_VLAN_PCP:
1777 flow_set_vlan_pcp(flow, value->u8);
1780 case MFF_MPLS_LABEL:
1781 flow_set_mpls_label(flow, 0, value->be32);
1785 flow_set_mpls_tc(flow, 0, value->u8);
1789 flow_set_mpls_bos(flow, 0, value->u8);
1793 flow->nw_src = value->be32;
1797 flow->nw_dst = value->be32;
1801 flow->ipv6_src = value->ipv6;
1805 flow->ipv6_dst = value->ipv6;
1808 case MFF_IPV6_LABEL:
1809 flow->ipv6_label = value->be32 & ~htonl(IPV6_LABEL_MASK);
1813 flow->nw_proto = value->u8;
1817 flow->nw_tos &= ~IP_DSCP_MASK;
1818 flow->nw_tos |= value->u8 & IP_DSCP_MASK;
1821 case MFF_IP_DSCP_SHIFTED:
1822 flow->nw_tos &= ~IP_DSCP_MASK;
1823 flow->nw_tos |= value->u8 << 2;
1827 flow->nw_tos &= ~IP_ECN_MASK;
1828 flow->nw_tos |= value->u8 & IP_ECN_MASK;
1832 flow->nw_ttl = value->u8;
1836 flow->nw_frag = value->u8 & FLOW_NW_FRAG_MASK;
1840 flow->nw_proto = ntohs(value->be16);
1844 flow->nw_src = value->be32;
1848 flow->nw_dst = value->be32;
1853 memcpy(flow->arp_sha, value->mac, ETH_ADDR_LEN);
1858 memcpy(flow->arp_tha, value->mac, ETH_ADDR_LEN);
1864 flow->tp_src = value->be16;
1870 flow->tp_dst = value->be16;
1874 flow->tcp_flags = value->be16;
1877 case MFF_ICMPV4_TYPE:
1878 case MFF_ICMPV6_TYPE:
1879 flow->tp_src = htons(value->u8);
1882 case MFF_ICMPV4_CODE:
1883 case MFF_ICMPV6_CODE:
1884 flow->tp_dst = htons(value->u8);
1888 flow->nd_target = value->ipv6;
1897 /* Returns true if 'mf' has a zero value in 'flow', false if it is nonzero.
1899 * The caller is responsible for ensuring that 'flow' meets 'mf''s
1902 mf_is_zero(const struct mf_field *mf, const struct flow *flow)
1904 union mf_value value;
1906 mf_get_value(mf, flow, &value);
1907 return is_all_zeros(&value, mf->n_bytes);
1910 /* Makes 'match' wildcard field 'mf'.
1912 * The caller is responsible for ensuring that 'match' meets 'mf''s
1915 mf_set_wild(const struct mf_field *mf, struct match *match)
1919 match->flow.dp_hash = 0;
1920 match->wc.masks.dp_hash = 0;
1923 match->flow.recirc_id = 0;
1924 match->wc.masks.recirc_id = 0;
1927 match_set_tun_id_masked(match, htonll(0), htonll(0));
1930 match_set_tun_src_masked(match, htonl(0), htonl(0));
1933 match_set_tun_dst_masked(match, htonl(0), htonl(0));
1936 match_set_tun_flags_masked(match, 0, 0);
1939 match_set_tun_tos_masked(match, 0, 0);
1942 match_set_tun_ttl_masked(match, 0, 0);
1946 match_set_metadata_masked(match, htonll(0), htonll(0));
1950 case MFF_IN_PORT_OXM:
1951 match->flow.in_port.ofp_port = 0;
1952 match->wc.masks.in_port.ofp_port = 0;
1955 case MFF_SKB_PRIORITY:
1956 match->flow.skb_priority = 0;
1957 match->wc.masks.skb_priority = 0;
1961 match->flow.pkt_mark = 0;
1962 match->wc.masks.pkt_mark = 0;
1966 match_set_reg_masked(match, mf->id - MFF_REG0, 0, 0);
1970 match_set_xreg_masked(match, mf->id - MFF_XREG0, 0, 0);
1974 memset(match->flow.dl_src, 0, ETH_ADDR_LEN);
1975 memset(match->wc.masks.dl_src, 0, ETH_ADDR_LEN);
1979 memset(match->flow.dl_dst, 0, ETH_ADDR_LEN);
1980 memset(match->wc.masks.dl_dst, 0, ETH_ADDR_LEN);
1984 match->flow.dl_type = htons(0);
1985 match->wc.masks.dl_type = htons(0);
1989 match_set_dl_tci_masked(match, htons(0), htons(0));
1994 match_set_any_vid(match);
1997 case MFF_DL_VLAN_PCP:
1999 match_set_any_pcp(match);
2002 case MFF_MPLS_LABEL:
2003 match_set_any_mpls_label(match, 0);
2007 match_set_any_mpls_tc(match, 0);
2011 match_set_any_mpls_bos(match, 0);
2016 match_set_nw_src_masked(match, htonl(0), htonl(0));
2021 match_set_nw_dst_masked(match, htonl(0), htonl(0));
2025 memset(&match->wc.masks.ipv6_src, 0, sizeof match->wc.masks.ipv6_src);
2026 memset(&match->flow.ipv6_src, 0, sizeof match->flow.ipv6_src);
2030 memset(&match->wc.masks.ipv6_dst, 0, sizeof match->wc.masks.ipv6_dst);
2031 memset(&match->flow.ipv6_dst, 0, sizeof match->flow.ipv6_dst);
2034 case MFF_IPV6_LABEL:
2035 match->wc.masks.ipv6_label = htonl(0);
2036 match->flow.ipv6_label = htonl(0);
2040 match->wc.masks.nw_proto = 0;
2041 match->flow.nw_proto = 0;
2045 case MFF_IP_DSCP_SHIFTED:
2046 match->wc.masks.nw_tos &= ~IP_DSCP_MASK;
2047 match->flow.nw_tos &= ~IP_DSCP_MASK;
2051 match->wc.masks.nw_tos &= ~IP_ECN_MASK;
2052 match->flow.nw_tos &= ~IP_ECN_MASK;
2056 match->wc.masks.nw_ttl = 0;
2057 match->flow.nw_ttl = 0;
2061 match->wc.masks.nw_frag |= FLOW_NW_FRAG_MASK;
2062 match->flow.nw_frag &= ~FLOW_NW_FRAG_MASK;
2066 match->wc.masks.nw_proto = 0;
2067 match->flow.nw_proto = 0;
2072 memset(match->flow.arp_sha, 0, ETH_ADDR_LEN);
2073 memset(match->wc.masks.arp_sha, 0, ETH_ADDR_LEN);
2078 memset(match->flow.arp_tha, 0, ETH_ADDR_LEN);
2079 memset(match->wc.masks.arp_tha, 0, ETH_ADDR_LEN);
2085 case MFF_ICMPV4_TYPE:
2086 case MFF_ICMPV6_TYPE:
2087 match->wc.masks.tp_src = htons(0);
2088 match->flow.tp_src = htons(0);
2094 case MFF_ICMPV4_CODE:
2095 case MFF_ICMPV6_CODE:
2096 match->wc.masks.tp_dst = htons(0);
2097 match->flow.tp_dst = htons(0);
2101 match->wc.masks.tcp_flags = htons(0);
2102 match->flow.tcp_flags = htons(0);
2106 memset(&match->wc.masks.nd_target, 0,
2107 sizeof match->wc.masks.nd_target);
2108 memset(&match->flow.nd_target, 0, sizeof match->flow.nd_target);
2117 /* Makes 'match' match field 'mf' with the specified 'value' and 'mask'.
2118 * 'value' specifies a value to match and 'mask' specifies a wildcard pattern,
2119 * with a 1-bit indicating that the corresponding value bit must match and a
2120 * 0-bit indicating a don't-care.
2122 * If 'mask' is NULL or points to all-1-bits, then this call is equivalent to
2123 * mf_set_value(mf, value, match). If 'mask' points to all-0-bits, then this
2124 * call is equivalent to mf_set_wild(mf, match).
2126 * 'mask' must be a valid mask for 'mf' (see mf_is_mask_valid()). The caller
2127 * is responsible for ensuring that 'match' meets 'mf''s prerequisites. */
2128 enum ofputil_protocol
2129 mf_set(const struct mf_field *mf,
2130 const union mf_value *value, const union mf_value *mask,
2131 struct match *match)
2133 if (!mask || is_all_ones(mask, mf->n_bytes)) {
2134 mf_set_value(mf, value, match);
2135 return mf->usable_protocols;
2136 } else if (is_all_zeros(mask, mf->n_bytes)) {
2137 mf_set_wild(mf, match);
2138 return OFPUTIL_P_ANY;
2144 case MFF_IN_PORT_OXM:
2145 case MFF_SKB_PRIORITY:
2148 case MFF_DL_VLAN_PCP:
2150 case MFF_MPLS_LABEL:
2156 case MFF_IP_DSCP_SHIFTED:
2159 case MFF_ICMPV4_TYPE:
2160 case MFF_ICMPV4_CODE:
2161 case MFF_ICMPV6_TYPE:
2162 case MFF_ICMPV6_CODE:
2163 return OFPUTIL_P_NONE;
2166 match_set_dp_hash_masked(match, ntohl(value->be32), ntohl(mask->be32));
2169 match_set_tun_id_masked(match, value->be64, mask->be64);
2172 match_set_tun_src_masked(match, value->be32, mask->be32);
2175 match_set_tun_dst_masked(match, value->be32, mask->be32);
2178 match_set_tun_flags_masked(match, ntohs(value->be16), ntohs(mask->be16));
2181 match_set_tun_ttl_masked(match, value->u8, mask->u8);
2184 match_set_tun_tos_masked(match, value->u8, mask->u8);
2188 match_set_metadata_masked(match, value->be64, mask->be64);
2192 match_set_reg_masked(match, mf->id - MFF_REG0,
2193 ntohl(value->be32), ntohl(mask->be32));
2197 match_set_xreg_masked(match, mf->id - MFF_XREG0,
2198 ntohll(value->be64), ntohll(mask->be64));
2202 match_set_pkt_mark_masked(match, ntohl(value->be32),
2207 match_set_dl_dst_masked(match, value->mac, mask->mac);
2211 match_set_dl_src_masked(match, value->mac, mask->mac);
2216 match_set_arp_sha_masked(match, value->mac, mask->mac);
2221 match_set_arp_tha_masked(match, value->mac, mask->mac);
2225 match_set_dl_tci_masked(match, value->be16, mask->be16);
2229 match_set_vlan_vid_masked(match, value->be16, mask->be16);
2233 match_set_nw_src_masked(match, value->be32, mask->be32);
2237 match_set_nw_dst_masked(match, value->be32, mask->be32);
2241 match_set_ipv6_src_masked(match, &value->ipv6, &mask->ipv6);
2245 match_set_ipv6_dst_masked(match, &value->ipv6, &mask->ipv6);
2248 case MFF_IPV6_LABEL:
2249 if ((mask->be32 & htonl(IPV6_LABEL_MASK)) == htonl(IPV6_LABEL_MASK)) {
2250 mf_set_value(mf, value, match);
2252 match_set_ipv6_label_masked(match, value->be32, mask->be32);
2257 match_set_nd_target_masked(match, &value->ipv6, &mask->ipv6);
2261 match_set_nw_frag_masked(match, value->u8, mask->u8);
2265 match_set_nw_src_masked(match, value->be32, mask->be32);
2269 match_set_nw_dst_masked(match, value->be32, mask->be32);
2275 match_set_tp_src_masked(match, value->be16, mask->be16);
2281 match_set_tp_dst_masked(match, value->be16, mask->be16);
2285 match_set_tcp_flags_masked(match, value->be16, mask->be16);
2293 return mf->usable_protocols_bitwise;
2296 return ip_is_cidr(mask->be32) ? mf->usable_protocols :
2297 mf->usable_protocols_bitwise;
2301 mf_check__(const struct mf_subfield *sf, const struct flow *flow,
2305 VLOG_WARN_RL(&rl, "unknown %s field", type);
2306 return OFPERR_OFPBAC_BAD_SET_TYPE;
2307 } else if (!sf->n_bits) {
2308 VLOG_WARN_RL(&rl, "zero bit %s field %s", type, sf->field->name);
2309 return OFPERR_OFPBAC_BAD_SET_LEN;
2310 } else if (sf->ofs >= sf->field->n_bits) {
2311 VLOG_WARN_RL(&rl, "bit offset %d exceeds %d-bit width of %s field %s",
2312 sf->ofs, sf->field->n_bits, type, sf->field->name);
2313 return OFPERR_OFPBAC_BAD_SET_LEN;
2314 } else if (sf->ofs + sf->n_bits > sf->field->n_bits) {
2315 VLOG_WARN_RL(&rl, "bit offset %d and width %d exceeds %d-bit width "
2316 "of %s field %s", sf->ofs, sf->n_bits,
2317 sf->field->n_bits, type, sf->field->name);
2318 return OFPERR_OFPBAC_BAD_SET_LEN;
2319 } else if (flow && !mf_are_prereqs_ok(sf->field, flow)) {
2320 VLOG_WARN_RL(&rl, "%s field %s lacks correct prerequisites",
2321 type, sf->field->name);
2322 return OFPERR_OFPBAC_MATCH_INCONSISTENT;
2328 /* Checks whether 'sf' is valid for reading a subfield out of 'flow'. Returns
2329 * 0 if so, otherwise an OpenFlow error code (e.g. as returned by
2332 mf_check_src(const struct mf_subfield *sf, const struct flow *flow)
2334 return mf_check__(sf, flow, "source");
2337 /* Checks whether 'sf' is valid for writing a subfield into 'flow'. Returns 0
2338 * if so, otherwise an OpenFlow error code (e.g. as returned by
2341 mf_check_dst(const struct mf_subfield *sf, const struct flow *flow)
2343 int error = mf_check__(sf, flow, "destination");
2344 if (!error && !sf->field->writable) {
2345 VLOG_WARN_RL(&rl, "destination field %s is not writable",
2347 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
2352 /* Copies the value and wildcard bit pattern for 'mf' from 'match' into the
2353 * 'value' and 'mask', respectively. */
2355 mf_get(const struct mf_field *mf, const struct match *match,
2356 union mf_value *value, union mf_value *mask)
2358 mf_get_value(mf, &match->flow, value);
2359 mf_get_mask(mf, &match->wc, mask);
2363 mf_from_integer_string(const struct mf_field *mf, const char *s,
2364 uint8_t *valuep, uint8_t *maskp)
2366 unsigned long long int integer, mask;
2371 integer = strtoull(s, &tail, 0);
2372 if (errno || (*tail != '\0' && *tail != '/')) {
2377 mask = strtoull(tail + 1, &tail, 0);
2378 if (errno || *tail != '\0') {
2385 for (i = mf->n_bytes - 1; i >= 0; i--) {
2386 valuep[i] = integer;
2392 return xasprintf("%s: value too large for %u-byte field %s",
2393 s, mf->n_bytes, mf->name);
2398 return xasprintf("%s: bad syntax for %s", s, mf->name);
2402 mf_from_ethernet_string(const struct mf_field *mf, const char *s,
2403 uint8_t mac[ETH_ADDR_LEN],
2404 uint8_t mask[ETH_ADDR_LEN])
2408 ovs_assert(mf->n_bytes == ETH_ADDR_LEN);
2411 if (ovs_scan(s, ETH_ADDR_SCAN_FMT"%n", ETH_ADDR_SCAN_ARGS(mac), &n)
2412 && n == strlen(s)) {
2413 memset(mask, 0xff, ETH_ADDR_LEN);
2418 if (ovs_scan(s, ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT"%n",
2419 ETH_ADDR_SCAN_ARGS(mac), ETH_ADDR_SCAN_ARGS(mask), &n)
2420 && n == strlen(s)) {
2424 return xasprintf("%s: invalid Ethernet address", s);
2428 mf_from_ipv4_string(const struct mf_field *mf, const char *s,
2429 ovs_be32 *ip, ovs_be32 *mask)
2433 ovs_assert(mf->n_bytes == sizeof *ip);
2435 if (ovs_scan(s, IP_SCAN_FMT"/"IP_SCAN_FMT,
2436 IP_SCAN_ARGS(ip), IP_SCAN_ARGS(mask))) {
2438 } else if (ovs_scan(s, IP_SCAN_FMT"/%d", IP_SCAN_ARGS(ip), &prefix)) {
2439 if (prefix <= 0 || prefix > 32) {
2440 return xasprintf("%s: network prefix bits not between 1 and "
2442 } else if (prefix == 32) {
2443 *mask = OVS_BE32_MAX;
2445 *mask = htonl(((1u << prefix) - 1) << (32 - prefix));
2447 } else if (ovs_scan(s, IP_SCAN_FMT, IP_SCAN_ARGS(ip))) {
2448 *mask = OVS_BE32_MAX;
2450 return xasprintf("%s: invalid IP address", s);
2456 mf_from_ipv6_string(const struct mf_field *mf, const char *s,
2457 struct in6_addr *value, struct in6_addr *mask)
2459 char *str = xstrdup(s);
2460 char *save_ptr = NULL;
2461 const char *name, *netmask;
2464 ovs_assert(mf->n_bytes == sizeof *value);
2466 name = strtok_r(str, "/", &save_ptr);
2467 retval = name ? lookup_ipv6(name, value) : EINVAL;
2471 err = xasprintf("%s: could not convert to IPv6 address", str);
2477 netmask = strtok_r(NULL, "/", &save_ptr);
2479 if (inet_pton(AF_INET6, netmask, mask) != 1) {
2480 int prefix = atoi(netmask);
2481 if (prefix <= 0 || prefix > 128) {
2483 return xasprintf("%s: prefix bits not between 1 and 128", s);
2485 *mask = ipv6_create_mask(prefix);
2489 *mask = in6addr_exact;
2497 mf_from_ofp_port_string(const struct mf_field *mf, const char *s,
2498 ovs_be16 *valuep, ovs_be16 *maskp)
2502 ovs_assert(mf->n_bytes == sizeof(ovs_be16));
2504 if (ofputil_port_from_string(s, &port)) {
2505 *valuep = htons(ofp_to_u16(port));
2506 *maskp = OVS_BE16_MAX;
2509 return xasprintf("%s: port value out of range for %s", s, mf->name);
2513 mf_from_ofp_port_string32(const struct mf_field *mf, const char *s,
2514 ovs_be32 *valuep, ovs_be32 *maskp)
2518 ovs_assert(mf->n_bytes == sizeof(ovs_be32));
2519 if (ofputil_port_from_string(s, &port)) {
2520 *valuep = ofputil_port_to_ofp11(port);
2521 *maskp = OVS_BE32_MAX;
2524 return xasprintf("%s: port value out of range for %s", s, mf->name);
2527 struct frag_handling {
2533 static const struct frag_handling all_frags[] = {
2534 #define A FLOW_NW_FRAG_ANY
2535 #define L FLOW_NW_FRAG_LATER
2536 /* name mask value */
2539 { "first", A|L, A },
2540 { "later", A|L, A|L },
2545 { "not_later", L, 0 },
2552 mf_from_frag_string(const char *s, uint8_t *valuep, uint8_t *maskp)
2554 const struct frag_handling *h;
2556 for (h = all_frags; h < &all_frags[ARRAY_SIZE(all_frags)]; h++) {
2557 if (!strcasecmp(s, h->name)) {
2558 /* We force the upper bits of the mask on to make mf_parse_value()
2559 * happy (otherwise it will never think it's an exact match.) */
2560 *maskp = h->mask | ~FLOW_NW_FRAG_MASK;
2566 return xasprintf("%s: unknown fragment type (valid types are \"no\", "
2567 "\"yes\", \"first\", \"later\", \"not_first\"", s);
2571 parse_flow_tun_flags(const char *s_, const char *(*bit_to_string)(uint32_t),
2574 uint32_t result = 0;
2575 char *save_ptr = NULL;
2578 char *s = xstrdup(s_);
2580 for (name = strtok_r((char *)s, " |", &save_ptr); name;
2581 name = strtok_r(NULL, " |", &save_ptr)) {
2583 unsigned long long int flags;
2586 if (ovs_scan(name, "%lli", &flags)) {
2590 name_len = strlen(name);
2591 for (bit = 1; bit; bit <<= 1) {
2592 const char *fname = bit_to_string(bit);
2599 len = strlen(fname);
2600 if (len != name_len) {
2603 if (!strncmp(name, fname, len)) {
2615 *res = htons(result);
2622 mf_from_tun_flags_string(const char *s, ovs_be16 *valuep, ovs_be16 *maskp)
2624 if (!parse_flow_tun_flags(s, flow_tun_flag_to_string, valuep)) {
2625 *maskp = OVS_BE16_MAX;
2629 return xasprintf("%s: unknown tunnel flags (valid flags are \"df\", "
2630 "\"csum\", \"key\")", s);
2634 mf_from_tcp_flags_string(const char *s, ovs_be16 *flagsp, ovs_be16 *maskp)
2641 if (ovs_scan(s, "%"SCNi16"/%"SCNi16"%n", &flags, &mask, &n) && !s[n]) {
2642 *flagsp = htons(flags);
2643 *maskp = htons(mask);
2646 if (ovs_scan(s, "%"SCNi16"%n", &flags, &n) && !s[n]) {
2647 *flagsp = htons(flags);
2648 *maskp = OVS_BE16_MAX;
2652 while (*s != '\0') {
2664 return xasprintf("%s: TCP flag must be preceded by '+' (for SET) "
2665 "or '-' (NOT SET)", s);
2669 name_len = strcspn(s,"+-");
2671 for (bit = 1; bit; bit <<= 1) {
2672 const char *fname = packet_tcp_flag_to_string(bit);
2679 len = strlen(fname);
2680 if (len != name_len) {
2683 if (!strncmp(s, fname, len)) {
2685 return xasprintf("%s: Each TCP flag can be specified only "
2697 return xasprintf("%s: unknown TCP flag(s)", s);
2702 *flagsp = htons(flags);
2703 *maskp = htons(mask);
2708 /* Parses 's', a string value for field 'mf', into 'value' and 'mask'. Returns
2709 * NULL if successful, otherwise a malloc()'d string describing the error. */
2711 mf_parse(const struct mf_field *mf, const char *s,
2712 union mf_value *value, union mf_value *mask)
2716 if (!strcmp(s, "*")) {
2717 memset(value, 0, mf->n_bytes);
2718 memset(mask, 0, mf->n_bytes);
2722 switch (mf->string) {
2724 case MFS_HEXADECIMAL:
2725 error = mf_from_integer_string(mf, s,
2726 (uint8_t *) value, (uint8_t *) mask);
2730 error = mf_from_ethernet_string(mf, s, value->mac, mask->mac);
2734 error = mf_from_ipv4_string(mf, s, &value->be32, &mask->be32);
2738 error = mf_from_ipv6_string(mf, s, &value->ipv6, &mask->ipv6);
2742 error = mf_from_ofp_port_string(mf, s, &value->be16, &mask->be16);
2745 case MFS_OFP_PORT_OXM:
2746 error = mf_from_ofp_port_string32(mf, s, &value->be32, &mask->be32);
2750 error = mf_from_frag_string(s, &value->u8, &mask->u8);
2754 ovs_assert(mf->n_bytes == sizeof(ovs_be16));
2755 error = mf_from_tun_flags_string(s, &value->be16, &mask->be16);
2759 ovs_assert(mf->n_bytes == sizeof(ovs_be16));
2760 error = mf_from_tcp_flags_string(s, &value->be16, &mask->be16);
2767 if (!error && !mf_is_mask_valid(mf, mask)) {
2768 error = xasprintf("%s: invalid mask for field %s", s, mf->name);
2773 /* Parses 's', a string value for field 'mf', into 'value'. Returns NULL if
2774 * successful, otherwise a malloc()'d string describing the error. */
2776 mf_parse_value(const struct mf_field *mf, const char *s, union mf_value *value)
2778 union mf_value mask;
2781 error = mf_parse(mf, s, value, &mask);
2786 if (!is_all_ones((const uint8_t *) &mask, mf->n_bytes)) {
2787 return xasprintf("%s: wildcards not allowed here", s);
2793 mf_format_integer_string(const struct mf_field *mf, const uint8_t *valuep,
2794 const uint8_t *maskp, struct ds *s)
2796 unsigned long long int integer;
2799 ovs_assert(mf->n_bytes <= 8);
2802 for (i = 0; i < mf->n_bytes; i++) {
2803 integer = (integer << 8) | valuep[i];
2805 if (mf->string == MFS_HEXADECIMAL) {
2806 ds_put_format(s, "%#llx", integer);
2808 ds_put_format(s, "%lld", integer);
2812 unsigned long long int mask;
2815 for (i = 0; i < mf->n_bytes; i++) {
2816 mask = (mask << 8) | maskp[i];
2819 /* I guess we could write the mask in decimal for MFS_DECIMAL but I'm
2820 * not sure that that a bit-mask written in decimal is ever easier to
2821 * understand than the same bit-mask written in hexadecimal. */
2822 ds_put_format(s, "/%#llx", mask);
2827 mf_format_frag_string(uint8_t value, uint8_t mask, struct ds *s)
2829 const struct frag_handling *h;
2831 mask &= FLOW_NW_FRAG_MASK;
2834 for (h = all_frags; h < &all_frags[ARRAY_SIZE(all_frags)]; h++) {
2835 if (value == h->value && mask == h->mask) {
2836 ds_put_cstr(s, h->name);
2840 ds_put_cstr(s, "<error>");
2844 mf_format_tnl_flags_string(const ovs_be16 *valuep, struct ds *s)
2846 format_flags(s, flow_tun_flag_to_string, ntohs(*valuep), '|');
2850 mf_format_tcp_flags_string(ovs_be16 value, ovs_be16 mask, struct ds *s)
2852 format_flags_masked(s, NULL, packet_tcp_flag_to_string, ntohs(value),
2856 /* Appends to 's' a string representation of field 'mf' whose value is in
2857 * 'value' and 'mask'. 'mask' may be NULL to indicate an exact match. */
2859 mf_format(const struct mf_field *mf,
2860 const union mf_value *value, const union mf_value *mask,
2864 if (is_all_zeros(mask, mf->n_bytes)) {
2865 ds_put_cstr(s, "ANY");
2867 } else if (is_all_ones(mask, mf->n_bytes)) {
2872 switch (mf->string) {
2873 case MFS_OFP_PORT_OXM:
2876 ofputil_port_from_ofp11(value->be32, &port);
2877 ofputil_format_port(port, s);
2883 ofputil_format_port(u16_to_ofp(ntohs(value->be16)), s);
2888 case MFS_HEXADECIMAL:
2889 mf_format_integer_string(mf, (uint8_t *) value, (uint8_t *) mask, s);
2893 eth_format_masked(value->mac, mask->mac, s);
2897 ip_format_masked(value->be32, mask ? mask->be32 : OVS_BE32_MAX, s);
2901 print_ipv6_masked(s, &value->ipv6, mask ? &mask->ipv6 : NULL);
2905 mf_format_frag_string(value->u8, mask ? mask->u8 : UINT8_MAX, s);
2909 mf_format_tnl_flags_string(&value->be16, s);
2913 mf_format_tcp_flags_string(value->be16,
2914 mask ? mask->be16 : OVS_BE16_MAX, s);
2922 /* Makes subfield 'sf' within 'flow' exactly match the 'sf->n_bits'
2923 * least-significant bits in 'x'.
2926 mf_write_subfield_flow(const struct mf_subfield *sf,
2927 const union mf_subvalue *x, struct flow *flow)
2929 const struct mf_field *field = sf->field;
2930 union mf_value value;
2932 mf_get_value(field, flow, &value);
2933 bitwise_copy(x, sizeof *x, 0, &value, field->n_bytes,
2934 sf->ofs, sf->n_bits);
2935 mf_set_flow_value(field, &value, flow);
2938 /* Makes subfield 'sf' within 'match' exactly match the 'sf->n_bits'
2939 * least-significant bits in 'x'.
2942 mf_write_subfield(const struct mf_subfield *sf, const union mf_subvalue *x,
2943 struct match *match)
2945 const struct mf_field *field = sf->field;
2946 union mf_value value, mask;
2948 mf_get(field, match, &value, &mask);
2949 bitwise_copy(x, sizeof *x, 0, &value, field->n_bytes, sf->ofs, sf->n_bits);
2950 bitwise_one ( &mask, field->n_bytes, sf->ofs, sf->n_bits);
2951 mf_set(field, &value, &mask, match);
2954 /* Initializes 'x' to the value of 'sf' within 'flow'. 'sf' must be valid for
2955 * reading 'flow', e.g. as checked by mf_check_src(). */
2957 mf_read_subfield(const struct mf_subfield *sf, const struct flow *flow,
2958 union mf_subvalue *x)
2960 union mf_value value;
2962 mf_get_value(sf->field, flow, &value);
2964 memset(x, 0, sizeof *x);
2965 bitwise_copy(&value, sf->field->n_bytes, sf->ofs,
2970 /* Returns the value of 'sf' within 'flow'. 'sf' must be valid for reading
2971 * 'flow', e.g. as checked by mf_check_src() and sf->n_bits must be 64 or
2974 mf_get_subfield(const struct mf_subfield *sf, const struct flow *flow)
2976 union mf_value value;
2978 mf_get_value(sf->field, flow, &value);
2979 return bitwise_get(&value, sf->field->n_bytes, sf->ofs, sf->n_bits);
2982 /* Formats 'sf' into 's' in a format normally acceptable to
2983 * mf_parse_subfield(). (It won't be acceptable if sf->field is NULL or if
2984 * sf->field has no NXM name.) */
2986 mf_format_subfield(const struct mf_subfield *sf, struct ds *s)
2989 ds_put_cstr(s, "<unknown>");
2990 } else if (sf->field->nxm_name) {
2991 ds_put_cstr(s, sf->field->nxm_name);
2992 } else if (sf->field->nxm_header) {
2993 uint32_t header = sf->field->nxm_header;
2994 ds_put_format(s, "%d:%d", NXM_VENDOR(header), NXM_FIELD(header));
2996 ds_put_cstr(s, sf->field->name);
2999 if (sf->field && sf->ofs == 0 && sf->n_bits == sf->field->n_bits) {
3000 ds_put_cstr(s, "[]");
3001 } else if (sf->n_bits == 1) {
3002 ds_put_format(s, "[%d]", sf->ofs);
3004 ds_put_format(s, "[%d..%d]", sf->ofs, sf->ofs + sf->n_bits - 1);
3008 static const struct mf_field *
3009 mf_parse_subfield_name(const char *name, int name_len, bool *wild)
3013 *wild = name_len > 2 && !memcmp(&name[name_len - 2], "_W", 2);
3018 for (i = 0; i < MFF_N_IDS; i++) {
3019 const struct mf_field *mf = mf_from_id(i);
3022 && !strncmp(mf->nxm_name, name, name_len)
3023 && mf->nxm_name[name_len] == '\0') {
3027 && !strncmp(mf->oxm_name, name, name_len)
3028 && mf->oxm_name[name_len] == '\0') {
3036 /* Parses a subfield from the beginning of '*sp' into 'sf'. If successful,
3037 * returns NULL and advances '*sp' to the first byte following the parsed
3038 * string. On failure, returns a malloc()'d error message, does not modify
3039 * '*sp', and does not properly initialize 'sf'.
3041 * The syntax parsed from '*sp' takes the form "header[start..end]" where
3042 * 'header' is the name of an NXM field and 'start' and 'end' are (inclusive)
3043 * bit indexes. "..end" may be omitted to indicate a single bit. "start..end"
3044 * may both be omitted (the [] are still required) to indicate an entire
3046 char * WARN_UNUSED_RESULT
3047 mf_parse_subfield__(struct mf_subfield *sf, const char **sp)
3049 const struct mf_field *field;
3058 name_len = strcspn(s, "[");
3059 if (s[name_len] != '[') {
3060 return xasprintf("%s: missing [ looking for field name", *sp);
3063 field = mf_parse_subfield_name(name, name_len, &wild);
3065 return xasprintf("%s: unknown field `%.*s'", *sp, name_len, s);
3069 if (ovs_scan(s, "[%d..%d]", &start, &end)) {
3070 /* Nothing to do. */
3071 } else if (ovs_scan(s, "[%d]", &start)) {
3073 } else if (!strncmp(s, "[]", 2)) {
3075 end = field->n_bits - 1;
3077 return xasprintf("%s: syntax error expecting [] or [<bit>] or "
3078 "[<start>..<end>]", *sp);
3080 s = strchr(s, ']') + 1;
3083 return xasprintf("%s: starting bit %d is after ending bit %d",
3085 } else if (start >= field->n_bits) {
3086 return xasprintf("%s: starting bit %d is not valid because field is "
3087 "only %d bits wide", *sp, start, field->n_bits);
3088 } else if (end >= field->n_bits){
3089 return xasprintf("%s: ending bit %d is not valid because field is "
3090 "only %d bits wide", *sp, end, field->n_bits);
3095 sf->n_bits = end - start + 1;
3101 /* Parses a subfield from the entirety of 's' into 'sf'. Returns NULL if
3102 * successful, otherwise a malloc()'d string describing the error. The caller
3103 * is responsible for freeing the returned string.
3105 * The syntax parsed from 's' takes the form "header[start..end]" where
3106 * 'header' is the name of an NXM field and 'start' and 'end' are (inclusive)
3107 * bit indexes. "..end" may be omitted to indicate a single bit. "start..end"
3108 * may both be omitted (the [] are still required) to indicate an entire
3110 char * WARN_UNUSED_RESULT
3111 mf_parse_subfield(struct mf_subfield *sf, const char *s)
3113 char *error = mf_parse_subfield__(sf, &s);
3114 if (!error && s[0]) {
3115 error = xstrdup("unexpected input following field syntax");
3121 mf_format_subvalue(const union mf_subvalue *subvalue, struct ds *s)
3125 for (i = 0; i < ARRAY_SIZE(subvalue->u8); i++) {
3126 if (subvalue->u8[i]) {
3127 ds_put_format(s, "0x%"PRIx8, subvalue->u8[i]);
3128 for (i++; i < ARRAY_SIZE(subvalue->u8); i++) {
3129 ds_put_format(s, "%02"PRIx8, subvalue->u8[i]);
3134 ds_put_char(s, '0');