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 MF_FIELD_SIZES(MEMBER) \
41 sizeof ((union mf_value *)0)->MEMBER, \
42 8 * sizeof ((union mf_value *)0)->MEMBER
44 static const struct mf_field mf_fields[MFF_N_IDS] = {
50 MFF_TUN_ID, "tun_id", NULL,
56 NXM_NX_TUN_ID, "NXM_NX_TUN_ID",
57 OXM_OF_TUNNEL_ID, "OXM_OF_TUNNEL_ID",
58 OFPUTIL_P_NXM_OXM_ANY,
59 OFPUTIL_P_NXM_OXM_ANY,
61 MFF_TUN_SRC, "tun_src", NULL,
67 NXM_NX_TUN_IPV4_SRC, "NXM_NX_TUN_IPV4_SRC",
68 NXM_NX_TUN_IPV4_SRC, "NXM_NX_TUN_IPV4_SRC",
69 OFPUTIL_P_NXM_OXM_ANY,
70 OFPUTIL_P_NXM_OXM_ANY,
72 MFF_TUN_DST, "tun_dst", NULL,
78 NXM_NX_TUN_IPV4_DST, "NXM_NX_TUN_IPV4_DST",
79 NXM_NX_TUN_IPV4_DST, "NXM_NX_TUN_IPV4_DST",
80 OFPUTIL_P_NXM_OXM_ANY,
81 OFPUTIL_P_NXM_OXM_ANY,
83 MFF_TUN_FLAGS, "tun_flags", NULL,
94 MFF_TUN_TTL, "tun_ttl", NULL,
105 MFF_TUN_TOS, "tun_tos", NULL,
116 MFF_METADATA, "metadata", NULL,
117 MF_FIELD_SIZES(be64),
122 OXM_OF_METADATA, "OXM_OF_METADATA",
123 OXM_OF_METADATA, "OXM_OF_METADATA",
124 OFPUTIL_P_NXM_OF11_UP,
125 OFPUTIL_P_NXM_OF11_UP,
127 MFF_IN_PORT, "in_port", NULL,
128 MF_FIELD_SIZES(be16),
133 NXM_OF_IN_PORT, "NXM_OF_IN_PORT",
134 NXM_OF_IN_PORT, "NXM_OF_IN_PORT",
135 OFPUTIL_P_ANY, /* OF11+ via mapping to 32 bits. */
138 MFF_IN_PORT_OXM, "in_port_oxm", NULL,
139 MF_FIELD_SIZES(be32),
144 OXM_OF_IN_PORT, "OXM_OF_IN_PORT",
145 OXM_OF_IN_PORT, "OXM_OF_IN_PORT",
149 MFF_SKB_PRIORITY, "skb_priority", NULL,
150 MF_FIELD_SIZES(be32),
160 MFF_PKT_MARK, "pkt_mark", NULL,
161 MF_FIELD_SIZES(be32),
166 NXM_NX_PKT_MARK, "NXM_NX_PKT_MARK",
167 NXM_NX_PKT_MARK, "NXM_NX_PKT_MARK",
168 OFPUTIL_P_NXM_OXM_ANY,
169 OFPUTIL_P_NXM_OXM_ANY,
172 #define REGISTER(IDX) \
174 MFF_REG##IDX, "reg" #IDX, NULL, \
175 MF_FIELD_SIZES(be32), \
180 NXM_NX_REG(IDX), "NXM_NX_REG" #IDX, \
181 NXM_NX_REG(IDX), "NXM_NX_REG" #IDX, \
182 OFPUTIL_P_NXM_OXM_ANY, \
183 OFPUTIL_P_NXM_OXM_ANY, \
218 MFF_ETH_SRC, "eth_src", "dl_src",
224 NXM_OF_ETH_SRC, "NXM_OF_ETH_SRC",
225 OXM_OF_ETH_SRC, "OXM_OF_ETH_SRC",
227 OFPUTIL_P_NXM_OF11_UP, /* Bitwise masking only with NXM and OF11+! */
229 MFF_ETH_DST, "eth_dst", "dl_dst",
235 NXM_OF_ETH_DST, "NXM_OF_ETH_DST",
236 OXM_OF_ETH_DST, "OXM_OF_ETH_DST",
238 OFPUTIL_P_NXM_OF11_UP, /* Bitwise masking only with NXM and OF11+! */
240 MFF_ETH_TYPE, "eth_type", "dl_type",
241 MF_FIELD_SIZES(be16),
246 NXM_OF_ETH_TYPE, "NXM_OF_ETH_TYPE",
247 OXM_OF_ETH_TYPE, "OXM_OF_ETH_TYPE",
253 MFF_VLAN_TCI, "vlan_tci", NULL,
254 MF_FIELD_SIZES(be16),
259 NXM_OF_VLAN_TCI, "NXM_OF_VLAN_TCI",
260 NXM_OF_VLAN_TCI, "NXM_OF_VLAN_TCI",
262 OFPUTIL_P_NXM_OXM_ANY,
264 MFF_DL_VLAN, "dl_vlan", NULL,
265 sizeof(ovs_be16), 12,
273 OFPUTIL_P_NXM_OXM_ANY,
275 MFF_VLAN_VID, "vlan_vid", NULL,
276 sizeof(ovs_be16), 12,
281 OXM_OF_VLAN_VID, "OXM_OF_VLAN_VID",
282 OXM_OF_VLAN_VID, "OXM_OF_VLAN_VID",
284 OFPUTIL_P_NXM_OXM_ANY,
286 MFF_DL_VLAN_PCP, "dl_vlan_pcp", NULL,
294 OFPUTIL_P_ANY, /* Will be mapped to NXM and OXM. */
297 MFF_VLAN_PCP, "vlan_pcp", NULL,
303 OXM_OF_VLAN_PCP, "OXM_OF_VLAN_PCP",
304 OXM_OF_VLAN_PCP, "OXM_OF_VLAN_PCP",
305 OFPUTIL_P_ANY, /* Will be mapped to OF10 and NXM. */
313 MFF_MPLS_LABEL, "mpls_label", NULL,
319 OXM_OF_MPLS_LABEL, "OXM_OF_MPLS_LABEL",
320 OXM_OF_MPLS_LABEL, "OXM_OF_MPLS_LABEL",
321 OFPUTIL_P_NXM_OF11_UP,
324 MFF_MPLS_TC, "mpls_tc", NULL,
330 OXM_OF_MPLS_TC, "OXM_OF_MPLS_TC",
331 OXM_OF_MPLS_TC, "OXM_OF_MPLS_TC",
332 OFPUTIL_P_NXM_OF11_UP,
335 MFF_MPLS_BOS, "mpls_bos", NULL,
341 OXM_OF_MPLS_BOS, "OXM_OF_MPLS_BOS",
342 OXM_OF_MPLS_BOS, "OXM_OF_MPLS_BOS",
343 OFPUTIL_P_NXM_OXM_ANY,
352 MFF_IPV4_SRC, "ip_src", "nw_src",
353 MF_FIELD_SIZES(be32),
358 NXM_OF_IP_SRC, "NXM_OF_IP_SRC",
359 OXM_OF_IPV4_SRC, "OXM_OF_IPV4_SRC",
361 OFPUTIL_P_NXM_OF11_UP,
363 MFF_IPV4_DST, "ip_dst", "nw_dst",
364 MF_FIELD_SIZES(be32),
369 NXM_OF_IP_DST, "NXM_OF_IP_DST",
370 OXM_OF_IPV4_DST, "OXM_OF_IPV4_DST",
372 OFPUTIL_P_NXM_OF11_UP,
376 MFF_IPV6_SRC, "ipv6_src", NULL,
377 MF_FIELD_SIZES(ipv6),
382 NXM_NX_IPV6_SRC, "NXM_NX_IPV6_SRC",
383 OXM_OF_IPV6_SRC, "OXM_OF_IPV6_SRC",
384 OFPUTIL_P_NXM_OXM_ANY,
385 OFPUTIL_P_NXM_OXM_ANY,
387 MFF_IPV6_DST, "ipv6_dst", NULL,
388 MF_FIELD_SIZES(ipv6),
393 NXM_NX_IPV6_DST, "NXM_NX_IPV6_DST",
394 OXM_OF_IPV6_DST, "OXM_OF_IPV6_DST",
395 OFPUTIL_P_NXM_OXM_ANY,
396 OFPUTIL_P_NXM_OXM_ANY,
399 MFF_IPV6_LABEL, "ipv6_label", NULL,
405 NXM_NX_IPV6_LABEL, "NXM_NX_IPV6_LABEL",
406 OXM_OF_IPV6_FLABEL, "OXM_OF_IPV6_FLABEL",
407 OFPUTIL_P_NXM_OXM_ANY,
408 OFPUTIL_P_NXM_OXM_ANY,
412 MFF_IP_PROTO, "nw_proto", NULL,
418 NXM_OF_IP_PROTO, "NXM_OF_IP_PROTO",
419 OXM_OF_IP_PROTO, "OXM_OF_IP_PROTO",
423 MFF_IP_DSCP, "nw_tos", NULL,
429 NXM_OF_IP_TOS, "NXM_OF_IP_TOS",
430 NXM_OF_IP_TOS, "NXM_OF_IP_TOS",
431 OFPUTIL_P_ANY, /* Will be shifted for OXM. */
434 MFF_IP_DSCP_SHIFTED, "nw_tos_shifted", NULL,
440 OXM_OF_IP_DSCP, "OXM_OF_IP_DSCP",
441 OXM_OF_IP_DSCP, "OXM_OF_IP_DSCP",
442 OFPUTIL_P_ANY, /* Will be shifted for non-OXM. */
445 MFF_IP_ECN, "nw_ecn", NULL,
451 NXM_NX_IP_ECN, "NXM_NX_IP_ECN",
452 OXM_OF_IP_ECN, "OXM_OF_IP_ECN",
453 OFPUTIL_P_NXM_OXM_ANY,
456 MFF_IP_TTL, "nw_ttl", NULL,
462 NXM_NX_IP_TTL, "NXM_NX_IP_TTL",
463 NXM_NX_IP_TTL, "NXM_NX_IP_TTL",
464 OFPUTIL_P_NXM_OXM_ANY,
467 MFF_IP_FRAG, "ip_frag", NULL,
473 NXM_NX_IP_FRAG, "NXM_NX_IP_FRAG",
474 NXM_NX_IP_FRAG, "NXM_NX_IP_FRAG",
475 OFPUTIL_P_NXM_OXM_ANY,
476 OFPUTIL_P_NXM_OXM_ANY,
480 MFF_ARP_OP, "arp_op", NULL,
481 MF_FIELD_SIZES(be16),
486 NXM_OF_ARP_OP, "NXM_OF_ARP_OP",
487 OXM_OF_ARP_OP, "OXM_OF_ARP_OP",
491 MFF_ARP_SPA, "arp_spa", NULL,
492 MF_FIELD_SIZES(be32),
497 NXM_OF_ARP_SPA, "NXM_OF_ARP_SPA",
498 OXM_OF_ARP_SPA, "OXM_OF_ARP_SPA",
500 OFPUTIL_P_NXM_OF11_UP,
502 MFF_ARP_TPA, "arp_tpa", NULL,
503 MF_FIELD_SIZES(be32),
508 NXM_OF_ARP_TPA, "NXM_OF_ARP_TPA",
509 OXM_OF_ARP_TPA, "OXM_OF_ARP_TPA",
511 OFPUTIL_P_NXM_OF11_UP,
513 MFF_ARP_SHA, "arp_sha", NULL,
519 NXM_NX_ARP_SHA, "NXM_NX_ARP_SHA",
520 OXM_OF_ARP_SHA, "OXM_OF_ARP_SHA",
521 OFPUTIL_P_NXM_OXM_ANY,
522 OFPUTIL_P_NXM_OXM_ANY,
524 MFF_ARP_THA, "arp_tha", NULL,
530 NXM_NX_ARP_THA, "NXM_NX_ARP_THA",
531 OXM_OF_ARP_THA, "OXM_OF_ARP_THA",
532 OFPUTIL_P_NXM_OXM_ANY,
533 OFPUTIL_P_NXM_OXM_ANY,
541 MFF_TCP_SRC, "tcp_src", "tp_src",
542 MF_FIELD_SIZES(be16),
547 NXM_OF_TCP_SRC, "NXM_OF_TCP_SRC",
548 OXM_OF_TCP_SRC, "OXM_OF_TCP_SRC",
550 OFPUTIL_P_NXM_OXM_ANY,
552 MFF_TCP_DST, "tcp_dst", "tp_dst",
553 MF_FIELD_SIZES(be16),
558 NXM_OF_TCP_DST, "NXM_OF_TCP_DST",
559 OXM_OF_TCP_DST, "OXM_OF_TCP_DST",
561 OFPUTIL_P_NXM_OXM_ANY,
565 MFF_UDP_SRC, "udp_src", NULL,
566 MF_FIELD_SIZES(be16),
571 NXM_OF_UDP_SRC, "NXM_OF_UDP_SRC",
572 OXM_OF_UDP_SRC, "OXM_OF_UDP_SRC",
574 OFPUTIL_P_NXM_OXM_ANY,
576 MFF_UDP_DST, "udp_dst", NULL,
577 MF_FIELD_SIZES(be16),
582 NXM_OF_UDP_DST, "NXM_OF_UDP_DST",
583 OXM_OF_UDP_DST, "OXM_OF_UDP_DST",
585 OFPUTIL_P_NXM_OXM_ANY,
589 MFF_SCTP_SRC, "sctp_src", NULL,
590 MF_FIELD_SIZES(be16),
595 OXM_OF_SCTP_SRC, "OXM_OF_SCTP_SRC",
596 OXM_OF_SCTP_SRC, "OXM_OF_SCTP_SRC",
597 OFPUTIL_P_NXM_OF11_UP,
598 OFPUTIL_P_NXM_OXM_ANY,
600 MFF_SCTP_DST, "sctp_dst", NULL,
601 MF_FIELD_SIZES(be16),
606 OXM_OF_SCTP_DST, "OXM_OF_SCTP_DST",
607 OXM_OF_SCTP_DST, "OXM_OF_SCTP_DST",
608 OFPUTIL_P_NXM_OF11_UP,
609 OFPUTIL_P_NXM_OXM_ANY,
613 MFF_ICMPV4_TYPE, "icmp_type", NULL,
619 NXM_OF_ICMP_TYPE, "NXM_OF_ICMP_TYPE",
620 OXM_OF_ICMPV4_TYPE, "OXM_OF_ICMPV4_TYPE",
624 MFF_ICMPV4_CODE, "icmp_code", NULL,
630 NXM_OF_ICMP_CODE, "NXM_OF_ICMP_CODE",
631 OXM_OF_ICMPV4_CODE, "OXM_OF_ICMPV4_CODE",
637 MFF_ICMPV6_TYPE, "icmpv6_type", NULL,
643 NXM_NX_ICMPV6_TYPE, "NXM_NX_ICMPV6_TYPE",
644 OXM_OF_ICMPV6_TYPE, "OXM_OF_ICMPV6_TYPE",
645 OFPUTIL_P_NXM_OXM_ANY,
648 MFF_ICMPV6_CODE, "icmpv6_code", NULL,
654 NXM_NX_ICMPV6_CODE, "NXM_NX_ICMPV6_CODE",
655 OXM_OF_ICMPV6_CODE, "OXM_OF_ICMPV6_CODE",
656 OFPUTIL_P_NXM_OXM_ANY,
665 MFF_ND_TARGET, "nd_target", NULL,
666 MF_FIELD_SIZES(ipv6),
671 NXM_NX_ND_TARGET, "NXM_NX_ND_TARGET",
672 OXM_OF_IPV6_ND_TARGET, "OXM_OF_IPV6_ND_TARGET",
673 OFPUTIL_P_NXM_OXM_ANY,
674 OFPUTIL_P_NXM_OXM_ANY,
676 MFF_ND_SLL, "nd_sll", NULL,
682 NXM_NX_ND_SLL, "NXM_NX_ND_SLL",
683 OXM_OF_IPV6_ND_SLL, "OXM_OF_IPV6_ND_SLL",
684 OFPUTIL_P_NXM_OXM_ANY,
685 OFPUTIL_P_NXM_OXM_ANY,
687 MFF_ND_TLL, "nd_tll", NULL,
693 NXM_NX_ND_TLL, "NXM_NX_ND_TLL",
694 OXM_OF_IPV6_ND_TLL, "OXM_OF_IPV6_ND_TLL",
695 OFPUTIL_P_NXM_OXM_ANY,
696 OFPUTIL_P_NXM_OXM_ANY,
700 /* Maps an NXM or OXM header value to an mf_field. */
702 struct hmap_node hmap_node; /* In 'all_fields' hmap. */
703 uint32_t header; /* NXM or OXM header value. */
704 const struct mf_field *mf;
707 /* Contains 'struct nxm_field's. */
708 static struct hmap all_fields;
710 /* Maps from an mf_field's 'name' or 'extra_name' to the mf_field. */
711 static struct shash mf_by_name;
713 /* Rate limit for parse errors. These always indicate a bug in an OpenFlow
714 * controller and so there's not much point in showing a lot of them. */
715 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
717 const struct mf_field *mf_from_nxm_header__(uint32_t header);
718 static void nxm_init(void);
720 /* Returns the field with the given 'id'. */
721 const struct mf_field *
722 mf_from_id(enum mf_field_id id)
724 ovs_assert((unsigned int) id < MFF_N_IDS);
725 return &mf_fields[id];
728 /* Returns the field with the given 'name', or a null pointer if no field has
730 const struct mf_field *
731 mf_from_name(const char *name)
734 return shash_find_data(&mf_by_name, name);
738 add_nxm_field(uint32_t header, const struct mf_field *mf)
742 f = xmalloc(sizeof *f);
743 hmap_insert(&all_fields, &f->hmap_node, hash_int(header, 0));
749 nxm_init_add_field(const struct mf_field *mf, uint32_t header)
752 ovs_assert(!mf_from_nxm_header__(header));
753 add_nxm_field(header, mf);
754 if (mf->maskable != MFM_NONE) {
755 add_nxm_field(NXM_MAKE_WILD_HEADER(header), mf);
765 hmap_init(&all_fields);
766 shash_init(&mf_by_name);
767 for (i = 0; i < MFF_N_IDS; i++) {
768 const struct mf_field *mf = &mf_fields[i];
770 ovs_assert(mf->id == i); /* Fields must be in the enum order. */
772 nxm_init_add_field(mf, mf->nxm_header);
773 if (mf->oxm_header != mf->nxm_header) {
774 nxm_init_add_field(mf, mf->oxm_header);
777 shash_add_once(&mf_by_name, mf->name, mf);
778 if (mf->extra_name) {
779 shash_add_once(&mf_by_name, mf->extra_name, mf);
787 static pthread_once_t once = PTHREAD_ONCE_INIT;
788 pthread_once(&once, nxm_do_init);
791 const struct mf_field *
792 mf_from_nxm_header(uint32_t header)
795 return mf_from_nxm_header__(header);
798 const struct mf_field *
799 mf_from_nxm_header__(uint32_t header)
801 const struct nxm_field *f;
803 HMAP_FOR_EACH_IN_BUCKET (f, hmap_node, hash_int(header, 0), &all_fields) {
804 if (f->header == header) {
812 /* Returns true if 'wc' wildcards all the bits in field 'mf', false if 'wc'
813 * specifies at least one bit in the field.
815 * The caller is responsible for ensuring that 'wc' corresponds to a flow that
816 * meets 'mf''s prerequisites. */
818 mf_is_all_wild(const struct mf_field *mf, const struct flow_wildcards *wc)
822 return !wc->masks.tunnel.ip_src;
824 return !wc->masks.tunnel.ip_dst;
829 return !wc->masks.tunnel.tun_id;
831 return !wc->masks.metadata;
833 case MFF_IN_PORT_OXM:
834 return !wc->masks.in_port.ofp_port;
835 case MFF_SKB_PRIORITY:
836 return !wc->masks.skb_priority;
838 return !wc->masks.pkt_mark;
840 return !wc->masks.regs[mf->id - MFF_REG0];
843 return eth_addr_is_zero(wc->masks.dl_src);
845 return eth_addr_is_zero(wc->masks.dl_dst);
847 return !wc->masks.dl_type;
851 return eth_addr_is_zero(wc->masks.arp_sha);
855 return eth_addr_is_zero(wc->masks.arp_tha);
858 return !wc->masks.vlan_tci;
860 return !(wc->masks.vlan_tci & htons(VLAN_VID_MASK));
862 return !(wc->masks.vlan_tci & htons(VLAN_VID_MASK | VLAN_CFI));
863 case MFF_DL_VLAN_PCP:
865 return !(wc->masks.vlan_tci & htons(VLAN_PCP_MASK));
868 return !(wc->masks.mpls_lse & htonl(MPLS_LABEL_MASK));
870 return !(wc->masks.mpls_lse & htonl(MPLS_TC_MASK));
872 return !(wc->masks.mpls_lse & htonl(MPLS_BOS_MASK));
875 return !wc->masks.nw_src;
877 return !wc->masks.nw_dst;
880 return ipv6_mask_is_any(&wc->masks.ipv6_src);
882 return ipv6_mask_is_any(&wc->masks.ipv6_dst);
885 return !wc->masks.ipv6_label;
888 return !wc->masks.nw_proto;
890 case MFF_IP_DSCP_SHIFTED:
891 return !(wc->masks.nw_tos & IP_DSCP_MASK);
893 return !(wc->masks.nw_tos & IP_ECN_MASK);
895 return !wc->masks.nw_ttl;
898 return ipv6_mask_is_any(&wc->masks.nd_target);
901 return !(wc->masks.nw_frag & FLOW_NW_FRAG_MASK);
904 return !wc->masks.nw_proto;
906 return !wc->masks.nw_src;
908 return !wc->masks.nw_dst;
913 case MFF_ICMPV4_TYPE:
914 case MFF_ICMPV6_TYPE:
915 return !wc->masks.tp_src;
919 case MFF_ICMPV4_CODE:
920 case MFF_ICMPV6_CODE:
921 return !wc->masks.tp_dst;
929 /* Initializes 'mask' with the wildcard bit pattern for field 'mf' within 'wc'.
930 * Each bit in 'mask' will be set to 1 if the bit is significant for matching
931 * purposes, or to 0 if it is wildcarded.
933 * The caller is responsible for ensuring that 'wc' corresponds to a flow that
934 * meets 'mf''s prerequisites. */
936 mf_get_mask(const struct mf_field *mf, const struct flow_wildcards *wc,
937 union mf_value *mask)
939 mf_get_value(mf, &wc->masks, mask);
942 /* Tests whether 'mask' is a valid wildcard bit pattern for 'mf'. Returns true
943 * if the mask is valid, false otherwise. */
945 mf_is_mask_valid(const struct mf_field *mf, const union mf_value *mask)
947 switch (mf->maskable) {
949 return (is_all_zeros((const uint8_t *) mask, mf->n_bytes) ||
950 is_all_ones((const uint8_t *) mask, mf->n_bytes));
960 is_icmpv4(const struct flow *flow)
962 return (flow->dl_type == htons(ETH_TYPE_IP)
963 && flow->nw_proto == IPPROTO_ICMP);
967 is_icmpv6(const struct flow *flow)
969 return (flow->dl_type == htons(ETH_TYPE_IPV6)
970 && flow->nw_proto == IPPROTO_ICMPV6);
973 /* Returns true if 'flow' meets the prerequisites for 'mf', false otherwise. */
975 mf_are_prereqs_ok(const struct mf_field *mf, const struct flow *flow)
977 switch (mf->prereqs) {
982 return (flow->dl_type == htons(ETH_TYPE_ARP) ||
983 flow->dl_type == htons(ETH_TYPE_RARP));
985 return flow->dl_type == htons(ETH_TYPE_IP);
987 return flow->dl_type == htons(ETH_TYPE_IPV6);
989 return (flow->vlan_tci & htons(VLAN_CFI)) != 0;
991 return eth_type_mpls(flow->dl_type);
993 return is_ip_any(flow);
996 return is_ip_any(flow) && flow->nw_proto == IPPROTO_TCP;
998 return is_ip_any(flow) && flow->nw_proto == IPPROTO_UDP;
1000 return is_ip_any(flow) && flow->nw_proto == IPPROTO_SCTP;
1002 return is_icmpv4(flow);
1004 return is_icmpv6(flow);
1007 return (is_icmpv6(flow)
1008 && flow->tp_dst == htons(0)
1009 && (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT) ||
1010 flow->tp_src == htons(ND_NEIGHBOR_ADVERT)));
1011 case MFP_ND_SOLICIT:
1012 return (is_icmpv6(flow)
1013 && flow->tp_dst == htons(0)
1014 && (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT)));
1016 return (is_icmpv6(flow)
1017 && flow->tp_dst == htons(0)
1018 && (flow->tp_src == htons(ND_NEIGHBOR_ADVERT)));
1024 /* Returns true if 'value' may be a valid value *as part of a masked match*,
1027 * A value is not rejected just because it is not valid for the field in
1028 * question, but only if it doesn't make sense to test the bits in question at
1029 * all. For example, the MFF_VLAN_TCI field will never have a nonzero value
1030 * without the VLAN_CFI bit being set, but we can't reject those values because
1031 * it is still legitimate to test just for those bits (see the documentation
1032 * for NXM_OF_VLAN_TCI in nicira-ext.h). On the other hand, there is never a
1033 * reason to set the low bit of MFF_IP_DSCP to 1, so we reject that. */
1035 mf_is_value_valid(const struct mf_field *mf, const union mf_value *value)
1046 case MFF_SKB_PRIORITY:
1069 case MFF_ICMPV4_TYPE:
1070 case MFF_ICMPV4_CODE:
1071 case MFF_ICMPV6_TYPE:
1072 case MFF_ICMPV6_CODE:
1078 case MFF_IN_PORT_OXM: {
1080 return !ofputil_port_from_ofp11(value->be32, &port);
1084 return !(value->u8 & ~IP_DSCP_MASK);
1085 case MFF_IP_DSCP_SHIFTED:
1086 return !(value->u8 & (~IP_DSCP_MASK >> 2));
1088 return !(value->u8 & ~IP_ECN_MASK);
1090 return !(value->u8 & ~FLOW_NW_FRAG_MASK);
1093 return !(value->be16 & htons(0xff00));
1096 return !(value->be16 & htons(VLAN_CFI | VLAN_PCP_MASK));
1098 return !(value->be16 & htons(VLAN_PCP_MASK));
1100 case MFF_DL_VLAN_PCP:
1102 return !(value->u8 & ~(VLAN_PCP_MASK >> VLAN_PCP_SHIFT));
1104 case MFF_IPV6_LABEL:
1105 return !(value->be32 & ~htonl(IPV6_LABEL_MASK));
1107 case MFF_MPLS_LABEL:
1108 return !(value->be32 & ~htonl(MPLS_LABEL_MASK >> MPLS_LABEL_SHIFT));
1111 return !(value->u8 & ~(MPLS_TC_MASK >> MPLS_TC_SHIFT));
1114 return !(value->u8 & ~(MPLS_BOS_MASK >> MPLS_BOS_SHIFT));
1122 /* Copies the value of field 'mf' from 'flow' into 'value'. The caller is
1123 * responsible for ensuring that 'flow' meets 'mf''s prerequisites. */
1125 mf_get_value(const struct mf_field *mf, const struct flow *flow,
1126 union mf_value *value)
1130 value->be64 = flow->tunnel.tun_id;
1133 value->be32 = flow->tunnel.ip_src;
1136 value->be32 = flow->tunnel.ip_dst;
1139 value->be16 = htons(flow->tunnel.flags);
1142 value->u8 = flow->tunnel.ip_ttl;
1145 value->u8 = flow->tunnel.ip_tos;
1149 value->be64 = flow->metadata;
1153 value->be16 = htons(ofp_to_u16(flow->in_port.ofp_port));
1155 case MFF_IN_PORT_OXM:
1156 value->be32 = ofputil_port_to_ofp11(flow->in_port.ofp_port);
1159 case MFF_SKB_PRIORITY:
1160 value->be32 = htonl(flow->skb_priority);
1164 value->be32 = htonl(flow->pkt_mark);
1168 value->be32 = htonl(flow->regs[mf->id - MFF_REG0]);
1172 memcpy(value->mac, flow->dl_src, ETH_ADDR_LEN);
1176 memcpy(value->mac, flow->dl_dst, ETH_ADDR_LEN);
1180 value->be16 = flow->dl_type;
1184 value->be16 = flow->vlan_tci;
1188 value->be16 = flow->vlan_tci & htons(VLAN_VID_MASK);
1191 value->be16 = flow->vlan_tci & htons(VLAN_VID_MASK | VLAN_CFI);
1194 case MFF_DL_VLAN_PCP:
1196 value->u8 = vlan_tci_to_pcp(flow->vlan_tci);
1199 case MFF_MPLS_LABEL:
1200 value->be32 = htonl(mpls_lse_to_label(flow->mpls_lse));
1204 value->u8 = mpls_lse_to_tc(flow->mpls_lse);
1208 value->u8 = mpls_lse_to_bos(flow->mpls_lse);
1212 value->be32 = flow->nw_src;
1216 value->be32 = flow->nw_dst;
1220 value->ipv6 = flow->ipv6_src;
1224 value->ipv6 = flow->ipv6_dst;
1227 case MFF_IPV6_LABEL:
1228 value->be32 = flow->ipv6_label;
1232 value->u8 = flow->nw_proto;
1236 value->u8 = flow->nw_tos & IP_DSCP_MASK;
1239 case MFF_IP_DSCP_SHIFTED:
1240 value->u8 = flow->nw_tos >> 2;
1244 value->u8 = flow->nw_tos & IP_ECN_MASK;
1248 value->u8 = flow->nw_ttl;
1252 value->u8 = flow->nw_frag;
1256 value->be16 = htons(flow->nw_proto);
1260 value->be32 = flow->nw_src;
1264 value->be32 = flow->nw_dst;
1269 memcpy(value->mac, flow->arp_sha, ETH_ADDR_LEN);
1274 memcpy(value->mac, flow->arp_tha, ETH_ADDR_LEN);
1280 value->be16 = flow->tp_src;
1286 value->be16 = flow->tp_dst;
1289 case MFF_ICMPV4_TYPE:
1290 case MFF_ICMPV6_TYPE:
1291 value->u8 = ntohs(flow->tp_src);
1294 case MFF_ICMPV4_CODE:
1295 case MFF_ICMPV6_CODE:
1296 value->u8 = ntohs(flow->tp_dst);
1300 value->ipv6 = flow->nd_target;
1309 /* Makes 'match' match field 'mf' exactly, with the value matched taken from
1310 * 'value'. The caller is responsible for ensuring that 'match' meets 'mf''s
1313 mf_set_value(const struct mf_field *mf,
1314 const union mf_value *value, struct match *match)
1318 match_set_tun_id(match, value->be64);
1321 match_set_tun_src(match, value->be32);
1324 match_set_tun_dst(match, value->be32);
1327 match_set_tun_flags(match, ntohs(value->be16));
1330 match_set_tun_tos(match, value->u8);
1333 match_set_tun_ttl(match, value->u8);
1337 match_set_metadata(match, value->be64);
1341 match_set_in_port(match, u16_to_ofp(ntohs(value->be16)));
1344 case MFF_IN_PORT_OXM: {
1346 ofputil_port_from_ofp11(value->be32, &port);
1347 match_set_in_port(match, port);
1351 case MFF_SKB_PRIORITY:
1352 match_set_skb_priority(match, ntohl(value->be32));
1356 match_set_pkt_mark(match, ntohl(value->be32));
1360 match_set_reg(match, mf->id - MFF_REG0, ntohl(value->be32));
1364 match_set_dl_src(match, value->mac);
1368 match_set_dl_dst(match, value->mac);
1372 match_set_dl_type(match, value->be16);
1376 match_set_dl_tci(match, value->be16);
1380 match_set_dl_vlan(match, value->be16);
1383 match_set_vlan_vid(match, value->be16);
1386 case MFF_DL_VLAN_PCP:
1388 match_set_dl_vlan_pcp(match, value->u8);
1391 case MFF_MPLS_LABEL:
1392 match_set_mpls_label(match, value->be32);
1396 match_set_mpls_tc(match, value->u8);
1400 match_set_mpls_bos(match, value->u8);
1404 match_set_nw_src(match, value->be32);
1408 match_set_nw_dst(match, value->be32);
1412 match_set_ipv6_src(match, &value->ipv6);
1416 match_set_ipv6_dst(match, &value->ipv6);
1419 case MFF_IPV6_LABEL:
1420 match_set_ipv6_label(match, value->be32);
1424 match_set_nw_proto(match, value->u8);
1428 match_set_nw_dscp(match, value->u8);
1431 case MFF_IP_DSCP_SHIFTED:
1432 match_set_nw_dscp(match, value->u8 << 2);
1436 match_set_nw_ecn(match, value->u8);
1440 match_set_nw_ttl(match, value->u8);
1444 match_set_nw_frag(match, value->u8);
1448 match_set_nw_proto(match, ntohs(value->be16));
1452 match_set_nw_src(match, value->be32);
1456 match_set_nw_dst(match, value->be32);
1461 match_set_arp_sha(match, value->mac);
1466 match_set_arp_tha(match, value->mac);
1472 match_set_tp_src(match, value->be16);
1478 match_set_tp_dst(match, value->be16);
1481 case MFF_ICMPV4_TYPE:
1482 case MFF_ICMPV6_TYPE:
1483 match_set_icmp_type(match, value->u8);
1486 case MFF_ICMPV4_CODE:
1487 case MFF_ICMPV6_CODE:
1488 match_set_icmp_code(match, value->u8);
1492 match_set_nd_target(match, &value->ipv6);
1501 /* Sets 'flow' member field described by 'mf' to 'value'. The caller is
1502 * responsible for ensuring that 'flow' meets 'mf''s prerequisites.*/
1504 mf_set_flow_value(const struct mf_field *mf,
1505 const union mf_value *value, struct flow *flow)
1509 flow->tunnel.tun_id = value->be64;
1512 flow->tunnel.ip_src = value->be32;
1515 flow->tunnel.ip_dst = value->be32;
1518 flow->tunnel.flags = ntohs(value->be16);
1521 flow->tunnel.ip_tos = value->u8;
1524 flow->tunnel.ip_ttl = value->u8;
1528 flow->metadata = value->be64;
1532 flow->in_port.ofp_port = u16_to_ofp(ntohs(value->be16));
1535 case MFF_IN_PORT_OXM: {
1537 ofputil_port_from_ofp11(value->be32, &port);
1538 flow->in_port.ofp_port = port;
1542 case MFF_SKB_PRIORITY:
1543 flow->skb_priority = ntohl(value->be32);
1547 flow->pkt_mark = ntohl(value->be32);
1551 flow->regs[mf->id - MFF_REG0] = ntohl(value->be32);
1555 memcpy(flow->dl_src, value->mac, ETH_ADDR_LEN);
1559 memcpy(flow->dl_dst, value->mac, ETH_ADDR_LEN);
1563 flow->dl_type = value->be16;
1567 flow->vlan_tci = value->be16;
1571 flow_set_dl_vlan(flow, value->be16);
1574 flow_set_vlan_vid(flow, value->be16);
1577 case MFF_DL_VLAN_PCP:
1579 flow_set_vlan_pcp(flow, value->u8);
1582 case MFF_MPLS_LABEL:
1583 flow_set_mpls_label(flow, value->be32);
1587 flow_set_mpls_tc(flow, value->u8);
1591 flow_set_mpls_bos(flow, value->u8);
1595 flow->nw_src = value->be32;
1599 flow->nw_dst = value->be32;
1603 flow->ipv6_src = value->ipv6;
1607 flow->ipv6_dst = value->ipv6;
1610 case MFF_IPV6_LABEL:
1611 flow->ipv6_label = value->be32 & ~htonl(IPV6_LABEL_MASK);
1615 flow->nw_proto = value->u8;
1619 flow->nw_tos &= ~IP_DSCP_MASK;
1620 flow->nw_tos |= value->u8 & IP_DSCP_MASK;
1623 case MFF_IP_DSCP_SHIFTED:
1624 flow->nw_tos &= ~IP_DSCP_MASK;
1625 flow->nw_tos |= value->u8 << 2;
1629 flow->nw_tos &= ~IP_ECN_MASK;
1630 flow->nw_tos |= value->u8 & IP_ECN_MASK;
1634 flow->nw_ttl = value->u8;
1638 flow->nw_frag &= value->u8;
1642 flow->nw_proto = ntohs(value->be16);
1646 flow->nw_src = value->be32;
1650 flow->nw_dst = value->be32;
1655 memcpy(flow->arp_sha, value->mac, ETH_ADDR_LEN);
1660 memcpy(flow->arp_tha, value->mac, ETH_ADDR_LEN);
1666 flow->tp_src = value->be16;
1672 flow->tp_dst = value->be16;
1675 case MFF_ICMPV4_TYPE:
1676 case MFF_ICMPV6_TYPE:
1677 flow->tp_src = htons(value->u8);
1680 case MFF_ICMPV4_CODE:
1681 case MFF_ICMPV6_CODE:
1682 flow->tp_dst = htons(value->u8);
1686 flow->nd_target = value->ipv6;
1695 /* Returns true if 'mf' has a zero value in 'flow', false if it is nonzero.
1697 * The caller is responsible for ensuring that 'flow' meets 'mf''s
1700 mf_is_zero(const struct mf_field *mf, const struct flow *flow)
1702 union mf_value value;
1704 mf_get_value(mf, flow, &value);
1705 return is_all_zeros((const uint8_t *) &value, mf->n_bytes);
1708 /* Makes 'match' wildcard field 'mf'.
1710 * The caller is responsible for ensuring that 'match' meets 'mf''s
1713 mf_set_wild(const struct mf_field *mf, struct match *match)
1717 match_set_tun_id_masked(match, htonll(0), htonll(0));
1720 match_set_tun_src_masked(match, htonl(0), htonl(0));
1723 match_set_tun_dst_masked(match, htonl(0), htonl(0));
1726 match_set_tun_flags_masked(match, 0, 0);
1729 match_set_tun_tos_masked(match, 0, 0);
1732 match_set_tun_ttl_masked(match, 0, 0);
1736 match_set_metadata_masked(match, htonll(0), htonll(0));
1740 case MFF_IN_PORT_OXM:
1741 match->flow.in_port.ofp_port = 0;
1742 match->wc.masks.in_port.ofp_port = 0;
1745 case MFF_SKB_PRIORITY:
1746 match->flow.skb_priority = 0;
1747 match->wc.masks.skb_priority = 0;
1751 match->flow.pkt_mark = 0;
1752 match->wc.masks.pkt_mark = 0;
1756 match_set_reg_masked(match, mf->id - MFF_REG0, 0, 0);
1760 memset(match->flow.dl_src, 0, ETH_ADDR_LEN);
1761 memset(match->wc.masks.dl_src, 0, ETH_ADDR_LEN);
1765 memset(match->flow.dl_dst, 0, ETH_ADDR_LEN);
1766 memset(match->wc.masks.dl_dst, 0, ETH_ADDR_LEN);
1770 match->flow.dl_type = htons(0);
1771 match->wc.masks.dl_type = htons(0);
1775 match_set_dl_tci_masked(match, htons(0), htons(0));
1780 match_set_any_vid(match);
1783 case MFF_DL_VLAN_PCP:
1785 match_set_any_pcp(match);
1788 case MFF_MPLS_LABEL:
1789 match_set_any_mpls_label(match);
1793 match_set_any_mpls_tc(match);
1797 match_set_any_mpls_bos(match);
1802 match_set_nw_src_masked(match, htonl(0), htonl(0));
1807 match_set_nw_dst_masked(match, htonl(0), htonl(0));
1811 memset(&match->wc.masks.ipv6_src, 0, sizeof match->wc.masks.ipv6_src);
1812 memset(&match->flow.ipv6_src, 0, sizeof match->flow.ipv6_src);
1816 memset(&match->wc.masks.ipv6_dst, 0, sizeof match->wc.masks.ipv6_dst);
1817 memset(&match->flow.ipv6_dst, 0, sizeof match->flow.ipv6_dst);
1820 case MFF_IPV6_LABEL:
1821 match->wc.masks.ipv6_label = htonl(0);
1822 match->flow.ipv6_label = htonl(0);
1826 match->wc.masks.nw_proto = 0;
1827 match->flow.nw_proto = 0;
1831 case MFF_IP_DSCP_SHIFTED:
1832 match->wc.masks.nw_tos &= ~IP_DSCP_MASK;
1833 match->flow.nw_tos &= ~IP_DSCP_MASK;
1837 match->wc.masks.nw_tos &= ~IP_ECN_MASK;
1838 match->flow.nw_tos &= ~IP_ECN_MASK;
1842 match->wc.masks.nw_ttl = 0;
1843 match->flow.nw_ttl = 0;
1847 match->wc.masks.nw_frag |= FLOW_NW_FRAG_MASK;
1848 match->flow.nw_frag &= ~FLOW_NW_FRAG_MASK;
1852 match->wc.masks.nw_proto = 0;
1853 match->flow.nw_proto = 0;
1858 memset(match->flow.arp_sha, 0, ETH_ADDR_LEN);
1859 memset(match->wc.masks.arp_sha, 0, ETH_ADDR_LEN);
1864 memset(match->flow.arp_tha, 0, ETH_ADDR_LEN);
1865 memset(match->wc.masks.arp_tha, 0, ETH_ADDR_LEN);
1871 case MFF_ICMPV4_TYPE:
1872 case MFF_ICMPV6_TYPE:
1873 match->wc.masks.tp_src = htons(0);
1874 match->flow.tp_src = htons(0);
1880 case MFF_ICMPV4_CODE:
1881 case MFF_ICMPV6_CODE:
1882 match->wc.masks.tp_dst = htons(0);
1883 match->flow.tp_dst = htons(0);
1887 memset(&match->wc.masks.nd_target, 0,
1888 sizeof match->wc.masks.nd_target);
1889 memset(&match->flow.nd_target, 0, sizeof match->flow.nd_target);
1898 /* Makes 'match' match field 'mf' with the specified 'value' and 'mask'.
1899 * 'value' specifies a value to match and 'mask' specifies a wildcard pattern,
1900 * with a 1-bit indicating that the corresponding value bit must match and a
1901 * 0-bit indicating a don't-care.
1903 * If 'mask' is NULL or points to all-1-bits, then this call is equivalent to
1904 * mf_set_value(mf, value, match). If 'mask' points to all-0-bits, then this
1905 * call is equivalent to mf_set_wild(mf, match).
1907 * 'mask' must be a valid mask for 'mf' (see mf_is_mask_valid()). The caller
1908 * is responsible for ensuring that 'match' meets 'mf''s prerequisites. */
1909 enum ofputil_protocol
1910 mf_set(const struct mf_field *mf,
1911 const union mf_value *value, const union mf_value *mask,
1912 struct match *match)
1914 if (!mask || is_all_ones((const uint8_t *) mask, mf->n_bytes)) {
1915 mf_set_value(mf, value, match);
1916 return mf->usable_protocols;
1917 } else if (is_all_zeros((const uint8_t *) mask, mf->n_bytes)) {
1918 mf_set_wild(mf, match);
1919 return OFPUTIL_P_ANY;
1924 case MFF_IN_PORT_OXM:
1925 case MFF_SKB_PRIORITY:
1928 case MFF_DL_VLAN_PCP:
1930 case MFF_MPLS_LABEL:
1936 case MFF_IP_DSCP_SHIFTED:
1939 case MFF_ICMPV4_TYPE:
1940 case MFF_ICMPV4_CODE:
1941 case MFF_ICMPV6_TYPE:
1942 case MFF_ICMPV6_CODE:
1946 match_set_tun_id_masked(match, value->be64, mask->be64);
1949 match_set_tun_src_masked(match, value->be32, mask->be32);
1952 match_set_tun_dst_masked(match, value->be32, mask->be32);
1955 match_set_tun_flags_masked(match, ntohs(value->be16), ntohs(mask->be16));
1958 match_set_tun_ttl_masked(match, value->u8, mask->u8);
1961 match_set_tun_tos_masked(match, value->u8, mask->u8);
1965 match_set_metadata_masked(match, value->be64, mask->be64);
1969 match_set_reg_masked(match, mf->id - MFF_REG0,
1970 ntohl(value->be32), ntohl(mask->be32));
1974 match_set_pkt_mark_masked(match, ntohl(value->be32),
1979 match_set_dl_dst_masked(match, value->mac, mask->mac);
1983 match_set_dl_src_masked(match, value->mac, mask->mac);
1988 match_set_arp_sha_masked(match, value->mac, mask->mac);
1993 match_set_arp_tha_masked(match, value->mac, mask->mac);
1997 match_set_dl_tci_masked(match, value->be16, mask->be16);
2001 match_set_vlan_vid_masked(match, value->be16, mask->be16);
2005 match_set_nw_src_masked(match, value->be32, mask->be32);
2009 match_set_nw_dst_masked(match, value->be32, mask->be32);
2013 match_set_ipv6_src_masked(match, &value->ipv6, &mask->ipv6);
2017 match_set_ipv6_dst_masked(match, &value->ipv6, &mask->ipv6);
2020 case MFF_IPV6_LABEL:
2021 if ((mask->be32 & htonl(IPV6_LABEL_MASK)) == htonl(IPV6_LABEL_MASK)) {
2022 mf_set_value(mf, value, match);
2024 match_set_ipv6_label_masked(match, value->be32, mask->be32);
2029 match_set_nd_target_masked(match, &value->ipv6, &mask->ipv6);
2033 match_set_nw_frag_masked(match, value->u8, mask->u8);
2037 match_set_nw_src_masked(match, value->be32, mask->be32);
2041 match_set_nw_dst_masked(match, value->be32, mask->be32);
2047 match_set_tp_src_masked(match, value->be16, mask->be16);
2053 match_set_tp_dst_masked(match, value->be16, mask->be16);
2061 return mf->usable_protocols_bitwise;
2064 return ip_is_cidr(mask->be32) ? mf->usable_protocols :
2065 mf->usable_protocols_bitwise;
2069 mf_check__(const struct mf_subfield *sf, const struct flow *flow,
2073 VLOG_WARN_RL(&rl, "unknown %s field", type);
2074 return OFPERR_OFPBAC_BAD_SET_TYPE;
2075 } else if (!sf->n_bits) {
2076 VLOG_WARN_RL(&rl, "zero bit %s field %s", type, sf->field->name);
2077 return OFPERR_OFPBAC_BAD_SET_LEN;
2078 } else if (sf->ofs >= sf->field->n_bits) {
2079 VLOG_WARN_RL(&rl, "bit offset %d exceeds %d-bit width of %s field %s",
2080 sf->ofs, sf->field->n_bits, type, sf->field->name);
2081 return OFPERR_OFPBAC_BAD_SET_LEN;
2082 } else if (sf->ofs + sf->n_bits > sf->field->n_bits) {
2083 VLOG_WARN_RL(&rl, "bit offset %d and width %d exceeds %d-bit width "
2084 "of %s field %s", sf->ofs, sf->n_bits,
2085 sf->field->n_bits, type, sf->field->name);
2086 return OFPERR_OFPBAC_BAD_SET_LEN;
2087 } else if (flow && !mf_are_prereqs_ok(sf->field, flow)) {
2088 VLOG_WARN_RL(&rl, "%s field %s lacks correct prerequisites",
2089 type, sf->field->name);
2090 return OFPERR_OFPBAC_MATCH_INCONSISTENT;
2096 /* Checks whether 'sf' is valid for reading a subfield out of 'flow'. Returns
2097 * 0 if so, otherwise an OpenFlow error code (e.g. as returned by
2100 mf_check_src(const struct mf_subfield *sf, const struct flow *flow)
2102 return mf_check__(sf, flow, "source");
2105 /* Checks whether 'sf' is valid for writing a subfield into 'flow'. Returns 0
2106 * if so, otherwise an OpenFlow error code (e.g. as returned by
2109 mf_check_dst(const struct mf_subfield *sf, const struct flow *flow)
2111 int error = mf_check__(sf, flow, "destination");
2112 if (!error && !sf->field->writable) {
2113 VLOG_WARN_RL(&rl, "destination field %s is not writable",
2115 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
2120 /* Copies the value and wildcard bit pattern for 'mf' from 'match' into the
2121 * 'value' and 'mask', respectively. */
2123 mf_get(const struct mf_field *mf, const struct match *match,
2124 union mf_value *value, union mf_value *mask)
2126 mf_get_value(mf, &match->flow, value);
2127 mf_get_mask(mf, &match->wc, mask);
2130 /* Assigns a random value for field 'mf' to 'value'. */
2132 mf_random_value(const struct mf_field *mf, union mf_value *value)
2134 random_bytes(value, mf->n_bytes);
2146 case MFF_SKB_PRIORITY:
2168 case MFF_ICMPV4_TYPE:
2169 case MFF_ICMPV4_CODE:
2170 case MFF_ICMPV6_TYPE:
2171 case MFF_ICMPV6_CODE:
2177 case MFF_IN_PORT_OXM:
2178 value->be32 = ofputil_port_to_ofp11(u16_to_ofp(ntohs(value->be16)));
2181 case MFF_IPV6_LABEL:
2182 value->be32 &= ~htonl(IPV6_LABEL_MASK);
2186 value->u8 &= IP_DSCP_MASK;
2189 case MFF_IP_DSCP_SHIFTED:
2190 value->u8 &= IP_DSCP_MASK >> 2;
2194 value->u8 &= IP_ECN_MASK;
2198 value->u8 &= FLOW_NW_FRAG_MASK;
2202 value->be16 &= htons(0xff);
2206 value->be16 &= htons(VLAN_VID_MASK);
2209 value->be16 &= htons(VLAN_VID_MASK | VLAN_CFI);
2212 case MFF_DL_VLAN_PCP:
2217 case MFF_MPLS_LABEL:
2218 value->be32 &= htonl(MPLS_LABEL_MASK >> MPLS_LABEL_SHIFT);
2222 value->u8 &= MPLS_TC_MASK >> MPLS_TC_SHIFT;
2226 value->u8 &= MPLS_BOS_MASK >> MPLS_BOS_SHIFT;
2236 mf_from_integer_string(const struct mf_field *mf, const char *s,
2237 uint8_t *valuep, uint8_t *maskp)
2239 unsigned long long int integer, mask;
2244 integer = strtoull(s, &tail, 0);
2245 if (errno || (*tail != '\0' && *tail != '/')) {
2250 mask = strtoull(tail + 1, &tail, 0);
2251 if (errno || *tail != '\0') {
2258 for (i = mf->n_bytes - 1; i >= 0; i--) {
2259 valuep[i] = integer;
2265 return xasprintf("%s: value too large for %u-byte field %s",
2266 s, mf->n_bytes, mf->name);
2271 return xasprintf("%s: bad syntax for %s", s, mf->name);
2275 mf_from_ethernet_string(const struct mf_field *mf, const char *s,
2276 uint8_t mac[ETH_ADDR_LEN],
2277 uint8_t mask[ETH_ADDR_LEN])
2281 ovs_assert(mf->n_bytes == ETH_ADDR_LEN);
2284 if (sscanf(s, ETH_ADDR_SCAN_FMT"%n", ETH_ADDR_SCAN_ARGS(mac), &n) > 0
2285 && n == strlen(s)) {
2286 memset(mask, 0xff, ETH_ADDR_LEN);
2291 if (sscanf(s, ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT"%n",
2292 ETH_ADDR_SCAN_ARGS(mac), ETH_ADDR_SCAN_ARGS(mask), &n) > 0
2293 && n == strlen(s)) {
2297 return xasprintf("%s: invalid Ethernet address", s);
2301 mf_from_ipv4_string(const struct mf_field *mf, const char *s,
2302 ovs_be32 *ip, ovs_be32 *mask)
2306 ovs_assert(mf->n_bytes == sizeof *ip);
2308 if (sscanf(s, IP_SCAN_FMT"/"IP_SCAN_FMT,
2309 IP_SCAN_ARGS(ip), IP_SCAN_ARGS(mask)) == IP_SCAN_COUNT * 2) {
2311 } else if (sscanf(s, IP_SCAN_FMT"/%d",
2312 IP_SCAN_ARGS(ip), &prefix) == IP_SCAN_COUNT + 1) {
2313 if (prefix <= 0 || prefix > 32) {
2314 return xasprintf("%s: network prefix bits not between 1 and "
2316 } else if (prefix == 32) {
2317 *mask = htonl(UINT32_MAX);
2319 *mask = htonl(((1u << prefix) - 1) << (32 - prefix));
2321 } else if (sscanf(s, IP_SCAN_FMT, IP_SCAN_ARGS(ip)) == IP_SCAN_COUNT) {
2322 *mask = htonl(UINT32_MAX);
2324 return xasprintf("%s: invalid IP address", s);
2330 mf_from_ipv6_string(const struct mf_field *mf, const char *s,
2331 struct in6_addr *value, struct in6_addr *mask)
2333 char *str = xstrdup(s);
2334 char *save_ptr = NULL;
2335 const char *name, *netmask;
2338 ovs_assert(mf->n_bytes == sizeof *value);
2340 name = strtok_r(str, "/", &save_ptr);
2341 retval = name ? lookup_ipv6(name, value) : EINVAL;
2345 err = xasprintf("%s: could not convert to IPv6 address", str);
2351 netmask = strtok_r(NULL, "/", &save_ptr);
2353 if (inet_pton(AF_INET6, netmask, mask) != 1) {
2354 int prefix = atoi(netmask);
2355 if (prefix <= 0 || prefix > 128) {
2357 return xasprintf("%s: prefix bits not between 1 and 128", s);
2359 *mask = ipv6_create_mask(prefix);
2363 *mask = in6addr_exact;
2371 mf_from_ofp_port_string(const struct mf_field *mf, const char *s,
2372 ovs_be16 *valuep, ovs_be16 *maskp)
2376 ovs_assert(mf->n_bytes == sizeof(ovs_be16));
2378 if (ofputil_port_from_string(s, &port)) {
2379 *valuep = htons(ofp_to_u16(port));
2380 *maskp = htons(UINT16_MAX);
2383 return xasprintf("%s: port value out of range for %s", s, mf->name);
2387 mf_from_ofp_port_string32(const struct mf_field *mf, const char *s,
2388 ovs_be32 *valuep, ovs_be32 *maskp)
2392 ovs_assert(mf->n_bytes == sizeof(ovs_be32));
2393 if (ofputil_port_from_string(s, &port)) {
2394 *valuep = ofputil_port_to_ofp11(port);
2395 *maskp = htonl(UINT32_MAX);
2398 return xasprintf("%s: port value out of range for %s", s, mf->name);
2401 struct frag_handling {
2407 static const struct frag_handling all_frags[] = {
2408 #define A FLOW_NW_FRAG_ANY
2409 #define L FLOW_NW_FRAG_LATER
2410 /* name mask value */
2413 { "first", A|L, A },
2414 { "later", A|L, A|L },
2419 { "not_later", L, 0 },
2426 mf_from_frag_string(const char *s, uint8_t *valuep, uint8_t *maskp)
2428 const struct frag_handling *h;
2430 for (h = all_frags; h < &all_frags[ARRAY_SIZE(all_frags)]; h++) {
2431 if (!strcasecmp(s, h->name)) {
2432 /* We force the upper bits of the mask on to make mf_parse_value()
2433 * happy (otherwise it will never think it's an exact match.) */
2434 *maskp = h->mask | ~FLOW_NW_FRAG_MASK;
2440 return xasprintf("%s: unknown fragment type (valid types are \"no\", "
2441 "\"yes\", \"first\", \"later\", \"not_first\"", s);
2445 parse_flow_tun_flags(const char *s_, const char *(*bit_to_string)(uint32_t),
2448 uint32_t result = 0;
2449 char *save_ptr = NULL;
2452 char *s = xstrdup(s_);
2454 for (name = strtok_r((char *)s, " |", &save_ptr); name;
2455 name = strtok_r(NULL, " |", &save_ptr)) {
2457 unsigned long long int flags;
2461 if (sscanf(name, "%lli%n", &flags, &n0) > 0 && n0 > 0) {
2465 name_len = strlen(name);
2466 for (bit = 1; bit; bit <<= 1) {
2467 const char *fname = bit_to_string(bit);
2474 len = strlen(fname);
2475 if (len != name_len) {
2478 if (!strncmp(name, fname, len)) {
2490 *res = htons(result);
2497 mf_from_tun_flags_string(const char *s, ovs_be16 *valuep, ovs_be16 *maskp)
2499 if (!parse_flow_tun_flags(s, flow_tun_flag_to_string, valuep)) {
2500 *maskp = htons(UINT16_MAX);
2504 return xasprintf("%s: unknown tunnel flags (valid flags are \"df\", "
2505 "\"csum\", \"key\"", s);
2508 /* Parses 's', a string value for field 'mf', into 'value' and 'mask'. Returns
2509 * NULL if successful, otherwise a malloc()'d string describing the error. */
2511 mf_parse(const struct mf_field *mf, const char *s,
2512 union mf_value *value, union mf_value *mask)
2514 if (!strcmp(s, "*")) {
2515 memset(value, 0, mf->n_bytes);
2516 memset(mask, 0, mf->n_bytes);
2520 switch (mf->string) {
2522 case MFS_HEXADECIMAL:
2523 return mf_from_integer_string(mf, s,
2524 (uint8_t *) value, (uint8_t *) mask);
2527 return mf_from_ethernet_string(mf, s, value->mac, mask->mac);
2530 return mf_from_ipv4_string(mf, s, &value->be32, &mask->be32);
2533 return mf_from_ipv6_string(mf, s, &value->ipv6, &mask->ipv6);
2536 return mf_from_ofp_port_string(mf, s, &value->be16, &mask->be16);
2538 case MFS_OFP_PORT_OXM:
2539 return mf_from_ofp_port_string32(mf, s, &value->be32, &mask->be32);
2542 return mf_from_frag_string(s, &value->u8, &mask->u8);
2545 ovs_assert(mf->n_bytes == sizeof(ovs_be16));
2546 return mf_from_tun_flags_string(s, &value->be16, &mask->be16);
2551 /* Parses 's', a string value for field 'mf', into 'value'. Returns NULL if
2552 * successful, otherwise a malloc()'d string describing the error. */
2554 mf_parse_value(const struct mf_field *mf, const char *s, union mf_value *value)
2556 union mf_value mask;
2559 error = mf_parse(mf, s, value, &mask);
2564 if (!is_all_ones((const uint8_t *) &mask, mf->n_bytes)) {
2565 return xasprintf("%s: wildcards not allowed here", s);
2571 mf_format_integer_string(const struct mf_field *mf, const uint8_t *valuep,
2572 const uint8_t *maskp, struct ds *s)
2574 unsigned long long int integer;
2577 ovs_assert(mf->n_bytes <= 8);
2580 for (i = 0; i < mf->n_bytes; i++) {
2581 integer = (integer << 8) | valuep[i];
2583 if (mf->string == MFS_HEXADECIMAL) {
2584 ds_put_format(s, "%#llx", integer);
2586 ds_put_format(s, "%lld", integer);
2590 unsigned long long int mask;
2593 for (i = 0; i < mf->n_bytes; i++) {
2594 mask = (mask << 8) | maskp[i];
2597 /* I guess we could write the mask in decimal for MFS_DECIMAL but I'm
2598 * not sure that that a bit-mask written in decimal is ever easier to
2599 * understand than the same bit-mask written in hexadecimal. */
2600 ds_put_format(s, "/%#llx", mask);
2605 mf_format_frag_string(uint8_t value, uint8_t mask, struct ds *s)
2607 const struct frag_handling *h;
2609 mask &= FLOW_NW_FRAG_MASK;
2612 for (h = all_frags; h < &all_frags[ARRAY_SIZE(all_frags)]; h++) {
2613 if (value == h->value && mask == h->mask) {
2614 ds_put_cstr(s, h->name);
2618 ds_put_cstr(s, "<error>");
2622 mf_format_tnl_flags_string(const ovs_be16 *valuep, struct ds *s)
2624 format_flags(s, flow_tun_flag_to_string, ntohs(*valuep), '|');
2627 /* Appends to 's' a string representation of field 'mf' whose value is in
2628 * 'value' and 'mask'. 'mask' may be NULL to indicate an exact match. */
2630 mf_format(const struct mf_field *mf,
2631 const union mf_value *value, const union mf_value *mask,
2635 if (is_all_zeros((const uint8_t *) mask, mf->n_bytes)) {
2636 ds_put_cstr(s, "ANY");
2638 } else if (is_all_ones((const uint8_t *) mask, mf->n_bytes)) {
2643 switch (mf->string) {
2644 case MFS_OFP_PORT_OXM:
2647 ofputil_port_from_ofp11(value->be32, &port);
2648 ofputil_format_port(port, s);
2654 ofputil_format_port(u16_to_ofp(ntohs(value->be16)), s);
2659 case MFS_HEXADECIMAL:
2660 mf_format_integer_string(mf, (uint8_t *) value, (uint8_t *) mask, s);
2664 eth_format_masked(value->mac, mask->mac, s);
2668 ip_format_masked(value->be32, mask ? mask->be32 : htonl(UINT32_MAX),
2673 print_ipv6_masked(s, &value->ipv6, mask ? &mask->ipv6 : NULL);
2677 mf_format_frag_string(value->u8, mask ? mask->u8 : UINT8_MAX, s);
2681 mf_format_tnl_flags_string(&value->be16, s);
2689 /* Makes subfield 'sf' within 'flow' exactly match the 'sf->n_bits'
2690 * least-significant bits in 'x'.
2693 mf_write_subfield_flow(const struct mf_subfield *sf,
2694 const union mf_subvalue *x, struct flow *flow)
2696 const struct mf_field *field = sf->field;
2697 union mf_value value;
2699 mf_get_value(field, flow, &value);
2700 bitwise_copy(x, sizeof *x, 0, &value, field->n_bytes,
2701 sf->ofs, sf->n_bits);
2702 mf_set_flow_value(field, &value, flow);
2705 /* Makes subfield 'sf' within 'match' exactly match the 'sf->n_bits'
2706 * least-significant bits in 'x'.
2709 mf_write_subfield(const struct mf_subfield *sf, const union mf_subvalue *x,
2710 struct match *match)
2712 const struct mf_field *field = sf->field;
2713 union mf_value value, mask;
2715 mf_get(field, match, &value, &mask);
2716 bitwise_copy(x, sizeof *x, 0, &value, field->n_bytes, sf->ofs, sf->n_bits);
2717 bitwise_one ( &mask, field->n_bytes, sf->ofs, sf->n_bits);
2718 mf_set(field, &value, &mask, match);
2721 /* Initializes 'x' to the value of 'sf' within 'flow'. 'sf' must be valid for
2722 * reading 'flow', e.g. as checked by mf_check_src(). */
2724 mf_read_subfield(const struct mf_subfield *sf, const struct flow *flow,
2725 union mf_subvalue *x)
2727 union mf_value value;
2729 mf_get_value(sf->field, flow, &value);
2731 memset(x, 0, sizeof *x);
2732 bitwise_copy(&value, sf->field->n_bytes, sf->ofs,
2737 /* Returns the value of 'sf' within 'flow'. 'sf' must be valid for reading
2738 * 'flow', e.g. as checked by mf_check_src() and sf->n_bits must be 64 or
2741 mf_get_subfield(const struct mf_subfield *sf, const struct flow *flow)
2743 union mf_value value;
2745 mf_get_value(sf->field, flow, &value);
2746 return bitwise_get(&value, sf->field->n_bytes, sf->ofs, sf->n_bits);
2749 /* Formats 'sf' into 's' in a format normally acceptable to
2750 * mf_parse_subfield(). (It won't be acceptable if sf->field is NULL or if
2751 * sf->field has no NXM name.) */
2753 mf_format_subfield(const struct mf_subfield *sf, struct ds *s)
2756 ds_put_cstr(s, "<unknown>");
2757 } else if (sf->field->nxm_name) {
2758 ds_put_cstr(s, sf->field->nxm_name);
2759 } else if (sf->field->nxm_header) {
2760 uint32_t header = sf->field->nxm_header;
2761 ds_put_format(s, "%d:%d", NXM_VENDOR(header), NXM_FIELD(header));
2763 ds_put_cstr(s, sf->field->name);
2766 if (sf->field && sf->ofs == 0 && sf->n_bits == sf->field->n_bits) {
2767 ds_put_cstr(s, "[]");
2768 } else if (sf->n_bits == 1) {
2769 ds_put_format(s, "[%d]", sf->ofs);
2771 ds_put_format(s, "[%d..%d]", sf->ofs, sf->ofs + sf->n_bits - 1);
2775 static const struct mf_field *
2776 mf_parse_subfield_name(const char *name, int name_len, bool *wild)
2780 *wild = name_len > 2 && !memcmp(&name[name_len - 2], "_W", 2);
2785 for (i = 0; i < MFF_N_IDS; i++) {
2786 const struct mf_field *mf = mf_from_id(i);
2789 && !strncmp(mf->nxm_name, name, name_len)
2790 && mf->nxm_name[name_len] == '\0') {
2794 && !strncmp(mf->oxm_name, name, name_len)
2795 && mf->oxm_name[name_len] == '\0') {
2803 /* Parses a subfield from the beginning of '*sp' into 'sf'. If successful,
2804 * returns NULL and advances '*sp' to the first byte following the parsed
2805 * string. On failure, returns a malloc()'d error message, does not modify
2806 * '*sp', and does not properly initialize 'sf'.
2808 * The syntax parsed from '*sp' takes the form "header[start..end]" where
2809 * 'header' is the name of an NXM field and 'start' and 'end' are (inclusive)
2810 * bit indexes. "..end" may be omitted to indicate a single bit. "start..end"
2811 * may both be omitted (the [] are still required) to indicate an entire
2813 char * WARN_UNUSED_RESULT
2814 mf_parse_subfield__(struct mf_subfield *sf, const char **sp)
2816 const struct mf_field *field;
2825 name_len = strcspn(s, "[");
2826 if (s[name_len] != '[') {
2827 return xasprintf("%s: missing [ looking for field name", *sp);
2830 field = mf_parse_subfield_name(name, name_len, &wild);
2832 return xasprintf("%s: unknown field `%.*s'", *sp, name_len, s);
2836 if (sscanf(s, "[%d..%d]", &start, &end) == 2) {
2837 /* Nothing to do. */
2838 } else if (sscanf(s, "[%d]", &start) == 1) {
2840 } else if (!strncmp(s, "[]", 2)) {
2842 end = field->n_bits - 1;
2844 return xasprintf("%s: syntax error expecting [] or [<bit>] or "
2845 "[<start>..<end>]", *sp);
2847 s = strchr(s, ']') + 1;
2850 return xasprintf("%s: starting bit %d is after ending bit %d",
2852 } else if (start >= field->n_bits) {
2853 return xasprintf("%s: starting bit %d is not valid because field is "
2854 "only %d bits wide", *sp, start, field->n_bits);
2855 } else if (end >= field->n_bits){
2856 return xasprintf("%s: ending bit %d is not valid because field is "
2857 "only %d bits wide", *sp, end, field->n_bits);
2862 sf->n_bits = end - start + 1;
2868 /* Parses a subfield from the entirety of 's' into 'sf'. Returns NULL if
2869 * successful, otherwise a malloc()'d string describing the error. The caller
2870 * is responsible for freeing the returned string.
2872 * The syntax parsed from 's' takes the form "header[start..end]" where
2873 * 'header' is the name of an NXM field and 'start' and 'end' are (inclusive)
2874 * bit indexes. "..end" may be omitted to indicate a single bit. "start..end"
2875 * may both be omitted (the [] are still required) to indicate an entire
2877 char * WARN_UNUSED_RESULT
2878 mf_parse_subfield(struct mf_subfield *sf, const char *s)
2880 char *error = mf_parse_subfield__(sf, &s);
2881 if (!error && s[0]) {
2882 error = xstrdup("unexpected input following field syntax");
2888 mf_format_subvalue(const union mf_subvalue *subvalue, struct ds *s)
2892 for (i = 0; i < ARRAY_SIZE(subvalue->u8); i++) {
2893 if (subvalue->u8[i]) {
2894 ds_put_format(s, "0x%"PRIx8, subvalue->u8[i]);
2895 for (i++; i < ARRAY_SIZE(subvalue->u8); i++) {
2896 ds_put_format(s, "%02"PRIx8, subvalue->u8[i]);
2901 ds_put_char(s, '0');