2 * Copyright (c) 2011, 2012, 2013 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_TUN_ID, "tun_id", "tunnel_id",
61 NXM_NX_TUN_ID, "NXM_NX_TUN_ID",
62 OXM_OF_TUNNEL_ID, "OXM_OF_TUNNEL_ID",
63 OFPUTIL_P_NXM_OXM_ANY,
64 OFPUTIL_P_NXM_OXM_ANY,
65 FLOW_U32OFS(tunnel.tun_id),
67 MFF_TUN_SRC, "tun_src", NULL,
73 NXM_NX_TUN_IPV4_SRC, "NXM_NX_TUN_IPV4_SRC",
74 NXM_NX_TUN_IPV4_SRC, "NXM_NX_TUN_IPV4_SRC",
75 OFPUTIL_P_NXM_OXM_ANY,
76 OFPUTIL_P_NXM_OXM_ANY,
77 FLOW_U32OFS(tunnel.ip_src),
79 MFF_TUN_DST, "tun_dst", NULL,
85 NXM_NX_TUN_IPV4_DST, "NXM_NX_TUN_IPV4_DST",
86 NXM_NX_TUN_IPV4_DST, "NXM_NX_TUN_IPV4_DST",
87 OFPUTIL_P_NXM_OXM_ANY,
88 OFPUTIL_P_NXM_OXM_ANY,
89 FLOW_U32OFS(tunnel.ip_dst),
91 MFF_TUN_FLAGS, "tun_flags", NULL,
103 MFF_TUN_TTL, "tun_ttl", NULL,
115 MFF_TUN_TOS, "tun_tos", NULL,
127 MFF_METADATA, "metadata", NULL,
128 MF_FIELD_SIZES(be64),
133 OXM_OF_METADATA, "OXM_OF_METADATA",
134 OXM_OF_METADATA, "OXM_OF_METADATA",
135 OFPUTIL_P_NXM_OF11_UP,
136 OFPUTIL_P_NXM_OF11_UP,
139 MFF_IN_PORT, "in_port", NULL,
140 MF_FIELD_SIZES(be16),
145 NXM_OF_IN_PORT, "NXM_OF_IN_PORT",
146 NXM_OF_IN_PORT, "NXM_OF_IN_PORT",
147 OFPUTIL_P_ANY, /* OF11+ via mapping to 32 bits. */
151 MFF_IN_PORT_OXM, "in_port_oxm", NULL,
152 MF_FIELD_SIZES(be32),
157 OXM_OF_IN_PORT, "OXM_OF_IN_PORT",
158 OXM_OF_IN_PORT, "OXM_OF_IN_PORT",
163 MFF_SKB_PRIORITY, "skb_priority", NULL,
164 MF_FIELD_SIZES(be32),
175 MFF_PKT_MARK, "pkt_mark", NULL,
176 MF_FIELD_SIZES(be32),
181 NXM_NX_PKT_MARK, "NXM_NX_PKT_MARK",
182 NXM_NX_PKT_MARK, "NXM_NX_PKT_MARK",
183 OFPUTIL_P_NXM_OXM_ANY,
184 OFPUTIL_P_NXM_OXM_ANY,
188 #define REGISTER(IDX) \
190 MFF_REG##IDX, "reg" #IDX, NULL, \
191 MF_FIELD_SIZES(be32), \
196 NXM_NX_REG(IDX), "NXM_NX_REG" #IDX, \
197 NXM_NX_REG(IDX), "NXM_NX_REG" #IDX, \
198 OFPUTIL_P_NXM_OXM_ANY, \
199 OFPUTIL_P_NXM_OXM_ANY, \
235 MFF_ETH_SRC, "eth_src", "dl_src",
241 NXM_OF_ETH_SRC, "NXM_OF_ETH_SRC",
242 OXM_OF_ETH_SRC, "OXM_OF_ETH_SRC",
244 OFPUTIL_P_NXM_OF11_UP, /* Bitwise masking only with NXM and OF11+! */
247 MFF_ETH_DST, "eth_dst", "dl_dst",
253 NXM_OF_ETH_DST, "NXM_OF_ETH_DST",
254 OXM_OF_ETH_DST, "OXM_OF_ETH_DST",
256 OFPUTIL_P_NXM_OF11_UP, /* Bitwise masking only with NXM and OF11+! */
259 MFF_ETH_TYPE, "eth_type", "dl_type",
260 MF_FIELD_SIZES(be16),
265 NXM_OF_ETH_TYPE, "NXM_OF_ETH_TYPE",
266 OXM_OF_ETH_TYPE, "OXM_OF_ETH_TYPE",
273 MFF_VLAN_TCI, "vlan_tci", NULL,
274 MF_FIELD_SIZES(be16),
279 NXM_OF_VLAN_TCI, "NXM_OF_VLAN_TCI",
280 NXM_OF_VLAN_TCI, "NXM_OF_VLAN_TCI",
282 OFPUTIL_P_NXM_OXM_ANY,
285 MFF_DL_VLAN, "dl_vlan", NULL,
286 sizeof(ovs_be16), 12,
294 OFPUTIL_P_NXM_OXM_ANY,
297 MFF_VLAN_VID, "vlan_vid", NULL,
298 sizeof(ovs_be16), 12,
303 OXM_OF_VLAN_VID, "OXM_OF_VLAN_VID",
304 OXM_OF_VLAN_VID, "OXM_OF_VLAN_VID",
306 OFPUTIL_P_NXM_OXM_ANY,
309 MFF_DL_VLAN_PCP, "dl_vlan_pcp", NULL,
317 OFPUTIL_P_ANY, /* Will be mapped to NXM and OXM. */
321 MFF_VLAN_PCP, "vlan_pcp", NULL,
327 OXM_OF_VLAN_PCP, "OXM_OF_VLAN_PCP",
328 OXM_OF_VLAN_PCP, "OXM_OF_VLAN_PCP",
329 OFPUTIL_P_ANY, /* Will be mapped to OF10 and NXM. */
338 MFF_MPLS_LABEL, "mpls_label", NULL,
344 OXM_OF_MPLS_LABEL, "OXM_OF_MPLS_LABEL",
345 OXM_OF_MPLS_LABEL, "OXM_OF_MPLS_LABEL",
346 OFPUTIL_P_NXM_OF11_UP,
350 MFF_MPLS_TC, "mpls_tc", NULL,
356 OXM_OF_MPLS_TC, "OXM_OF_MPLS_TC",
357 OXM_OF_MPLS_TC, "OXM_OF_MPLS_TC",
358 OFPUTIL_P_NXM_OF11_UP,
362 MFF_MPLS_BOS, "mpls_bos", NULL,
368 OXM_OF_MPLS_BOS, "OXM_OF_MPLS_BOS",
369 OXM_OF_MPLS_BOS, "OXM_OF_MPLS_BOS",
370 OFPUTIL_P_NXM_OXM_ANY,
380 MFF_IPV4_SRC, "ip_src", "nw_src",
381 MF_FIELD_SIZES(be32),
386 NXM_OF_IP_SRC, "NXM_OF_IP_SRC",
387 OXM_OF_IPV4_SRC, "OXM_OF_IPV4_SRC",
389 OFPUTIL_P_NXM_OF11_UP,
392 MFF_IPV4_DST, "ip_dst", "nw_dst",
393 MF_FIELD_SIZES(be32),
398 NXM_OF_IP_DST, "NXM_OF_IP_DST",
399 OXM_OF_IPV4_DST, "OXM_OF_IPV4_DST",
401 OFPUTIL_P_NXM_OF11_UP,
406 MFF_IPV6_SRC, "ipv6_src", NULL,
407 MF_FIELD_SIZES(ipv6),
412 NXM_NX_IPV6_SRC, "NXM_NX_IPV6_SRC",
413 OXM_OF_IPV6_SRC, "OXM_OF_IPV6_SRC",
414 OFPUTIL_P_NXM_OXM_ANY,
415 OFPUTIL_P_NXM_OXM_ANY,
416 FLOW_U32OFS(ipv6_src),
418 MFF_IPV6_DST, "ipv6_dst", NULL,
419 MF_FIELD_SIZES(ipv6),
424 NXM_NX_IPV6_DST, "NXM_NX_IPV6_DST",
425 OXM_OF_IPV6_DST, "OXM_OF_IPV6_DST",
426 OFPUTIL_P_NXM_OXM_ANY,
427 OFPUTIL_P_NXM_OXM_ANY,
428 FLOW_U32OFS(ipv6_dst),
431 MFF_IPV6_LABEL, "ipv6_label", NULL,
437 NXM_NX_IPV6_LABEL, "NXM_NX_IPV6_LABEL",
438 OXM_OF_IPV6_FLABEL, "OXM_OF_IPV6_FLABEL",
439 OFPUTIL_P_NXM_OXM_ANY,
440 OFPUTIL_P_NXM_OXM_ANY,
445 MFF_IP_PROTO, "nw_proto", "ip_proto",
451 NXM_OF_IP_PROTO, "NXM_OF_IP_PROTO",
452 OXM_OF_IP_PROTO, "OXM_OF_IP_PROTO",
457 MFF_IP_DSCP, "nw_tos", NULL,
463 NXM_OF_IP_TOS, "NXM_OF_IP_TOS",
464 NXM_OF_IP_TOS, "NXM_OF_IP_TOS",
465 OFPUTIL_P_ANY, /* Will be shifted for OXM. */
469 MFF_IP_DSCP_SHIFTED, "ip_dscp", NULL,
475 OXM_OF_IP_DSCP, "OXM_OF_IP_DSCP",
476 OXM_OF_IP_DSCP, "OXM_OF_IP_DSCP",
477 OFPUTIL_P_ANY, /* Will be shifted for non-OXM. */
481 MFF_IP_ECN, "nw_ecn", "ip_ecn",
487 NXM_NX_IP_ECN, "NXM_NX_IP_ECN",
488 OXM_OF_IP_ECN, "OXM_OF_IP_ECN",
489 OFPUTIL_P_NXM_OXM_ANY,
493 MFF_IP_TTL, "nw_ttl", NULL,
499 NXM_NX_IP_TTL, "NXM_NX_IP_TTL",
500 NXM_NX_IP_TTL, "NXM_NX_IP_TTL",
501 OFPUTIL_P_NXM_OXM_ANY,
505 MFF_IP_FRAG, "ip_frag", NULL,
511 NXM_NX_IP_FRAG, "NXM_NX_IP_FRAG",
512 NXM_NX_IP_FRAG, "NXM_NX_IP_FRAG",
513 OFPUTIL_P_NXM_OXM_ANY,
514 OFPUTIL_P_NXM_OXM_ANY,
519 MFF_ARP_OP, "arp_op", NULL,
520 MF_FIELD_SIZES(be16),
525 NXM_OF_ARP_OP, "NXM_OF_ARP_OP",
526 OXM_OF_ARP_OP, "OXM_OF_ARP_OP",
531 MFF_ARP_SPA, "arp_spa", NULL,
532 MF_FIELD_SIZES(be32),
537 NXM_OF_ARP_SPA, "NXM_OF_ARP_SPA",
538 OXM_OF_ARP_SPA, "OXM_OF_ARP_SPA",
540 OFPUTIL_P_NXM_OF11_UP,
543 MFF_ARP_TPA, "arp_tpa", NULL,
544 MF_FIELD_SIZES(be32),
549 NXM_OF_ARP_TPA, "NXM_OF_ARP_TPA",
550 OXM_OF_ARP_TPA, "OXM_OF_ARP_TPA",
552 OFPUTIL_P_NXM_OF11_UP,
555 MFF_ARP_SHA, "arp_sha", NULL,
561 NXM_NX_ARP_SHA, "NXM_NX_ARP_SHA",
562 OXM_OF_ARP_SHA, "OXM_OF_ARP_SHA",
563 OFPUTIL_P_NXM_OXM_ANY,
564 OFPUTIL_P_NXM_OXM_ANY,
567 MFF_ARP_THA, "arp_tha", NULL,
573 NXM_NX_ARP_THA, "NXM_NX_ARP_THA",
574 OXM_OF_ARP_THA, "OXM_OF_ARP_THA",
575 OFPUTIL_P_NXM_OXM_ANY,
576 OFPUTIL_P_NXM_OXM_ANY,
585 MFF_TCP_SRC, "tcp_src", "tp_src",
586 MF_FIELD_SIZES(be16),
591 NXM_OF_TCP_SRC, "NXM_OF_TCP_SRC",
592 OXM_OF_TCP_SRC, "OXM_OF_TCP_SRC",
594 OFPUTIL_P_NXM_OXM_ANY,
597 MFF_TCP_DST, "tcp_dst", "tp_dst",
598 MF_FIELD_SIZES(be16),
603 NXM_OF_TCP_DST, "NXM_OF_TCP_DST",
604 OXM_OF_TCP_DST, "OXM_OF_TCP_DST",
606 OFPUTIL_P_NXM_OXM_ANY,
609 MFF_TCP_FLAGS, "tcp_flags", NULL,
615 NXM_NX_TCP_FLAGS, "NXM_NX_TCP_FLAGS",
616 NXM_NX_TCP_FLAGS, "NXM_NX_TCP_FLAGS",
617 OFPUTIL_P_NXM_OXM_ANY,
618 OFPUTIL_P_NXM_OXM_ANY,
623 MFF_UDP_SRC, "udp_src", NULL,
624 MF_FIELD_SIZES(be16),
629 NXM_OF_UDP_SRC, "NXM_OF_UDP_SRC",
630 OXM_OF_UDP_SRC, "OXM_OF_UDP_SRC",
632 OFPUTIL_P_NXM_OXM_ANY,
635 MFF_UDP_DST, "udp_dst", NULL,
636 MF_FIELD_SIZES(be16),
641 NXM_OF_UDP_DST, "NXM_OF_UDP_DST",
642 OXM_OF_UDP_DST, "OXM_OF_UDP_DST",
644 OFPUTIL_P_NXM_OXM_ANY,
649 MFF_SCTP_SRC, "sctp_src", NULL,
650 MF_FIELD_SIZES(be16),
655 OXM_OF_SCTP_SRC, "OXM_OF_SCTP_SRC",
656 OXM_OF_SCTP_SRC, "OXM_OF_SCTP_SRC",
657 OFPUTIL_P_NXM_OF11_UP,
658 OFPUTIL_P_NXM_OXM_ANY,
661 MFF_SCTP_DST, "sctp_dst", NULL,
662 MF_FIELD_SIZES(be16),
667 OXM_OF_SCTP_DST, "OXM_OF_SCTP_DST",
668 OXM_OF_SCTP_DST, "OXM_OF_SCTP_DST",
669 OFPUTIL_P_NXM_OF11_UP,
670 OFPUTIL_P_NXM_OXM_ANY,
675 MFF_ICMPV4_TYPE, "icmp_type", NULL,
681 NXM_OF_ICMP_TYPE, "NXM_OF_ICMP_TYPE",
682 OXM_OF_ICMPV4_TYPE, "OXM_OF_ICMPV4_TYPE",
687 MFF_ICMPV4_CODE, "icmp_code", NULL,
693 NXM_OF_ICMP_CODE, "NXM_OF_ICMP_CODE",
694 OXM_OF_ICMPV4_CODE, "OXM_OF_ICMPV4_CODE",
701 MFF_ICMPV6_TYPE, "icmpv6_type", NULL,
707 NXM_NX_ICMPV6_TYPE, "NXM_NX_ICMPV6_TYPE",
708 OXM_OF_ICMPV6_TYPE, "OXM_OF_ICMPV6_TYPE",
709 OFPUTIL_P_NXM_OXM_ANY,
713 MFF_ICMPV6_CODE, "icmpv6_code", NULL,
719 NXM_NX_ICMPV6_CODE, "NXM_NX_ICMPV6_CODE",
720 OXM_OF_ICMPV6_CODE, "OXM_OF_ICMPV6_CODE",
721 OFPUTIL_P_NXM_OXM_ANY,
731 MFF_ND_TARGET, "nd_target", NULL,
732 MF_FIELD_SIZES(ipv6),
737 NXM_NX_ND_TARGET, "NXM_NX_ND_TARGET",
738 OXM_OF_IPV6_ND_TARGET, "OXM_OF_IPV6_ND_TARGET",
739 OFPUTIL_P_NXM_OXM_ANY,
740 OFPUTIL_P_NXM_OXM_ANY,
743 MFF_ND_SLL, "nd_sll", NULL,
749 NXM_NX_ND_SLL, "NXM_NX_ND_SLL",
750 OXM_OF_IPV6_ND_SLL, "OXM_OF_IPV6_ND_SLL",
751 OFPUTIL_P_NXM_OXM_ANY,
752 OFPUTIL_P_NXM_OXM_ANY,
755 MFF_ND_TLL, "nd_tll", NULL,
761 NXM_NX_ND_TLL, "NXM_NX_ND_TLL",
762 OXM_OF_IPV6_ND_TLL, "OXM_OF_IPV6_ND_TLL",
763 OFPUTIL_P_NXM_OXM_ANY,
764 OFPUTIL_P_NXM_OXM_ANY,
769 /* Maps an NXM or OXM header value to an mf_field. */
771 struct hmap_node hmap_node; /* In 'all_fields' hmap. */
772 uint32_t header; /* NXM or OXM header value. */
773 const struct mf_field *mf;
776 /* Contains 'struct nxm_field's. */
777 static struct hmap all_fields;
779 /* Maps from an mf_field's 'name' or 'extra_name' to the mf_field. */
780 static struct shash mf_by_name;
782 /* Rate limit for parse errors. These always indicate a bug in an OpenFlow
783 * controller and so there's not much point in showing a lot of them. */
784 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
786 const struct mf_field *mf_from_nxm_header__(uint32_t header);
787 static void nxm_init(void);
789 /* Returns the field with the given 'name', or a null pointer if no field has
791 const struct mf_field *
792 mf_from_name(const char *name)
795 return shash_find_data(&mf_by_name, name);
799 add_nxm_field(uint32_t header, const struct mf_field *mf)
803 f = xmalloc(sizeof *f);
804 hmap_insert(&all_fields, &f->hmap_node, hash_int(header, 0));
810 nxm_init_add_field(const struct mf_field *mf, uint32_t header)
813 ovs_assert(!mf_from_nxm_header__(header));
814 add_nxm_field(header, mf);
815 if (mf->maskable != MFM_NONE) {
816 add_nxm_field(NXM_MAKE_WILD_HEADER(header), mf);
826 hmap_init(&all_fields);
827 shash_init(&mf_by_name);
828 for (i = 0; i < MFF_N_IDS; i++) {
829 const struct mf_field *mf = &mf_fields[i];
831 ovs_assert(mf->id == i); /* Fields must be in the enum order. */
833 nxm_init_add_field(mf, mf->nxm_header);
834 if (mf->oxm_header != mf->nxm_header) {
835 nxm_init_add_field(mf, mf->oxm_header);
838 shash_add_once(&mf_by_name, mf->name, mf);
839 if (mf->extra_name) {
840 shash_add_once(&mf_by_name, mf->extra_name, mf);
848 static pthread_once_t once = PTHREAD_ONCE_INIT;
849 pthread_once(&once, nxm_do_init);
852 const struct mf_field *
853 mf_from_nxm_header(uint32_t header)
856 return mf_from_nxm_header__(header);
859 const struct mf_field *
860 mf_from_nxm_header__(uint32_t header)
862 const struct nxm_field *f;
864 HMAP_FOR_EACH_IN_BUCKET (f, hmap_node, hash_int(header, 0), &all_fields) {
865 if (f->header == header) {
873 /* Returns true if 'wc' wildcards all the bits in field 'mf', false if 'wc'
874 * specifies at least one bit in the field.
876 * The caller is responsible for ensuring that 'wc' corresponds to a flow that
877 * meets 'mf''s prerequisites. */
879 mf_is_all_wild(const struct mf_field *mf, const struct flow_wildcards *wc)
883 return !wc->masks.tunnel.ip_src;
885 return !wc->masks.tunnel.ip_dst;
890 return !wc->masks.tunnel.tun_id;
892 return !wc->masks.metadata;
894 case MFF_IN_PORT_OXM:
895 return !wc->masks.in_port.ofp_port;
896 case MFF_SKB_PRIORITY:
897 return !wc->masks.skb_priority;
899 return !wc->masks.pkt_mark;
901 return !wc->masks.regs[mf->id - MFF_REG0];
904 return eth_addr_is_zero(wc->masks.dl_src);
906 return eth_addr_is_zero(wc->masks.dl_dst);
908 return !wc->masks.dl_type;
912 return eth_addr_is_zero(wc->masks.arp_sha);
916 return eth_addr_is_zero(wc->masks.arp_tha);
919 return !wc->masks.vlan_tci;
921 return !(wc->masks.vlan_tci & htons(VLAN_VID_MASK));
923 return !(wc->masks.vlan_tci & htons(VLAN_VID_MASK | VLAN_CFI));
924 case MFF_DL_VLAN_PCP:
926 return !(wc->masks.vlan_tci & htons(VLAN_PCP_MASK));
929 return !(wc->masks.mpls_lse[0] & htonl(MPLS_LABEL_MASK));
931 return !(wc->masks.mpls_lse[1] & htonl(MPLS_TC_MASK));
933 return !(wc->masks.mpls_lse[2] & htonl(MPLS_BOS_MASK));
936 return !wc->masks.nw_src;
938 return !wc->masks.nw_dst;
941 return ipv6_mask_is_any(&wc->masks.ipv6_src);
943 return ipv6_mask_is_any(&wc->masks.ipv6_dst);
946 return !wc->masks.ipv6_label;
949 return !wc->masks.nw_proto;
951 case MFF_IP_DSCP_SHIFTED:
952 return !(wc->masks.nw_tos & IP_DSCP_MASK);
954 return !(wc->masks.nw_tos & IP_ECN_MASK);
956 return !wc->masks.nw_ttl;
959 return ipv6_mask_is_any(&wc->masks.nd_target);
962 return !(wc->masks.nw_frag & FLOW_NW_FRAG_MASK);
965 return !wc->masks.nw_proto;
967 return !wc->masks.nw_src;
969 return !wc->masks.nw_dst;
974 case MFF_ICMPV4_TYPE:
975 case MFF_ICMPV6_TYPE:
976 return !wc->masks.tp_src;
980 case MFF_ICMPV4_CODE:
981 case MFF_ICMPV6_CODE:
982 return !wc->masks.tp_dst;
984 return !wc->masks.tcp_flags;
992 /* Initializes 'mask' with the wildcard bit pattern for field 'mf' within 'wc'.
993 * Each bit in 'mask' will be set to 1 if the bit is significant for matching
994 * purposes, or to 0 if it is wildcarded.
996 * The caller is responsible for ensuring that 'wc' corresponds to a flow that
997 * meets 'mf''s prerequisites. */
999 mf_get_mask(const struct mf_field *mf, const struct flow_wildcards *wc,
1000 union mf_value *mask)
1002 mf_get_value(mf, &wc->masks, mask);
1005 /* Tests whether 'mask' is a valid wildcard bit pattern for 'mf'. Returns true
1006 * if the mask is valid, false otherwise. */
1008 mf_is_mask_valid(const struct mf_field *mf, const union mf_value *mask)
1010 switch (mf->maskable) {
1012 return (is_all_zeros((const uint8_t *) mask, mf->n_bytes) ||
1013 is_all_ones((const uint8_t *) mask, mf->n_bytes));
1022 /* Returns true if 'flow' meets the prerequisites for 'mf', false otherwise. */
1024 mf_are_prereqs_ok(const struct mf_field *mf, const struct flow *flow)
1026 switch (mf->prereqs) {
1031 return (flow->dl_type == htons(ETH_TYPE_ARP) ||
1032 flow->dl_type == htons(ETH_TYPE_RARP));
1034 return flow->dl_type == htons(ETH_TYPE_IP);
1036 return flow->dl_type == htons(ETH_TYPE_IPV6);
1038 return (flow->vlan_tci & htons(VLAN_CFI)) != 0;
1040 return eth_type_mpls(flow->dl_type);
1042 return is_ip_any(flow);
1045 return is_ip_any(flow) && flow->nw_proto == IPPROTO_TCP;
1047 return is_ip_any(flow) && flow->nw_proto == IPPROTO_UDP;
1049 return is_ip_any(flow) && flow->nw_proto == IPPROTO_SCTP;
1051 return is_icmpv4(flow);
1053 return is_icmpv6(flow);
1056 return (is_icmpv6(flow)
1057 && flow->tp_dst == htons(0)
1058 && (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT) ||
1059 flow->tp_src == htons(ND_NEIGHBOR_ADVERT)));
1060 case MFP_ND_SOLICIT:
1061 return (is_icmpv6(flow)
1062 && flow->tp_dst == htons(0)
1063 && (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT)));
1065 return (is_icmpv6(flow)
1066 && flow->tp_dst == htons(0)
1067 && (flow->tp_src == htons(ND_NEIGHBOR_ADVERT)));
1073 /* Set field and it's prerequisities in the mask.
1074 * This is only ever called for writeable 'mf's, but we do not make the
1075 * distinction here. */
1077 mf_mask_field_and_prereqs(const struct mf_field *mf, struct flow *mask)
1079 static const union mf_value exact_match_mask = MF_EXACT_MASK_INITIALIZER;
1081 mf_set_flow_value(mf, &exact_match_mask, mask);
1083 switch (mf->prereqs) {
1085 case MFP_ND_SOLICIT:
1087 mask->tp_src = OVS_BE16_MAX;
1088 mask->tp_dst = OVS_BE16_MAX;
1095 mask->nw_proto = 0xff;
1102 mask->dl_type = OVS_BE16_MAX;
1105 mask->vlan_tci |= htons(VLAN_CFI);
1113 /* Returns true if 'value' may be a valid value *as part of a masked match*,
1116 * A value is not rejected just because it is not valid for the field in
1117 * question, but only if it doesn't make sense to test the bits in question at
1118 * all. For example, the MFF_VLAN_TCI field will never have a nonzero value
1119 * without the VLAN_CFI bit being set, but we can't reject those values because
1120 * it is still legitimate to test just for those bits (see the documentation
1121 * for NXM_OF_VLAN_TCI in nicira-ext.h). On the other hand, there is never a
1122 * reason to set the low bit of MFF_IP_DSCP to 1, so we reject that. */
1124 mf_is_value_valid(const struct mf_field *mf, const union mf_value *value)
1135 case MFF_SKB_PRIORITY:
1158 case MFF_ICMPV4_TYPE:
1159 case MFF_ICMPV4_CODE:
1160 case MFF_ICMPV6_TYPE:
1161 case MFF_ICMPV6_CODE:
1167 case MFF_IN_PORT_OXM: {
1169 return !ofputil_port_from_ofp11(value->be32, &port);
1173 return !(value->u8 & ~IP_DSCP_MASK);
1174 case MFF_IP_DSCP_SHIFTED:
1175 return !(value->u8 & (~IP_DSCP_MASK >> 2));
1177 return !(value->u8 & ~IP_ECN_MASK);
1179 return !(value->u8 & ~FLOW_NW_FRAG_MASK);
1181 return !(value->be16 & ~htons(0x0fff));
1184 return !(value->be16 & htons(0xff00));
1187 return !(value->be16 & htons(VLAN_CFI | VLAN_PCP_MASK));
1189 return !(value->be16 & htons(VLAN_PCP_MASK));
1191 case MFF_DL_VLAN_PCP:
1193 return !(value->u8 & ~(VLAN_PCP_MASK >> VLAN_PCP_SHIFT));
1195 case MFF_IPV6_LABEL:
1196 return !(value->be32 & ~htonl(IPV6_LABEL_MASK));
1198 case MFF_MPLS_LABEL:
1199 return !(value->be32 & ~htonl(MPLS_LABEL_MASK >> MPLS_LABEL_SHIFT));
1202 return !(value->u8 & ~(MPLS_TC_MASK >> MPLS_TC_SHIFT));
1205 return !(value->u8 & ~(MPLS_BOS_MASK >> MPLS_BOS_SHIFT));
1213 /* Copies the value of field 'mf' from 'flow' into 'value'. The caller is
1214 * responsible for ensuring that 'flow' meets 'mf''s prerequisites. */
1216 mf_get_value(const struct mf_field *mf, const struct flow *flow,
1217 union mf_value *value)
1221 value->be64 = flow->tunnel.tun_id;
1224 value->be32 = flow->tunnel.ip_src;
1227 value->be32 = flow->tunnel.ip_dst;
1230 value->be16 = htons(flow->tunnel.flags);
1233 value->u8 = flow->tunnel.ip_ttl;
1236 value->u8 = flow->tunnel.ip_tos;
1240 value->be64 = flow->metadata;
1244 value->be16 = htons(ofp_to_u16(flow->in_port.ofp_port));
1246 case MFF_IN_PORT_OXM:
1247 value->be32 = ofputil_port_to_ofp11(flow->in_port.ofp_port);
1250 case MFF_SKB_PRIORITY:
1251 value->be32 = htonl(flow->skb_priority);
1255 value->be32 = htonl(flow->pkt_mark);
1259 value->be32 = htonl(flow->regs[mf->id - MFF_REG0]);
1263 memcpy(value->mac, flow->dl_src, ETH_ADDR_LEN);
1267 memcpy(value->mac, flow->dl_dst, ETH_ADDR_LEN);
1271 value->be16 = flow->dl_type;
1275 value->be16 = flow->vlan_tci;
1279 value->be16 = flow->vlan_tci & htons(VLAN_VID_MASK);
1282 value->be16 = flow->vlan_tci & htons(VLAN_VID_MASK | VLAN_CFI);
1285 case MFF_DL_VLAN_PCP:
1287 value->u8 = vlan_tci_to_pcp(flow->vlan_tci);
1290 case MFF_MPLS_LABEL:
1291 value->be32 = htonl(mpls_lse_to_label(flow->mpls_lse[0]));
1295 value->u8 = mpls_lse_to_tc(flow->mpls_lse[0]);
1299 value->u8 = mpls_lse_to_bos(flow->mpls_lse[0]);
1304 value->be32 = flow->nw_src;
1308 value->be32 = flow->nw_dst;
1312 value->ipv6 = flow->ipv6_src;
1316 value->ipv6 = flow->ipv6_dst;
1319 case MFF_IPV6_LABEL:
1320 value->be32 = flow->ipv6_label;
1324 value->u8 = flow->nw_proto;
1328 value->u8 = flow->nw_tos & IP_DSCP_MASK;
1331 case MFF_IP_DSCP_SHIFTED:
1332 value->u8 = flow->nw_tos >> 2;
1336 value->u8 = flow->nw_tos & IP_ECN_MASK;
1340 value->u8 = flow->nw_ttl;
1344 value->u8 = flow->nw_frag;
1348 value->be16 = htons(flow->nw_proto);
1352 value->be32 = flow->nw_src;
1356 value->be32 = flow->nw_dst;
1361 memcpy(value->mac, flow->arp_sha, ETH_ADDR_LEN);
1366 memcpy(value->mac, flow->arp_tha, ETH_ADDR_LEN);
1372 value->be16 = flow->tp_src;
1378 value->be16 = flow->tp_dst;
1382 value->be16 = flow->tcp_flags;
1385 case MFF_ICMPV4_TYPE:
1386 case MFF_ICMPV6_TYPE:
1387 value->u8 = ntohs(flow->tp_src);
1390 case MFF_ICMPV4_CODE:
1391 case MFF_ICMPV6_CODE:
1392 value->u8 = ntohs(flow->tp_dst);
1396 value->ipv6 = flow->nd_target;
1405 /* Makes 'match' match field 'mf' exactly, with the value matched taken from
1406 * 'value'. The caller is responsible for ensuring that 'match' meets 'mf''s
1409 mf_set_value(const struct mf_field *mf,
1410 const union mf_value *value, struct match *match)
1414 match_set_tun_id(match, value->be64);
1417 match_set_tun_src(match, value->be32);
1420 match_set_tun_dst(match, value->be32);
1423 match_set_tun_flags(match, ntohs(value->be16));
1426 match_set_tun_tos(match, value->u8);
1429 match_set_tun_ttl(match, value->u8);
1433 match_set_metadata(match, value->be64);
1437 match_set_in_port(match, u16_to_ofp(ntohs(value->be16)));
1440 case MFF_IN_PORT_OXM: {
1442 ofputil_port_from_ofp11(value->be32, &port);
1443 match_set_in_port(match, port);
1447 case MFF_SKB_PRIORITY:
1448 match_set_skb_priority(match, ntohl(value->be32));
1452 match_set_pkt_mark(match, ntohl(value->be32));
1456 match_set_reg(match, mf->id - MFF_REG0, ntohl(value->be32));
1460 match_set_dl_src(match, value->mac);
1464 match_set_dl_dst(match, value->mac);
1468 match_set_dl_type(match, value->be16);
1472 match_set_dl_tci(match, value->be16);
1476 match_set_dl_vlan(match, value->be16);
1479 match_set_vlan_vid(match, value->be16);
1482 case MFF_DL_VLAN_PCP:
1484 match_set_dl_vlan_pcp(match, value->u8);
1487 case MFF_MPLS_LABEL:
1488 match_set_mpls_label(match, 0, value->be32);
1492 match_set_mpls_tc(match, 0, value->u8);
1496 match_set_mpls_bos(match, 0, value->u8);
1501 match_set_nw_src(match, value->be32);
1505 match_set_nw_dst(match, value->be32);
1509 match_set_ipv6_src(match, &value->ipv6);
1513 match_set_ipv6_dst(match, &value->ipv6);
1516 case MFF_IPV6_LABEL:
1517 match_set_ipv6_label(match, value->be32);
1521 match_set_nw_proto(match, value->u8);
1525 match_set_nw_dscp(match, value->u8);
1528 case MFF_IP_DSCP_SHIFTED:
1529 match_set_nw_dscp(match, value->u8 << 2);
1533 match_set_nw_ecn(match, value->u8);
1537 match_set_nw_ttl(match, value->u8);
1541 match_set_nw_frag(match, value->u8);
1545 match_set_nw_proto(match, ntohs(value->be16));
1549 match_set_nw_src(match, value->be32);
1553 match_set_nw_dst(match, value->be32);
1558 match_set_arp_sha(match, value->mac);
1563 match_set_arp_tha(match, value->mac);
1569 match_set_tp_src(match, value->be16);
1575 match_set_tp_dst(match, value->be16);
1579 match_set_tcp_flags(match, value->be16);
1582 case MFF_ICMPV4_TYPE:
1583 case MFF_ICMPV6_TYPE:
1584 match_set_icmp_type(match, value->u8);
1587 case MFF_ICMPV4_CODE:
1588 case MFF_ICMPV6_CODE:
1589 match_set_icmp_code(match, value->u8);
1593 match_set_nd_target(match, &value->ipv6);
1602 /* Unwildcard 'mask' member field described by 'mf'. The caller is
1603 * responsible for ensuring that 'mask' meets 'mf''s prerequisites. */
1605 mf_mask_field(const struct mf_field *mf, struct flow *mask)
1607 static const union mf_value exact_match_mask = MF_EXACT_MASK_INITIALIZER;
1609 /* For MFF_DL_VLAN, we cannot send a all 1's to flow_set_dl_vlan()
1610 * as that will be considered as OFP10_VLAN_NONE. So consider it as a
1611 * special case. For the rest, calling mf_set_flow_value() is good
1613 if (mf->id == MFF_DL_VLAN) {
1614 flow_set_dl_vlan(mask, htons(VLAN_VID_MASK));
1616 mf_set_flow_value(mf, &exact_match_mask, mask);
1620 /* Sets 'flow' member field described by 'mf' to 'value'. The caller is
1621 * responsible for ensuring that 'flow' meets 'mf''s prerequisites.*/
1623 mf_set_flow_value(const struct mf_field *mf,
1624 const union mf_value *value, struct flow *flow)
1628 flow->tunnel.tun_id = value->be64;
1631 flow->tunnel.ip_src = value->be32;
1634 flow->tunnel.ip_dst = value->be32;
1637 flow->tunnel.flags = ntohs(value->be16);
1640 flow->tunnel.ip_tos = value->u8;
1643 flow->tunnel.ip_ttl = value->u8;
1647 flow->metadata = value->be64;
1651 flow->in_port.ofp_port = u16_to_ofp(ntohs(value->be16));
1654 case MFF_IN_PORT_OXM: {
1656 ofputil_port_from_ofp11(value->be32, &port);
1657 flow->in_port.ofp_port = port;
1661 case MFF_SKB_PRIORITY:
1662 flow->skb_priority = ntohl(value->be32);
1666 flow->pkt_mark = ntohl(value->be32);
1670 flow->regs[mf->id - MFF_REG0] = ntohl(value->be32);
1674 memcpy(flow->dl_src, value->mac, ETH_ADDR_LEN);
1678 memcpy(flow->dl_dst, value->mac, ETH_ADDR_LEN);
1682 flow->dl_type = value->be16;
1686 flow->vlan_tci = value->be16;
1690 flow_set_dl_vlan(flow, value->be16);
1693 flow_set_vlan_vid(flow, value->be16);
1696 case MFF_DL_VLAN_PCP:
1698 flow_set_vlan_pcp(flow, value->u8);
1701 case MFF_MPLS_LABEL:
1702 flow_set_mpls_label(flow, 0, value->be32);
1706 flow_set_mpls_tc(flow, 0, value->u8);
1710 flow_set_mpls_bos(flow, 0, value->u8);
1715 flow->nw_src = value->be32;
1719 flow->nw_dst = value->be32;
1723 flow->ipv6_src = value->ipv6;
1727 flow->ipv6_dst = value->ipv6;
1730 case MFF_IPV6_LABEL:
1731 flow->ipv6_label = value->be32 & ~htonl(IPV6_LABEL_MASK);
1735 flow->nw_proto = value->u8;
1739 flow->nw_tos &= ~IP_DSCP_MASK;
1740 flow->nw_tos |= value->u8 & IP_DSCP_MASK;
1743 case MFF_IP_DSCP_SHIFTED:
1744 flow->nw_tos &= ~IP_DSCP_MASK;
1745 flow->nw_tos |= value->u8 << 2;
1749 flow->nw_tos &= ~IP_ECN_MASK;
1750 flow->nw_tos |= value->u8 & IP_ECN_MASK;
1754 flow->nw_ttl = value->u8;
1758 flow->nw_frag = value->u8 & FLOW_NW_FRAG_MASK;
1762 flow->nw_proto = ntohs(value->be16);
1766 flow->nw_src = value->be32;
1770 flow->nw_dst = value->be32;
1775 memcpy(flow->arp_sha, value->mac, ETH_ADDR_LEN);
1780 memcpy(flow->arp_tha, value->mac, ETH_ADDR_LEN);
1786 flow->tp_src = value->be16;
1792 flow->tp_dst = value->be16;
1796 flow->tcp_flags = value->be16;
1799 case MFF_ICMPV4_TYPE:
1800 case MFF_ICMPV6_TYPE:
1801 flow->tp_src = htons(value->u8);
1804 case MFF_ICMPV4_CODE:
1805 case MFF_ICMPV6_CODE:
1806 flow->tp_dst = htons(value->u8);
1810 flow->nd_target = value->ipv6;
1819 /* Returns true if 'mf' has a zero value in 'flow', false if it is nonzero.
1821 * The caller is responsible for ensuring that 'flow' meets 'mf''s
1824 mf_is_zero(const struct mf_field *mf, const struct flow *flow)
1826 union mf_value value;
1828 mf_get_value(mf, flow, &value);
1829 return is_all_zeros((const uint8_t *) &value, mf->n_bytes);
1832 /* Makes 'match' wildcard field 'mf'.
1834 * The caller is responsible for ensuring that 'match' meets 'mf''s
1837 mf_set_wild(const struct mf_field *mf, struct match *match)
1841 match_set_tun_id_masked(match, htonll(0), htonll(0));
1844 match_set_tun_src_masked(match, htonl(0), htonl(0));
1847 match_set_tun_dst_masked(match, htonl(0), htonl(0));
1850 match_set_tun_flags_masked(match, 0, 0);
1853 match_set_tun_tos_masked(match, 0, 0);
1856 match_set_tun_ttl_masked(match, 0, 0);
1860 match_set_metadata_masked(match, htonll(0), htonll(0));
1864 case MFF_IN_PORT_OXM:
1865 match->flow.in_port.ofp_port = 0;
1866 match->wc.masks.in_port.ofp_port = 0;
1869 case MFF_SKB_PRIORITY:
1870 match->flow.skb_priority = 0;
1871 match->wc.masks.skb_priority = 0;
1875 match->flow.pkt_mark = 0;
1876 match->wc.masks.pkt_mark = 0;
1880 match_set_reg_masked(match, mf->id - MFF_REG0, 0, 0);
1884 memset(match->flow.dl_src, 0, ETH_ADDR_LEN);
1885 memset(match->wc.masks.dl_src, 0, ETH_ADDR_LEN);
1889 memset(match->flow.dl_dst, 0, ETH_ADDR_LEN);
1890 memset(match->wc.masks.dl_dst, 0, ETH_ADDR_LEN);
1894 match->flow.dl_type = htons(0);
1895 match->wc.masks.dl_type = htons(0);
1899 match_set_dl_tci_masked(match, htons(0), htons(0));
1904 match_set_any_vid(match);
1907 case MFF_DL_VLAN_PCP:
1909 match_set_any_pcp(match);
1912 case MFF_MPLS_LABEL:
1913 match_set_any_mpls_label(match, 0);
1917 match_set_any_mpls_tc(match, 0);
1921 match_set_any_mpls_bos(match, 0);
1927 match_set_nw_src_masked(match, htonl(0), htonl(0));
1932 match_set_nw_dst_masked(match, htonl(0), htonl(0));
1936 memset(&match->wc.masks.ipv6_src, 0, sizeof match->wc.masks.ipv6_src);
1937 memset(&match->flow.ipv6_src, 0, sizeof match->flow.ipv6_src);
1941 memset(&match->wc.masks.ipv6_dst, 0, sizeof match->wc.masks.ipv6_dst);
1942 memset(&match->flow.ipv6_dst, 0, sizeof match->flow.ipv6_dst);
1945 case MFF_IPV6_LABEL:
1946 match->wc.masks.ipv6_label = htonl(0);
1947 match->flow.ipv6_label = htonl(0);
1951 match->wc.masks.nw_proto = 0;
1952 match->flow.nw_proto = 0;
1956 case MFF_IP_DSCP_SHIFTED:
1957 match->wc.masks.nw_tos &= ~IP_DSCP_MASK;
1958 match->flow.nw_tos &= ~IP_DSCP_MASK;
1962 match->wc.masks.nw_tos &= ~IP_ECN_MASK;
1963 match->flow.nw_tos &= ~IP_ECN_MASK;
1967 match->wc.masks.nw_ttl = 0;
1968 match->flow.nw_ttl = 0;
1972 match->wc.masks.nw_frag |= FLOW_NW_FRAG_MASK;
1973 match->flow.nw_frag &= ~FLOW_NW_FRAG_MASK;
1977 match->wc.masks.nw_proto = 0;
1978 match->flow.nw_proto = 0;
1983 memset(match->flow.arp_sha, 0, ETH_ADDR_LEN);
1984 memset(match->wc.masks.arp_sha, 0, ETH_ADDR_LEN);
1989 memset(match->flow.arp_tha, 0, ETH_ADDR_LEN);
1990 memset(match->wc.masks.arp_tha, 0, ETH_ADDR_LEN);
1996 case MFF_ICMPV4_TYPE:
1997 case MFF_ICMPV6_TYPE:
1998 match->wc.masks.tp_src = htons(0);
1999 match->flow.tp_src = htons(0);
2005 case MFF_ICMPV4_CODE:
2006 case MFF_ICMPV6_CODE:
2007 match->wc.masks.tp_dst = htons(0);
2008 match->flow.tp_dst = htons(0);
2012 match->wc.masks.tcp_flags = htons(0);
2013 match->flow.tcp_flags = htons(0);
2017 memset(&match->wc.masks.nd_target, 0,
2018 sizeof match->wc.masks.nd_target);
2019 memset(&match->flow.nd_target, 0, sizeof match->flow.nd_target);
2028 /* Makes 'match' match field 'mf' with the specified 'value' and 'mask'.
2029 * 'value' specifies a value to match and 'mask' specifies a wildcard pattern,
2030 * with a 1-bit indicating that the corresponding value bit must match and a
2031 * 0-bit indicating a don't-care.
2033 * If 'mask' is NULL or points to all-1-bits, then this call is equivalent to
2034 * mf_set_value(mf, value, match). If 'mask' points to all-0-bits, then this
2035 * call is equivalent to mf_set_wild(mf, match).
2037 * 'mask' must be a valid mask for 'mf' (see mf_is_mask_valid()). The caller
2038 * is responsible for ensuring that 'match' meets 'mf''s prerequisites. */
2039 enum ofputil_protocol
2040 mf_set(const struct mf_field *mf,
2041 const union mf_value *value, const union mf_value *mask,
2042 struct match *match)
2044 if (!mask || is_all_ones((const uint8_t *) mask, mf->n_bytes)) {
2045 mf_set_value(mf, value, match);
2046 return mf->usable_protocols;
2047 } else if (is_all_zeros((const uint8_t *) mask, mf->n_bytes)) {
2048 mf_set_wild(mf, match);
2049 return OFPUTIL_P_ANY;
2054 case MFF_IN_PORT_OXM:
2055 case MFF_SKB_PRIORITY:
2058 case MFF_DL_VLAN_PCP:
2060 case MFF_MPLS_LABEL:
2066 case MFF_IP_DSCP_SHIFTED:
2069 case MFF_ICMPV4_TYPE:
2070 case MFF_ICMPV4_CODE:
2071 case MFF_ICMPV6_TYPE:
2072 case MFF_ICMPV6_CODE:
2073 return OFPUTIL_P_NONE;
2076 match_set_tun_id_masked(match, value->be64, mask->be64);
2079 match_set_tun_src_masked(match, value->be32, mask->be32);
2082 match_set_tun_dst_masked(match, value->be32, mask->be32);
2085 match_set_tun_flags_masked(match, ntohs(value->be16), ntohs(mask->be16));
2088 match_set_tun_ttl_masked(match, value->u8, mask->u8);
2091 match_set_tun_tos_masked(match, value->u8, mask->u8);
2095 match_set_metadata_masked(match, value->be64, mask->be64);
2099 match_set_reg_masked(match, mf->id - MFF_REG0,
2100 ntohl(value->be32), ntohl(mask->be32));
2104 match_set_pkt_mark_masked(match, ntohl(value->be32),
2109 match_set_dl_dst_masked(match, value->mac, mask->mac);
2113 match_set_dl_src_masked(match, value->mac, mask->mac);
2118 match_set_arp_sha_masked(match, value->mac, mask->mac);
2123 match_set_arp_tha_masked(match, value->mac, mask->mac);
2127 match_set_dl_tci_masked(match, value->be16, mask->be16);
2131 match_set_vlan_vid_masked(match, value->be16, mask->be16);
2135 match_set_nw_src_masked(match, value->be32, mask->be32);
2139 match_set_nw_dst_masked(match, value->be32, mask->be32);
2143 match_set_ipv6_src_masked(match, &value->ipv6, &mask->ipv6);
2147 match_set_ipv6_dst_masked(match, &value->ipv6, &mask->ipv6);
2150 case MFF_IPV6_LABEL:
2151 if ((mask->be32 & htonl(IPV6_LABEL_MASK)) == htonl(IPV6_LABEL_MASK)) {
2152 mf_set_value(mf, value, match);
2154 match_set_ipv6_label_masked(match, value->be32, mask->be32);
2159 match_set_nd_target_masked(match, &value->ipv6, &mask->ipv6);
2163 match_set_nw_frag_masked(match, value->u8, mask->u8);
2167 match_set_nw_src_masked(match, value->be32, mask->be32);
2171 match_set_nw_dst_masked(match, value->be32, mask->be32);
2177 match_set_tp_src_masked(match, value->be16, mask->be16);
2183 match_set_tp_dst_masked(match, value->be16, mask->be16);
2187 match_set_tcp_flags_masked(match, value->be16, mask->be16);
2195 return mf->usable_protocols_bitwise;
2198 return ip_is_cidr(mask->be32) ? mf->usable_protocols :
2199 mf->usable_protocols_bitwise;
2203 mf_check__(const struct mf_subfield *sf, const struct flow *flow,
2207 VLOG_WARN_RL(&rl, "unknown %s field", type);
2208 return OFPERR_OFPBAC_BAD_SET_TYPE;
2209 } else if (!sf->n_bits) {
2210 VLOG_WARN_RL(&rl, "zero bit %s field %s", type, sf->field->name);
2211 return OFPERR_OFPBAC_BAD_SET_LEN;
2212 } else if (sf->ofs >= sf->field->n_bits) {
2213 VLOG_WARN_RL(&rl, "bit offset %d exceeds %d-bit width of %s field %s",
2214 sf->ofs, sf->field->n_bits, type, sf->field->name);
2215 return OFPERR_OFPBAC_BAD_SET_LEN;
2216 } else if (sf->ofs + sf->n_bits > sf->field->n_bits) {
2217 VLOG_WARN_RL(&rl, "bit offset %d and width %d exceeds %d-bit width "
2218 "of %s field %s", sf->ofs, sf->n_bits,
2219 sf->field->n_bits, type, sf->field->name);
2220 return OFPERR_OFPBAC_BAD_SET_LEN;
2221 } else if (flow && !mf_are_prereqs_ok(sf->field, flow)) {
2222 VLOG_WARN_RL(&rl, "%s field %s lacks correct prerequisites",
2223 type, sf->field->name);
2224 return OFPERR_OFPBAC_MATCH_INCONSISTENT;
2230 /* Checks whether 'sf' is valid for reading a subfield out of 'flow'. Returns
2231 * 0 if so, otherwise an OpenFlow error code (e.g. as returned by
2234 mf_check_src(const struct mf_subfield *sf, const struct flow *flow)
2236 return mf_check__(sf, flow, "source");
2239 /* Checks whether 'sf' is valid for writing a subfield into 'flow'. Returns 0
2240 * if so, otherwise an OpenFlow error code (e.g. as returned by
2243 mf_check_dst(const struct mf_subfield *sf, const struct flow *flow)
2245 int error = mf_check__(sf, flow, "destination");
2246 if (!error && !sf->field->writable) {
2247 VLOG_WARN_RL(&rl, "destination field %s is not writable",
2249 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
2254 /* Copies the value and wildcard bit pattern for 'mf' from 'match' into the
2255 * 'value' and 'mask', respectively. */
2257 mf_get(const struct mf_field *mf, const struct match *match,
2258 union mf_value *value, union mf_value *mask)
2260 mf_get_value(mf, &match->flow, value);
2261 mf_get_mask(mf, &match->wc, mask);
2265 mf_from_integer_string(const struct mf_field *mf, const char *s,
2266 uint8_t *valuep, uint8_t *maskp)
2268 unsigned long long int integer, mask;
2273 integer = strtoull(s, &tail, 0);
2274 if (errno || (*tail != '\0' && *tail != '/')) {
2279 mask = strtoull(tail + 1, &tail, 0);
2280 if (errno || *tail != '\0') {
2287 for (i = mf->n_bytes - 1; i >= 0; i--) {
2288 valuep[i] = integer;
2294 return xasprintf("%s: value too large for %u-byte field %s",
2295 s, mf->n_bytes, mf->name);
2300 return xasprintf("%s: bad syntax for %s", s, mf->name);
2304 mf_from_ethernet_string(const struct mf_field *mf, const char *s,
2305 uint8_t mac[ETH_ADDR_LEN],
2306 uint8_t mask[ETH_ADDR_LEN])
2310 ovs_assert(mf->n_bytes == ETH_ADDR_LEN);
2313 if (ovs_scan(s, ETH_ADDR_SCAN_FMT"%n", ETH_ADDR_SCAN_ARGS(mac), &n)
2314 && n == strlen(s)) {
2315 memset(mask, 0xff, ETH_ADDR_LEN);
2320 if (ovs_scan(s, ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT"%n",
2321 ETH_ADDR_SCAN_ARGS(mac), ETH_ADDR_SCAN_ARGS(mask), &n)
2322 && n == strlen(s)) {
2326 return xasprintf("%s: invalid Ethernet address", s);
2330 mf_from_ipv4_string(const struct mf_field *mf, const char *s,
2331 ovs_be32 *ip, ovs_be32 *mask)
2335 ovs_assert(mf->n_bytes == sizeof *ip);
2337 if (ovs_scan(s, IP_SCAN_FMT"/"IP_SCAN_FMT,
2338 IP_SCAN_ARGS(ip), IP_SCAN_ARGS(mask))) {
2340 } else if (ovs_scan(s, IP_SCAN_FMT"/%d", IP_SCAN_ARGS(ip), &prefix)) {
2341 if (prefix <= 0 || prefix > 32) {
2342 return xasprintf("%s: network prefix bits not between 1 and "
2344 } else if (prefix == 32) {
2345 *mask = OVS_BE32_MAX;
2347 *mask = htonl(((1u << prefix) - 1) << (32 - prefix));
2349 } else if (ovs_scan(s, IP_SCAN_FMT, IP_SCAN_ARGS(ip))) {
2350 *mask = OVS_BE32_MAX;
2352 return xasprintf("%s: invalid IP address", s);
2358 mf_from_ipv6_string(const struct mf_field *mf, const char *s,
2359 struct in6_addr *value, struct in6_addr *mask)
2361 char *str = xstrdup(s);
2362 char *save_ptr = NULL;
2363 const char *name, *netmask;
2366 ovs_assert(mf->n_bytes == sizeof *value);
2368 name = strtok_r(str, "/", &save_ptr);
2369 retval = name ? lookup_ipv6(name, value) : EINVAL;
2373 err = xasprintf("%s: could not convert to IPv6 address", str);
2379 netmask = strtok_r(NULL, "/", &save_ptr);
2381 if (inet_pton(AF_INET6, netmask, mask) != 1) {
2382 int prefix = atoi(netmask);
2383 if (prefix <= 0 || prefix > 128) {
2385 return xasprintf("%s: prefix bits not between 1 and 128", s);
2387 *mask = ipv6_create_mask(prefix);
2391 *mask = in6addr_exact;
2399 mf_from_ofp_port_string(const struct mf_field *mf, const char *s,
2400 ovs_be16 *valuep, ovs_be16 *maskp)
2404 ovs_assert(mf->n_bytes == sizeof(ovs_be16));
2406 if (ofputil_port_from_string(s, &port)) {
2407 *valuep = htons(ofp_to_u16(port));
2408 *maskp = OVS_BE16_MAX;
2411 return xasprintf("%s: port value out of range for %s", s, mf->name);
2415 mf_from_ofp_port_string32(const struct mf_field *mf, const char *s,
2416 ovs_be32 *valuep, ovs_be32 *maskp)
2420 ovs_assert(mf->n_bytes == sizeof(ovs_be32));
2421 if (ofputil_port_from_string(s, &port)) {
2422 *valuep = ofputil_port_to_ofp11(port);
2423 *maskp = OVS_BE32_MAX;
2426 return xasprintf("%s: port value out of range for %s", s, mf->name);
2429 struct frag_handling {
2435 static const struct frag_handling all_frags[] = {
2436 #define A FLOW_NW_FRAG_ANY
2437 #define L FLOW_NW_FRAG_LATER
2438 /* name mask value */
2441 { "first", A|L, A },
2442 { "later", A|L, A|L },
2447 { "not_later", L, 0 },
2454 mf_from_frag_string(const char *s, uint8_t *valuep, uint8_t *maskp)
2456 const struct frag_handling *h;
2458 for (h = all_frags; h < &all_frags[ARRAY_SIZE(all_frags)]; h++) {
2459 if (!strcasecmp(s, h->name)) {
2460 /* We force the upper bits of the mask on to make mf_parse_value()
2461 * happy (otherwise it will never think it's an exact match.) */
2462 *maskp = h->mask | ~FLOW_NW_FRAG_MASK;
2468 return xasprintf("%s: unknown fragment type (valid types are \"no\", "
2469 "\"yes\", \"first\", \"later\", \"not_first\"", s);
2473 parse_flow_tun_flags(const char *s_, const char *(*bit_to_string)(uint32_t),
2476 uint32_t result = 0;
2477 char *save_ptr = NULL;
2480 char *s = xstrdup(s_);
2482 for (name = strtok_r((char *)s, " |", &save_ptr); name;
2483 name = strtok_r(NULL, " |", &save_ptr)) {
2485 unsigned long long int flags;
2488 if (ovs_scan(name, "%lli", &flags)) {
2492 name_len = strlen(name);
2493 for (bit = 1; bit; bit <<= 1) {
2494 const char *fname = bit_to_string(bit);
2501 len = strlen(fname);
2502 if (len != name_len) {
2505 if (!strncmp(name, fname, len)) {
2517 *res = htons(result);
2524 mf_from_tun_flags_string(const char *s, ovs_be16 *valuep, ovs_be16 *maskp)
2526 if (!parse_flow_tun_flags(s, flow_tun_flag_to_string, valuep)) {
2527 *maskp = OVS_BE16_MAX;
2531 return xasprintf("%s: unknown tunnel flags (valid flags are \"df\", "
2532 "\"csum\", \"key\")", s);
2536 mf_from_tcp_flags_string(const char *s, ovs_be16 *flagsp, ovs_be16 *maskp)
2543 if (ovs_scan(s, "%"SCNi16"/%"SCNi16"%n", &flags, &mask, &n) && !s[n]) {
2544 *flagsp = htons(flags);
2545 *maskp = htons(mask);
2548 if (ovs_scan(s, "%"SCNi16"%n", &flags, &n) && !s[n]) {
2549 *flagsp = htons(flags);
2550 *maskp = OVS_BE16_MAX;
2554 while (*s != '\0') {
2566 return xasprintf("%s: TCP flag must be preceded by '+' (for SET) "
2567 "or '-' (NOT SET)", s);
2571 name_len = strcspn(s,"+-");
2573 for (bit = 1; bit; bit <<= 1) {
2574 const char *fname = packet_tcp_flag_to_string(bit);
2581 len = strlen(fname);
2582 if (len != name_len) {
2585 if (!strncmp(s, fname, len)) {
2587 return xasprintf("%s: Each TCP flag can be specified only "
2599 return xasprintf("%s: unknown TCP flag(s)", s);
2604 *flagsp = htons(flags);
2605 *maskp = htons(mask);
2610 /* Parses 's', a string value for field 'mf', into 'value' and 'mask'. Returns
2611 * NULL if successful, otherwise a malloc()'d string describing the error. */
2613 mf_parse(const struct mf_field *mf, const char *s,
2614 union mf_value *value, union mf_value *mask)
2618 if (!strcmp(s, "*")) {
2619 memset(value, 0, mf->n_bytes);
2620 memset(mask, 0, mf->n_bytes);
2624 switch (mf->string) {
2626 case MFS_HEXADECIMAL:
2627 error = mf_from_integer_string(mf, s,
2628 (uint8_t *) value, (uint8_t *) mask);
2632 error = mf_from_ethernet_string(mf, s, value->mac, mask->mac);
2636 error = mf_from_ipv4_string(mf, s, &value->be32, &mask->be32);
2640 error = mf_from_ipv6_string(mf, s, &value->ipv6, &mask->ipv6);
2644 error = mf_from_ofp_port_string(mf, s, &value->be16, &mask->be16);
2647 case MFS_OFP_PORT_OXM:
2648 error = mf_from_ofp_port_string32(mf, s, &value->be32, &mask->be32);
2652 error = mf_from_frag_string(s, &value->u8, &mask->u8);
2656 ovs_assert(mf->n_bytes == sizeof(ovs_be16));
2657 error = mf_from_tun_flags_string(s, &value->be16, &mask->be16);
2661 ovs_assert(mf->n_bytes == sizeof(ovs_be16));
2662 error = mf_from_tcp_flags_string(s, &value->be16, &mask->be16);
2669 if (!error && !mf_is_mask_valid(mf, mask)) {
2670 error = xasprintf("%s: invalid mask for field %s", s, mf->name);
2675 /* Parses 's', a string value for field 'mf', into 'value'. Returns NULL if
2676 * successful, otherwise a malloc()'d string describing the error. */
2678 mf_parse_value(const struct mf_field *mf, const char *s, union mf_value *value)
2680 union mf_value mask;
2683 error = mf_parse(mf, s, value, &mask);
2688 if (!is_all_ones((const uint8_t *) &mask, mf->n_bytes)) {
2689 return xasprintf("%s: wildcards not allowed here", s);
2695 mf_format_integer_string(const struct mf_field *mf, const uint8_t *valuep,
2696 const uint8_t *maskp, struct ds *s)
2698 unsigned long long int integer;
2701 ovs_assert(mf->n_bytes <= 8);
2704 for (i = 0; i < mf->n_bytes; i++) {
2705 integer = (integer << 8) | valuep[i];
2707 if (mf->string == MFS_HEXADECIMAL) {
2708 ds_put_format(s, "%#llx", integer);
2710 ds_put_format(s, "%lld", integer);
2714 unsigned long long int mask;
2717 for (i = 0; i < mf->n_bytes; i++) {
2718 mask = (mask << 8) | maskp[i];
2721 /* I guess we could write the mask in decimal for MFS_DECIMAL but I'm
2722 * not sure that that a bit-mask written in decimal is ever easier to
2723 * understand than the same bit-mask written in hexadecimal. */
2724 ds_put_format(s, "/%#llx", mask);
2729 mf_format_frag_string(uint8_t value, uint8_t mask, struct ds *s)
2731 const struct frag_handling *h;
2733 mask &= FLOW_NW_FRAG_MASK;
2736 for (h = all_frags; h < &all_frags[ARRAY_SIZE(all_frags)]; h++) {
2737 if (value == h->value && mask == h->mask) {
2738 ds_put_cstr(s, h->name);
2742 ds_put_cstr(s, "<error>");
2746 mf_format_tnl_flags_string(const ovs_be16 *valuep, struct ds *s)
2748 format_flags(s, flow_tun_flag_to_string, ntohs(*valuep), '|');
2752 mf_format_tcp_flags_string(ovs_be16 value, ovs_be16 mask, struct ds *s)
2754 format_flags_masked(s, NULL, packet_tcp_flag_to_string, ntohs(value),
2758 /* Appends to 's' a string representation of field 'mf' whose value is in
2759 * 'value' and 'mask'. 'mask' may be NULL to indicate an exact match. */
2761 mf_format(const struct mf_field *mf,
2762 const union mf_value *value, const union mf_value *mask,
2766 if (is_all_zeros((const uint8_t *) mask, mf->n_bytes)) {
2767 ds_put_cstr(s, "ANY");
2769 } else if (is_all_ones((const uint8_t *) mask, mf->n_bytes)) {
2774 switch (mf->string) {
2775 case MFS_OFP_PORT_OXM:
2778 ofputil_port_from_ofp11(value->be32, &port);
2779 ofputil_format_port(port, s);
2785 ofputil_format_port(u16_to_ofp(ntohs(value->be16)), s);
2790 case MFS_HEXADECIMAL:
2791 mf_format_integer_string(mf, (uint8_t *) value, (uint8_t *) mask, s);
2795 eth_format_masked(value->mac, mask->mac, s);
2799 ip_format_masked(value->be32, mask ? mask->be32 : OVS_BE32_MAX, s);
2803 print_ipv6_masked(s, &value->ipv6, mask ? &mask->ipv6 : NULL);
2807 mf_format_frag_string(value->u8, mask ? mask->u8 : UINT8_MAX, s);
2811 mf_format_tnl_flags_string(&value->be16, s);
2815 mf_format_tcp_flags_string(value->be16,
2816 mask ? mask->be16 : OVS_BE16_MAX, s);
2824 /* Makes subfield 'sf' within 'flow' exactly match the 'sf->n_bits'
2825 * least-significant bits in 'x'.
2828 mf_write_subfield_flow(const struct mf_subfield *sf,
2829 const union mf_subvalue *x, struct flow *flow)
2831 const struct mf_field *field = sf->field;
2832 union mf_value value;
2834 mf_get_value(field, flow, &value);
2835 bitwise_copy(x, sizeof *x, 0, &value, field->n_bytes,
2836 sf->ofs, sf->n_bits);
2837 mf_set_flow_value(field, &value, flow);
2840 /* Makes subfield 'sf' within 'match' exactly match the 'sf->n_bits'
2841 * least-significant bits in 'x'.
2844 mf_write_subfield(const struct mf_subfield *sf, const union mf_subvalue *x,
2845 struct match *match)
2847 const struct mf_field *field = sf->field;
2848 union mf_value value, mask;
2850 mf_get(field, match, &value, &mask);
2851 bitwise_copy(x, sizeof *x, 0, &value, field->n_bytes, sf->ofs, sf->n_bits);
2852 bitwise_one ( &mask, field->n_bytes, sf->ofs, sf->n_bits);
2853 mf_set(field, &value, &mask, match);
2856 /* Initializes 'x' to the value of 'sf' within 'flow'. 'sf' must be valid for
2857 * reading 'flow', e.g. as checked by mf_check_src(). */
2859 mf_read_subfield(const struct mf_subfield *sf, const struct flow *flow,
2860 union mf_subvalue *x)
2862 union mf_value value;
2864 mf_get_value(sf->field, flow, &value);
2866 memset(x, 0, sizeof *x);
2867 bitwise_copy(&value, sf->field->n_bytes, sf->ofs,
2872 /* Returns the value of 'sf' within 'flow'. 'sf' must be valid for reading
2873 * 'flow', e.g. as checked by mf_check_src() and sf->n_bits must be 64 or
2876 mf_get_subfield(const struct mf_subfield *sf, const struct flow *flow)
2878 union mf_value value;
2880 mf_get_value(sf->field, flow, &value);
2881 return bitwise_get(&value, sf->field->n_bytes, sf->ofs, sf->n_bits);
2884 /* Formats 'sf' into 's' in a format normally acceptable to
2885 * mf_parse_subfield(). (It won't be acceptable if sf->field is NULL or if
2886 * sf->field has no NXM name.) */
2888 mf_format_subfield(const struct mf_subfield *sf, struct ds *s)
2891 ds_put_cstr(s, "<unknown>");
2892 } else if (sf->field->nxm_name) {
2893 ds_put_cstr(s, sf->field->nxm_name);
2894 } else if (sf->field->nxm_header) {
2895 uint32_t header = sf->field->nxm_header;
2896 ds_put_format(s, "%d:%d", NXM_VENDOR(header), NXM_FIELD(header));
2898 ds_put_cstr(s, sf->field->name);
2901 if (sf->field && sf->ofs == 0 && sf->n_bits == sf->field->n_bits) {
2902 ds_put_cstr(s, "[]");
2903 } else if (sf->n_bits == 1) {
2904 ds_put_format(s, "[%d]", sf->ofs);
2906 ds_put_format(s, "[%d..%d]", sf->ofs, sf->ofs + sf->n_bits - 1);
2910 static const struct mf_field *
2911 mf_parse_subfield_name(const char *name, int name_len, bool *wild)
2915 *wild = name_len > 2 && !memcmp(&name[name_len - 2], "_W", 2);
2920 for (i = 0; i < MFF_N_IDS; i++) {
2921 const struct mf_field *mf = mf_from_id(i);
2924 && !strncmp(mf->nxm_name, name, name_len)
2925 && mf->nxm_name[name_len] == '\0') {
2929 && !strncmp(mf->oxm_name, name, name_len)
2930 && mf->oxm_name[name_len] == '\0') {
2938 /* Parses a subfield from the beginning of '*sp' into 'sf'. If successful,
2939 * returns NULL and advances '*sp' to the first byte following the parsed
2940 * string. On failure, returns a malloc()'d error message, does not modify
2941 * '*sp', and does not properly initialize 'sf'.
2943 * The syntax parsed from '*sp' takes the form "header[start..end]" where
2944 * 'header' is the name of an NXM field and 'start' and 'end' are (inclusive)
2945 * bit indexes. "..end" may be omitted to indicate a single bit. "start..end"
2946 * may both be omitted (the [] are still required) to indicate an entire
2948 char * WARN_UNUSED_RESULT
2949 mf_parse_subfield__(struct mf_subfield *sf, const char **sp)
2951 const struct mf_field *field;
2960 name_len = strcspn(s, "[");
2961 if (s[name_len] != '[') {
2962 return xasprintf("%s: missing [ looking for field name", *sp);
2965 field = mf_parse_subfield_name(name, name_len, &wild);
2967 return xasprintf("%s: unknown field `%.*s'", *sp, name_len, s);
2971 if (ovs_scan(s, "[%d..%d]", &start, &end)) {
2972 /* Nothing to do. */
2973 } else if (ovs_scan(s, "[%d]", &start)) {
2975 } else if (!strncmp(s, "[]", 2)) {
2977 end = field->n_bits - 1;
2979 return xasprintf("%s: syntax error expecting [] or [<bit>] or "
2980 "[<start>..<end>]", *sp);
2982 s = strchr(s, ']') + 1;
2985 return xasprintf("%s: starting bit %d is after ending bit %d",
2987 } else if (start >= field->n_bits) {
2988 return xasprintf("%s: starting bit %d is not valid because field is "
2989 "only %d bits wide", *sp, start, field->n_bits);
2990 } else if (end >= field->n_bits){
2991 return xasprintf("%s: ending bit %d is not valid because field is "
2992 "only %d bits wide", *sp, end, field->n_bits);
2997 sf->n_bits = end - start + 1;
3003 /* Parses a subfield from the entirety of 's' into 'sf'. Returns NULL if
3004 * successful, otherwise a malloc()'d string describing the error. The caller
3005 * is responsible for freeing the returned string.
3007 * The syntax parsed from 's' takes the form "header[start..end]" where
3008 * 'header' is the name of an NXM field and 'start' and 'end' are (inclusive)
3009 * bit indexes. "..end" may be omitted to indicate a single bit. "start..end"
3010 * may both be omitted (the [] are still required) to indicate an entire
3012 char * WARN_UNUSED_RESULT
3013 mf_parse_subfield(struct mf_subfield *sf, const char *s)
3015 char *error = mf_parse_subfield__(sf, &s);
3016 if (!error && s[0]) {
3017 error = xstrdup("unexpected input following field syntax");
3023 mf_format_subvalue(const union mf_subvalue *subvalue, struct ds *s)
3027 for (i = 0; i < ARRAY_SIZE(subvalue->u8); i++) {
3028 if (subvalue->u8[i]) {
3029 ds_put_format(s, "0x%"PRIx8, subvalue->u8[i]);
3030 for (i++; i < ARRAY_SIZE(subvalue->u8); i++) {
3031 ds_put_format(s, "%02"PRIx8, subvalue->u8[i]);
3036 ds_put_char(s, '0');