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"
39 VLOG_DEFINE_THIS_MODULE(meta_flow);
41 #define MF_FIELD_SIZES(MEMBER) \
42 sizeof ((union mf_value *)0)->MEMBER, \
43 8 * sizeof ((union mf_value *)0)->MEMBER
45 static const struct mf_field mf_fields[MFF_N_IDS] = {
51 MFF_TUN_ID, "tun_id", NULL,
57 NXM_NX_TUN_ID, "NXM_NX_TUN_ID",
58 OXM_OF_TUNNEL_ID, "OXM_OF_TUNNEL_ID",
59 OFPUTIL_P_NXM_OXM_ANY,
60 OFPUTIL_P_NXM_OXM_ANY,
62 MFF_TUN_SRC, "tun_src", NULL,
68 NXM_NX_TUN_IPV4_SRC, "NXM_NX_TUN_IPV4_SRC",
69 NXM_NX_TUN_IPV4_SRC, "NXM_NX_TUN_IPV4_SRC",
70 OFPUTIL_P_NXM_OXM_ANY,
71 OFPUTIL_P_NXM_OXM_ANY,
73 MFF_TUN_DST, "tun_dst", NULL,
79 NXM_NX_TUN_IPV4_DST, "NXM_NX_TUN_IPV4_DST",
80 NXM_NX_TUN_IPV4_DST, "NXM_NX_TUN_IPV4_DST",
81 OFPUTIL_P_NXM_OXM_ANY,
82 OFPUTIL_P_NXM_OXM_ANY,
84 MFF_TUN_FLAGS, "tun_flags", NULL,
95 MFF_TUN_TTL, "tun_ttl", NULL,
106 MFF_TUN_TOS, "tun_tos", NULL,
117 MFF_METADATA, "metadata", NULL,
118 MF_FIELD_SIZES(be64),
123 OXM_OF_METADATA, "OXM_OF_METADATA",
124 OXM_OF_METADATA, "OXM_OF_METADATA",
125 OFPUTIL_P_NXM_OF11_UP,
126 OFPUTIL_P_NXM_OF11_UP,
128 MFF_IN_PORT, "in_port", NULL,
129 MF_FIELD_SIZES(be16),
134 NXM_OF_IN_PORT, "NXM_OF_IN_PORT",
135 NXM_OF_IN_PORT, "NXM_OF_IN_PORT",
136 OFPUTIL_P_ANY, /* OF11+ via mapping to 32 bits. */
139 MFF_IN_PORT_OXM, "in_port_oxm", NULL,
140 MF_FIELD_SIZES(be32),
145 OXM_OF_IN_PORT, "OXM_OF_IN_PORT",
146 OXM_OF_IN_PORT, "OXM_OF_IN_PORT",
150 MFF_SKB_PRIORITY, "skb_priority", NULL,
151 MF_FIELD_SIZES(be32),
161 MFF_PKT_MARK, "pkt_mark", NULL,
162 MF_FIELD_SIZES(be32),
167 NXM_NX_PKT_MARK, "NXM_NX_PKT_MARK",
168 NXM_NX_PKT_MARK, "NXM_NX_PKT_MARK",
169 OFPUTIL_P_NXM_OXM_ANY,
170 OFPUTIL_P_NXM_OXM_ANY,
173 #define REGISTER(IDX) \
175 MFF_REG##IDX, "reg" #IDX, NULL, \
176 MF_FIELD_SIZES(be32), \
181 NXM_NX_REG(IDX), "NXM_NX_REG" #IDX, \
182 NXM_NX_REG(IDX), "NXM_NX_REG" #IDX, \
183 OFPUTIL_P_NXM_OXM_ANY, \
184 OFPUTIL_P_NXM_OXM_ANY, \
219 MFF_ETH_SRC, "eth_src", "dl_src",
225 NXM_OF_ETH_SRC, "NXM_OF_ETH_SRC",
226 OXM_OF_ETH_SRC, "OXM_OF_ETH_SRC",
228 OFPUTIL_P_NXM_OF11_UP, /* Bitwise masking only with NXM and OF11+! */
230 MFF_ETH_DST, "eth_dst", "dl_dst",
236 NXM_OF_ETH_DST, "NXM_OF_ETH_DST",
237 OXM_OF_ETH_DST, "OXM_OF_ETH_DST",
239 OFPUTIL_P_NXM_OF11_UP, /* Bitwise masking only with NXM and OF11+! */
241 MFF_ETH_TYPE, "eth_type", "dl_type",
242 MF_FIELD_SIZES(be16),
247 NXM_OF_ETH_TYPE, "NXM_OF_ETH_TYPE",
248 OXM_OF_ETH_TYPE, "OXM_OF_ETH_TYPE",
254 MFF_VLAN_TCI, "vlan_tci", NULL,
255 MF_FIELD_SIZES(be16),
260 NXM_OF_VLAN_TCI, "NXM_OF_VLAN_TCI",
261 NXM_OF_VLAN_TCI, "NXM_OF_VLAN_TCI",
263 OFPUTIL_P_NXM_OXM_ANY,
265 MFF_DL_VLAN, "dl_vlan", NULL,
266 sizeof(ovs_be16), 12,
274 OFPUTIL_P_NXM_OXM_ANY,
276 MFF_VLAN_VID, "vlan_vid", NULL,
277 sizeof(ovs_be16), 12,
282 OXM_OF_VLAN_VID, "OXM_OF_VLAN_VID",
283 OXM_OF_VLAN_VID, "OXM_OF_VLAN_VID",
285 OFPUTIL_P_NXM_OXM_ANY,
287 MFF_DL_VLAN_PCP, "dl_vlan_pcp", NULL,
295 OFPUTIL_P_ANY, /* Will be mapped to NXM and OXM. */
298 MFF_VLAN_PCP, "vlan_pcp", NULL,
304 OXM_OF_VLAN_PCP, "OXM_OF_VLAN_PCP",
305 OXM_OF_VLAN_PCP, "OXM_OF_VLAN_PCP",
306 OFPUTIL_P_ANY, /* Will be mapped to OF10 and NXM. */
314 MFF_MPLS_LABEL, "mpls_label", NULL,
320 OXM_OF_MPLS_LABEL, "OXM_OF_MPLS_LABEL",
321 OXM_OF_MPLS_LABEL, "OXM_OF_MPLS_LABEL",
322 OFPUTIL_P_NXM_OF11_UP,
325 MFF_MPLS_TC, "mpls_tc", NULL,
331 OXM_OF_MPLS_TC, "OXM_OF_MPLS_TC",
332 OXM_OF_MPLS_TC, "OXM_OF_MPLS_TC",
333 OFPUTIL_P_NXM_OF11_UP,
336 MFF_MPLS_BOS, "mpls_bos", NULL,
342 OXM_OF_MPLS_BOS, "OXM_OF_MPLS_BOS",
343 OXM_OF_MPLS_BOS, "OXM_OF_MPLS_BOS",
344 OFPUTIL_P_NXM_OXM_ANY,
353 MFF_IPV4_SRC, "ip_src", "nw_src",
354 MF_FIELD_SIZES(be32),
359 NXM_OF_IP_SRC, "NXM_OF_IP_SRC",
360 OXM_OF_IPV4_SRC, "OXM_OF_IPV4_SRC",
362 OFPUTIL_P_NXM_OF11_UP,
364 MFF_IPV4_DST, "ip_dst", "nw_dst",
365 MF_FIELD_SIZES(be32),
370 NXM_OF_IP_DST, "NXM_OF_IP_DST",
371 OXM_OF_IPV4_DST, "OXM_OF_IPV4_DST",
373 OFPUTIL_P_NXM_OF11_UP,
377 MFF_IPV6_SRC, "ipv6_src", NULL,
378 MF_FIELD_SIZES(ipv6),
383 NXM_NX_IPV6_SRC, "NXM_NX_IPV6_SRC",
384 OXM_OF_IPV6_SRC, "OXM_OF_IPV6_SRC",
385 OFPUTIL_P_NXM_OXM_ANY,
386 OFPUTIL_P_NXM_OXM_ANY,
388 MFF_IPV6_DST, "ipv6_dst", NULL,
389 MF_FIELD_SIZES(ipv6),
394 NXM_NX_IPV6_DST, "NXM_NX_IPV6_DST",
395 OXM_OF_IPV6_DST, "OXM_OF_IPV6_DST",
396 OFPUTIL_P_NXM_OXM_ANY,
397 OFPUTIL_P_NXM_OXM_ANY,
400 MFF_IPV6_LABEL, "ipv6_label", NULL,
406 NXM_NX_IPV6_LABEL, "NXM_NX_IPV6_LABEL",
407 OXM_OF_IPV6_FLABEL, "OXM_OF_IPV6_FLABEL",
408 OFPUTIL_P_NXM_OXM_ANY,
409 OFPUTIL_P_NXM_OXM_ANY,
413 MFF_IP_PROTO, "nw_proto", NULL,
419 NXM_OF_IP_PROTO, "NXM_OF_IP_PROTO",
420 OXM_OF_IP_PROTO, "OXM_OF_IP_PROTO",
424 MFF_IP_DSCP, "nw_tos", NULL,
430 NXM_OF_IP_TOS, "NXM_OF_IP_TOS",
431 NXM_OF_IP_TOS, "NXM_OF_IP_TOS",
432 OFPUTIL_P_ANY, /* Will be shifted for OXM. */
435 MFF_IP_DSCP_SHIFTED, "nw_tos_shifted", NULL,
441 OXM_OF_IP_DSCP, "OXM_OF_IP_DSCP",
442 OXM_OF_IP_DSCP, "OXM_OF_IP_DSCP",
443 OFPUTIL_P_ANY, /* Will be shifted for non-OXM. */
446 MFF_IP_ECN, "nw_ecn", NULL,
452 NXM_NX_IP_ECN, "NXM_NX_IP_ECN",
453 OXM_OF_IP_ECN, "OXM_OF_IP_ECN",
454 OFPUTIL_P_NXM_OXM_ANY,
457 MFF_IP_TTL, "nw_ttl", NULL,
463 NXM_NX_IP_TTL, "NXM_NX_IP_TTL",
464 NXM_NX_IP_TTL, "NXM_NX_IP_TTL",
465 OFPUTIL_P_NXM_OXM_ANY,
468 MFF_IP_FRAG, "ip_frag", NULL,
474 NXM_NX_IP_FRAG, "NXM_NX_IP_FRAG",
475 NXM_NX_IP_FRAG, "NXM_NX_IP_FRAG",
476 OFPUTIL_P_NXM_OXM_ANY,
477 OFPUTIL_P_NXM_OXM_ANY,
481 MFF_ARP_OP, "arp_op", NULL,
482 MF_FIELD_SIZES(be16),
487 NXM_OF_ARP_OP, "NXM_OF_ARP_OP",
488 OXM_OF_ARP_OP, "OXM_OF_ARP_OP",
492 MFF_ARP_SPA, "arp_spa", NULL,
493 MF_FIELD_SIZES(be32),
498 NXM_OF_ARP_SPA, "NXM_OF_ARP_SPA",
499 OXM_OF_ARP_SPA, "OXM_OF_ARP_SPA",
501 OFPUTIL_P_NXM_OF11_UP,
503 MFF_ARP_TPA, "arp_tpa", NULL,
504 MF_FIELD_SIZES(be32),
509 NXM_OF_ARP_TPA, "NXM_OF_ARP_TPA",
510 OXM_OF_ARP_TPA, "OXM_OF_ARP_TPA",
512 OFPUTIL_P_NXM_OF11_UP,
514 MFF_ARP_SHA, "arp_sha", NULL,
520 NXM_NX_ARP_SHA, "NXM_NX_ARP_SHA",
521 OXM_OF_ARP_SHA, "OXM_OF_ARP_SHA",
522 OFPUTIL_P_NXM_OXM_ANY,
523 OFPUTIL_P_NXM_OXM_ANY,
525 MFF_ARP_THA, "arp_tha", NULL,
531 NXM_NX_ARP_THA, "NXM_NX_ARP_THA",
532 OXM_OF_ARP_THA, "OXM_OF_ARP_THA",
533 OFPUTIL_P_NXM_OXM_ANY,
534 OFPUTIL_P_NXM_OXM_ANY,
542 MFF_TCP_SRC, "tcp_src", "tp_src",
543 MF_FIELD_SIZES(be16),
548 NXM_OF_TCP_SRC, "NXM_OF_TCP_SRC",
549 OXM_OF_TCP_SRC, "OXM_OF_TCP_SRC",
551 OFPUTIL_P_NXM_OXM_ANY,
553 MFF_TCP_DST, "tcp_dst", "tp_dst",
554 MF_FIELD_SIZES(be16),
559 NXM_OF_TCP_DST, "NXM_OF_TCP_DST",
560 OXM_OF_TCP_DST, "OXM_OF_TCP_DST",
562 OFPUTIL_P_NXM_OXM_ANY,
566 MFF_UDP_SRC, "udp_src", NULL,
567 MF_FIELD_SIZES(be16),
572 NXM_OF_UDP_SRC, "NXM_OF_UDP_SRC",
573 OXM_OF_UDP_SRC, "OXM_OF_UDP_SRC",
575 OFPUTIL_P_NXM_OXM_ANY,
577 MFF_UDP_DST, "udp_dst", NULL,
578 MF_FIELD_SIZES(be16),
583 NXM_OF_UDP_DST, "NXM_OF_UDP_DST",
584 OXM_OF_UDP_DST, "OXM_OF_UDP_DST",
586 OFPUTIL_P_NXM_OXM_ANY,
590 MFF_SCTP_SRC, "sctp_src", NULL,
591 MF_FIELD_SIZES(be16),
596 OXM_OF_SCTP_SRC, "OXM_OF_SCTP_SRC",
597 OXM_OF_SCTP_SRC, "OXM_OF_SCTP_SRC",
598 OFPUTIL_P_NXM_OF11_UP,
599 OFPUTIL_P_NXM_OXM_ANY,
601 MFF_SCTP_DST, "sctp_dst", NULL,
602 MF_FIELD_SIZES(be16),
607 OXM_OF_SCTP_DST, "OXM_OF_SCTP_DST",
608 OXM_OF_SCTP_DST, "OXM_OF_SCTP_DST",
609 OFPUTIL_P_NXM_OF11_UP,
610 OFPUTIL_P_NXM_OXM_ANY,
614 MFF_ICMPV4_TYPE, "icmp_type", NULL,
620 NXM_OF_ICMP_TYPE, "NXM_OF_ICMP_TYPE",
621 OXM_OF_ICMPV4_TYPE, "OXM_OF_ICMPV4_TYPE",
625 MFF_ICMPV4_CODE, "icmp_code", NULL,
631 NXM_OF_ICMP_CODE, "NXM_OF_ICMP_CODE",
632 OXM_OF_ICMPV4_CODE, "OXM_OF_ICMPV4_CODE",
638 MFF_ICMPV6_TYPE, "icmpv6_type", NULL,
644 NXM_NX_ICMPV6_TYPE, "NXM_NX_ICMPV6_TYPE",
645 OXM_OF_ICMPV6_TYPE, "OXM_OF_ICMPV6_TYPE",
646 OFPUTIL_P_NXM_OXM_ANY,
649 MFF_ICMPV6_CODE, "icmpv6_code", NULL,
655 NXM_NX_ICMPV6_CODE, "NXM_NX_ICMPV6_CODE",
656 OXM_OF_ICMPV6_CODE, "OXM_OF_ICMPV6_CODE",
657 OFPUTIL_P_NXM_OXM_ANY,
666 MFF_ND_TARGET, "nd_target", NULL,
667 MF_FIELD_SIZES(ipv6),
672 NXM_NX_ND_TARGET, "NXM_NX_ND_TARGET",
673 OXM_OF_IPV6_ND_TARGET, "OXM_OF_IPV6_ND_TARGET",
674 OFPUTIL_P_NXM_OXM_ANY,
675 OFPUTIL_P_NXM_OXM_ANY,
677 MFF_ND_SLL, "nd_sll", NULL,
683 NXM_NX_ND_SLL, "NXM_NX_ND_SLL",
684 OXM_OF_IPV6_ND_SLL, "OXM_OF_IPV6_ND_SLL",
685 OFPUTIL_P_NXM_OXM_ANY,
686 OFPUTIL_P_NXM_OXM_ANY,
688 MFF_ND_TLL, "nd_tll", NULL,
694 NXM_NX_ND_TLL, "NXM_NX_ND_TLL",
695 OXM_OF_IPV6_ND_TLL, "OXM_OF_IPV6_ND_TLL",
696 OFPUTIL_P_NXM_OXM_ANY,
697 OFPUTIL_P_NXM_OXM_ANY,
701 /* Maps an NXM or OXM header value to an mf_field. */
703 struct hmap_node hmap_node; /* In 'all_fields' hmap. */
704 uint32_t header; /* NXM or OXM header value. */
705 const struct mf_field *mf;
708 /* Contains 'struct nxm_field's. */
709 static struct hmap all_fields;
711 /* Maps from an mf_field's 'name' or 'extra_name' to the mf_field. */
712 static struct shash mf_by_name;
714 /* Rate limit for parse errors. These always indicate a bug in an OpenFlow
715 * controller and so there's not much point in showing a lot of them. */
716 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
718 const struct mf_field *mf_from_nxm_header__(uint32_t header);
719 static void nxm_init(void);
721 /* Returns the field with the given 'id'. */
722 const struct mf_field *
723 mf_from_id(enum mf_field_id id)
725 ovs_assert((unsigned int) id < MFF_N_IDS);
726 return &mf_fields[id];
729 /* Returns the field with the given 'name', or a null pointer if no field has
731 const struct mf_field *
732 mf_from_name(const char *name)
735 return shash_find_data(&mf_by_name, name);
739 add_nxm_field(uint32_t header, const struct mf_field *mf)
743 f = xmalloc(sizeof *f);
744 hmap_insert(&all_fields, &f->hmap_node, hash_int(header, 0));
750 nxm_init_add_field(const struct mf_field *mf, uint32_t header)
753 ovs_assert(!mf_from_nxm_header__(header));
754 add_nxm_field(header, mf);
755 if (mf->maskable != MFM_NONE) {
756 add_nxm_field(NXM_MAKE_WILD_HEADER(header), mf);
766 hmap_init(&all_fields);
767 shash_init(&mf_by_name);
768 for (i = 0; i < MFF_N_IDS; i++) {
769 const struct mf_field *mf = &mf_fields[i];
771 ovs_assert(mf->id == i); /* Fields must be in the enum order. */
773 nxm_init_add_field(mf, mf->nxm_header);
774 if (mf->oxm_header != mf->nxm_header) {
775 nxm_init_add_field(mf, mf->oxm_header);
778 shash_add_once(&mf_by_name, mf->name, mf);
779 if (mf->extra_name) {
780 shash_add_once(&mf_by_name, mf->extra_name, mf);
788 static pthread_once_t once = PTHREAD_ONCE_INIT;
789 pthread_once(&once, nxm_do_init);
792 const struct mf_field *
793 mf_from_nxm_header(uint32_t header)
796 return mf_from_nxm_header__(header);
799 const struct mf_field *
800 mf_from_nxm_header__(uint32_t header)
802 const struct nxm_field *f;
804 HMAP_FOR_EACH_IN_BUCKET (f, hmap_node, hash_int(header, 0), &all_fields) {
805 if (f->header == header) {
813 /* Returns true if 'wc' wildcards all the bits in field 'mf', false if 'wc'
814 * specifies at least one bit in the field.
816 * The caller is responsible for ensuring that 'wc' corresponds to a flow that
817 * meets 'mf''s prerequisites. */
819 mf_is_all_wild(const struct mf_field *mf, const struct flow_wildcards *wc)
823 return !wc->masks.tunnel.ip_src;
825 return !wc->masks.tunnel.ip_dst;
830 return !wc->masks.tunnel.tun_id;
832 return !wc->masks.metadata;
834 case MFF_IN_PORT_OXM:
835 return !wc->masks.in_port.ofp_port;
836 case MFF_SKB_PRIORITY:
837 return !wc->masks.skb_priority;
839 return !wc->masks.pkt_mark;
841 return !wc->masks.regs[mf->id - MFF_REG0];
844 return eth_addr_is_zero(wc->masks.dl_src);
846 return eth_addr_is_zero(wc->masks.dl_dst);
848 return !wc->masks.dl_type;
852 return eth_addr_is_zero(wc->masks.arp_sha);
856 return eth_addr_is_zero(wc->masks.arp_tha);
859 return !wc->masks.vlan_tci;
861 return !(wc->masks.vlan_tci & htons(VLAN_VID_MASK));
863 return !(wc->masks.vlan_tci & htons(VLAN_VID_MASK | VLAN_CFI));
864 case MFF_DL_VLAN_PCP:
866 return !(wc->masks.vlan_tci & htons(VLAN_PCP_MASK));
869 return !(wc->masks.mpls_lse & htonl(MPLS_LABEL_MASK));
871 return !(wc->masks.mpls_lse & htonl(MPLS_TC_MASK));
873 return !(wc->masks.mpls_lse & htonl(MPLS_BOS_MASK));
876 return !wc->masks.nw_src;
878 return !wc->masks.nw_dst;
881 return ipv6_mask_is_any(&wc->masks.ipv6_src);
883 return ipv6_mask_is_any(&wc->masks.ipv6_dst);
886 return !wc->masks.ipv6_label;
889 return !wc->masks.nw_proto;
891 case MFF_IP_DSCP_SHIFTED:
892 return !(wc->masks.nw_tos & IP_DSCP_MASK);
894 return !(wc->masks.nw_tos & IP_ECN_MASK);
896 return !wc->masks.nw_ttl;
899 return ipv6_mask_is_any(&wc->masks.nd_target);
902 return !(wc->masks.nw_frag & FLOW_NW_FRAG_MASK);
905 return !wc->masks.nw_proto;
907 return !wc->masks.nw_src;
909 return !wc->masks.nw_dst;
914 case MFF_ICMPV4_TYPE:
915 case MFF_ICMPV6_TYPE:
916 return !wc->masks.tp_src;
920 case MFF_ICMPV4_CODE:
921 case MFF_ICMPV6_CODE:
922 return !wc->masks.tp_dst;
930 /* Initializes 'mask' with the wildcard bit pattern for field 'mf' within 'wc'.
931 * Each bit in 'mask' will be set to 1 if the bit is significant for matching
932 * purposes, or to 0 if it is wildcarded.
934 * The caller is responsible for ensuring that 'wc' corresponds to a flow that
935 * meets 'mf''s prerequisites. */
937 mf_get_mask(const struct mf_field *mf, const struct flow_wildcards *wc,
938 union mf_value *mask)
940 mf_get_value(mf, &wc->masks, mask);
943 /* Tests whether 'mask' is a valid wildcard bit pattern for 'mf'. Returns true
944 * if the mask is valid, false otherwise. */
946 mf_is_mask_valid(const struct mf_field *mf, const union mf_value *mask)
948 switch (mf->maskable) {
950 return (is_all_zeros((const uint8_t *) mask, mf->n_bytes) ||
951 is_all_ones((const uint8_t *) mask, mf->n_bytes));
961 is_icmpv4(const struct flow *flow)
963 return (flow->dl_type == htons(ETH_TYPE_IP)
964 && flow->nw_proto == IPPROTO_ICMP);
968 is_icmpv6(const struct flow *flow)
970 return (flow->dl_type == htons(ETH_TYPE_IPV6)
971 && flow->nw_proto == IPPROTO_ICMPV6);
974 /* Returns true if 'flow' meets the prerequisites for 'mf', false otherwise. */
976 mf_are_prereqs_ok(const struct mf_field *mf, const struct flow *flow)
978 switch (mf->prereqs) {
983 return (flow->dl_type == htons(ETH_TYPE_ARP) ||
984 flow->dl_type == htons(ETH_TYPE_RARP));
986 return flow->dl_type == htons(ETH_TYPE_IP);
988 return flow->dl_type == htons(ETH_TYPE_IPV6);
990 return (flow->vlan_tci & htons(VLAN_CFI)) != 0;
992 return eth_type_mpls(flow->dl_type);
994 return is_ip_any(flow);
997 return is_ip_any(flow) && flow->nw_proto == IPPROTO_TCP;
999 return is_ip_any(flow) && flow->nw_proto == IPPROTO_UDP;
1001 return is_ip_any(flow) && flow->nw_proto == IPPROTO_SCTP;
1003 return is_icmpv4(flow);
1005 return is_icmpv6(flow);
1008 return (is_icmpv6(flow)
1009 && flow->tp_dst == htons(0)
1010 && (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT) ||
1011 flow->tp_src == htons(ND_NEIGHBOR_ADVERT)));
1012 case MFP_ND_SOLICIT:
1013 return (is_icmpv6(flow)
1014 && flow->tp_dst == htons(0)
1015 && (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT)));
1017 return (is_icmpv6(flow)
1018 && flow->tp_dst == htons(0)
1019 && (flow->tp_src == htons(ND_NEIGHBOR_ADVERT)));
1025 /* Returns true if 'value' may be a valid value *as part of a masked match*,
1028 * A value is not rejected just because it is not valid for the field in
1029 * question, but only if it doesn't make sense to test the bits in question at
1030 * all. For example, the MFF_VLAN_TCI field will never have a nonzero value
1031 * without the VLAN_CFI bit being set, but we can't reject those values because
1032 * it is still legitimate to test just for those bits (see the documentation
1033 * for NXM_OF_VLAN_TCI in nicira-ext.h). On the other hand, there is never a
1034 * reason to set the low bit of MFF_IP_DSCP to 1, so we reject that. */
1036 mf_is_value_valid(const struct mf_field *mf, const union mf_value *value)
1047 case MFF_SKB_PRIORITY:
1070 case MFF_ICMPV4_TYPE:
1071 case MFF_ICMPV4_CODE:
1072 case MFF_ICMPV6_TYPE:
1073 case MFF_ICMPV6_CODE:
1079 case MFF_IN_PORT_OXM: {
1081 return !ofputil_port_from_ofp11(value->be32, &port);
1085 return !(value->u8 & ~IP_DSCP_MASK);
1086 case MFF_IP_DSCP_SHIFTED:
1087 return !(value->u8 & (~IP_DSCP_MASK >> 2));
1089 return !(value->u8 & ~IP_ECN_MASK);
1091 return !(value->u8 & ~FLOW_NW_FRAG_MASK);
1094 return !(value->be16 & htons(0xff00));
1097 return !(value->be16 & htons(VLAN_CFI | VLAN_PCP_MASK));
1099 return !(value->be16 & htons(VLAN_PCP_MASK));
1101 case MFF_DL_VLAN_PCP:
1103 return !(value->u8 & ~(VLAN_PCP_MASK >> VLAN_PCP_SHIFT));
1105 case MFF_IPV6_LABEL:
1106 return !(value->be32 & ~htonl(IPV6_LABEL_MASK));
1108 case MFF_MPLS_LABEL:
1109 return !(value->be32 & ~htonl(MPLS_LABEL_MASK >> MPLS_LABEL_SHIFT));
1112 return !(value->u8 & ~(MPLS_TC_MASK >> MPLS_TC_SHIFT));
1115 return !(value->u8 & ~(MPLS_BOS_MASK >> MPLS_BOS_SHIFT));
1123 /* Copies the value of field 'mf' from 'flow' into 'value'. The caller is
1124 * responsible for ensuring that 'flow' meets 'mf''s prerequisites. */
1126 mf_get_value(const struct mf_field *mf, const struct flow *flow,
1127 union mf_value *value)
1131 value->be64 = flow->tunnel.tun_id;
1134 value->be32 = flow->tunnel.ip_src;
1137 value->be32 = flow->tunnel.ip_dst;
1140 value->be16 = htons(flow->tunnel.flags);
1143 value->u8 = flow->tunnel.ip_ttl;
1146 value->u8 = flow->tunnel.ip_tos;
1150 value->be64 = flow->metadata;
1154 value->be16 = htons(ofp_to_u16(flow->in_port.ofp_port));
1156 case MFF_IN_PORT_OXM:
1157 value->be32 = ofputil_port_to_ofp11(flow->in_port.ofp_port);
1160 case MFF_SKB_PRIORITY:
1161 value->be32 = htonl(flow->skb_priority);
1165 value->be32 = htonl(flow->pkt_mark);
1169 value->be32 = htonl(flow->regs[mf->id - MFF_REG0]);
1173 memcpy(value->mac, flow->dl_src, ETH_ADDR_LEN);
1177 memcpy(value->mac, flow->dl_dst, ETH_ADDR_LEN);
1181 value->be16 = flow->dl_type;
1185 value->be16 = flow->vlan_tci;
1189 value->be16 = flow->vlan_tci & htons(VLAN_VID_MASK);
1192 value->be16 = flow->vlan_tci & htons(VLAN_VID_MASK | VLAN_CFI);
1195 case MFF_DL_VLAN_PCP:
1197 value->u8 = vlan_tci_to_pcp(flow->vlan_tci);
1200 case MFF_MPLS_LABEL:
1201 value->be32 = htonl(mpls_lse_to_label(flow->mpls_lse));
1205 value->u8 = mpls_lse_to_tc(flow->mpls_lse);
1209 value->u8 = mpls_lse_to_bos(flow->mpls_lse);
1213 value->be32 = flow->nw_src;
1217 value->be32 = flow->nw_dst;
1221 value->ipv6 = flow->ipv6_src;
1225 value->ipv6 = flow->ipv6_dst;
1228 case MFF_IPV6_LABEL:
1229 value->be32 = flow->ipv6_label;
1233 value->u8 = flow->nw_proto;
1237 value->u8 = flow->nw_tos & IP_DSCP_MASK;
1240 case MFF_IP_DSCP_SHIFTED:
1241 value->u8 = flow->nw_tos >> 2;
1245 value->u8 = flow->nw_tos & IP_ECN_MASK;
1249 value->u8 = flow->nw_ttl;
1253 value->u8 = flow->nw_frag;
1257 value->be16 = htons(flow->nw_proto);
1261 value->be32 = flow->nw_src;
1265 value->be32 = flow->nw_dst;
1270 memcpy(value->mac, flow->arp_sha, ETH_ADDR_LEN);
1275 memcpy(value->mac, flow->arp_tha, ETH_ADDR_LEN);
1281 value->be16 = flow->tp_src;
1287 value->be16 = flow->tp_dst;
1290 case MFF_ICMPV4_TYPE:
1291 case MFF_ICMPV6_TYPE:
1292 value->u8 = ntohs(flow->tp_src);
1295 case MFF_ICMPV4_CODE:
1296 case MFF_ICMPV6_CODE:
1297 value->u8 = ntohs(flow->tp_dst);
1301 value->ipv6 = flow->nd_target;
1310 /* Makes 'match' match field 'mf' exactly, with the value matched taken from
1311 * 'value'. The caller is responsible for ensuring that 'match' meets 'mf''s
1314 mf_set_value(const struct mf_field *mf,
1315 const union mf_value *value, struct match *match)
1319 match_set_tun_id(match, value->be64);
1322 match_set_tun_src(match, value->be32);
1325 match_set_tun_dst(match, value->be32);
1328 match_set_tun_flags(match, ntohs(value->be16));
1331 match_set_tun_tos(match, value->u8);
1334 match_set_tun_ttl(match, value->u8);
1338 match_set_metadata(match, value->be64);
1342 match_set_in_port(match, u16_to_ofp(ntohs(value->be16)));
1345 case MFF_IN_PORT_OXM: {
1347 ofputil_port_from_ofp11(value->be32, &port);
1348 match_set_in_port(match, port);
1352 case MFF_SKB_PRIORITY:
1353 match_set_skb_priority(match, ntohl(value->be32));
1357 match_set_pkt_mark(match, ntohl(value->be32));
1361 match_set_reg(match, mf->id - MFF_REG0, ntohl(value->be32));
1365 match_set_dl_src(match, value->mac);
1369 match_set_dl_dst(match, value->mac);
1373 match_set_dl_type(match, value->be16);
1377 match_set_dl_tci(match, value->be16);
1381 match_set_dl_vlan(match, value->be16);
1384 match_set_vlan_vid(match, value->be16);
1387 case MFF_DL_VLAN_PCP:
1389 match_set_dl_vlan_pcp(match, value->u8);
1392 case MFF_MPLS_LABEL:
1393 match_set_mpls_label(match, value->be32);
1397 match_set_mpls_tc(match, value->u8);
1401 match_set_mpls_bos(match, value->u8);
1405 match_set_nw_src(match, value->be32);
1409 match_set_nw_dst(match, value->be32);
1413 match_set_ipv6_src(match, &value->ipv6);
1417 match_set_ipv6_dst(match, &value->ipv6);
1420 case MFF_IPV6_LABEL:
1421 match_set_ipv6_label(match, value->be32);
1425 match_set_nw_proto(match, value->u8);
1429 match_set_nw_dscp(match, value->u8);
1432 case MFF_IP_DSCP_SHIFTED:
1433 match_set_nw_dscp(match, value->u8 << 2);
1437 match_set_nw_ecn(match, value->u8);
1441 match_set_nw_ttl(match, value->u8);
1445 match_set_nw_frag(match, value->u8);
1449 match_set_nw_proto(match, ntohs(value->be16));
1453 match_set_nw_src(match, value->be32);
1457 match_set_nw_dst(match, value->be32);
1462 match_set_arp_sha(match, value->mac);
1467 match_set_arp_tha(match, value->mac);
1473 match_set_tp_src(match, value->be16);
1479 match_set_tp_dst(match, value->be16);
1482 case MFF_ICMPV4_TYPE:
1483 case MFF_ICMPV6_TYPE:
1484 match_set_icmp_type(match, value->u8);
1487 case MFF_ICMPV4_CODE:
1488 case MFF_ICMPV6_CODE:
1489 match_set_icmp_code(match, value->u8);
1493 match_set_nd_target(match, &value->ipv6);
1502 /* Sets 'flow' member field described by 'mf' to 'value'. The caller is
1503 * responsible for ensuring that 'flow' meets 'mf''s prerequisites.*/
1505 mf_set_flow_value(const struct mf_field *mf,
1506 const union mf_value *value, struct flow *flow)
1510 flow->tunnel.tun_id = value->be64;
1513 flow->tunnel.ip_src = value->be32;
1516 flow->tunnel.ip_dst = value->be32;
1519 flow->tunnel.flags = ntohs(value->be16);
1522 flow->tunnel.ip_tos = value->u8;
1525 flow->tunnel.ip_ttl = value->u8;
1529 flow->metadata = value->be64;
1533 flow->in_port.ofp_port = u16_to_ofp(ntohs(value->be16));
1536 case MFF_IN_PORT_OXM: {
1538 ofputil_port_from_ofp11(value->be32, &port);
1539 flow->in_port.ofp_port = port;
1543 case MFF_SKB_PRIORITY:
1544 flow->skb_priority = ntohl(value->be32);
1548 flow->pkt_mark = ntohl(value->be32);
1552 flow->regs[mf->id - MFF_REG0] = ntohl(value->be32);
1556 memcpy(flow->dl_src, value->mac, ETH_ADDR_LEN);
1560 memcpy(flow->dl_dst, value->mac, ETH_ADDR_LEN);
1564 flow->dl_type = value->be16;
1568 flow->vlan_tci = value->be16;
1572 flow_set_dl_vlan(flow, value->be16);
1575 flow_set_vlan_vid(flow, value->be16);
1578 case MFF_DL_VLAN_PCP:
1580 flow_set_vlan_pcp(flow, value->u8);
1583 case MFF_MPLS_LABEL:
1584 flow_set_mpls_label(flow, value->be32);
1588 flow_set_mpls_tc(flow, value->u8);
1592 flow_set_mpls_bos(flow, value->u8);
1596 flow->nw_src = value->be32;
1600 flow->nw_dst = value->be32;
1604 flow->ipv6_src = value->ipv6;
1608 flow->ipv6_dst = value->ipv6;
1611 case MFF_IPV6_LABEL:
1612 flow->ipv6_label = value->be32 & ~htonl(IPV6_LABEL_MASK);
1616 flow->nw_proto = value->u8;
1620 flow->nw_tos &= ~IP_DSCP_MASK;
1621 flow->nw_tos |= value->u8 & IP_DSCP_MASK;
1624 case MFF_IP_DSCP_SHIFTED:
1625 flow->nw_tos &= ~IP_DSCP_MASK;
1626 flow->nw_tos |= value->u8 << 2;
1630 flow->nw_tos &= ~IP_ECN_MASK;
1631 flow->nw_tos |= value->u8 & IP_ECN_MASK;
1635 flow->nw_ttl = value->u8;
1639 flow->nw_frag &= value->u8;
1643 flow->nw_proto = ntohs(value->be16);
1647 flow->nw_src = value->be32;
1651 flow->nw_dst = value->be32;
1656 memcpy(flow->arp_sha, value->mac, ETH_ADDR_LEN);
1661 memcpy(flow->arp_tha, value->mac, ETH_ADDR_LEN);
1667 flow->tp_src = value->be16;
1673 flow->tp_dst = value->be16;
1676 case MFF_ICMPV4_TYPE:
1677 case MFF_ICMPV6_TYPE:
1678 flow->tp_src = htons(value->u8);
1681 case MFF_ICMPV4_CODE:
1682 case MFF_ICMPV6_CODE:
1683 flow->tp_dst = htons(value->u8);
1687 flow->nd_target = value->ipv6;
1696 /* Returns true if 'mf' has a zero value in 'flow', false if it is nonzero.
1698 * The caller is responsible for ensuring that 'flow' meets 'mf''s
1701 mf_is_zero(const struct mf_field *mf, const struct flow *flow)
1703 union mf_value value;
1705 mf_get_value(mf, flow, &value);
1706 return is_all_zeros((const uint8_t *) &value, mf->n_bytes);
1709 /* Makes 'match' wildcard field 'mf'.
1711 * The caller is responsible for ensuring that 'match' meets 'mf''s
1714 mf_set_wild(const struct mf_field *mf, struct match *match)
1718 match_set_tun_id_masked(match, htonll(0), htonll(0));
1721 match_set_tun_src_masked(match, htonl(0), htonl(0));
1724 match_set_tun_dst_masked(match, htonl(0), htonl(0));
1727 match_set_tun_flags_masked(match, 0, 0);
1730 match_set_tun_tos_masked(match, 0, 0);
1733 match_set_tun_ttl_masked(match, 0, 0);
1737 match_set_metadata_masked(match, htonll(0), htonll(0));
1741 case MFF_IN_PORT_OXM:
1742 match->flow.in_port.ofp_port = 0;
1743 match->wc.masks.in_port.ofp_port = 0;
1746 case MFF_SKB_PRIORITY:
1747 match->flow.skb_priority = 0;
1748 match->wc.masks.skb_priority = 0;
1752 match->flow.pkt_mark = 0;
1753 match->wc.masks.pkt_mark = 0;
1757 match_set_reg_masked(match, mf->id - MFF_REG0, 0, 0);
1761 memset(match->flow.dl_src, 0, ETH_ADDR_LEN);
1762 memset(match->wc.masks.dl_src, 0, ETH_ADDR_LEN);
1766 memset(match->flow.dl_dst, 0, ETH_ADDR_LEN);
1767 memset(match->wc.masks.dl_dst, 0, ETH_ADDR_LEN);
1771 match->flow.dl_type = htons(0);
1772 match->wc.masks.dl_type = htons(0);
1776 match_set_dl_tci_masked(match, htons(0), htons(0));
1781 match_set_any_vid(match);
1784 case MFF_DL_VLAN_PCP:
1786 match_set_any_pcp(match);
1789 case MFF_MPLS_LABEL:
1790 match_set_any_mpls_label(match);
1794 match_set_any_mpls_tc(match);
1798 match_set_any_mpls_bos(match);
1803 match_set_nw_src_masked(match, htonl(0), htonl(0));
1808 match_set_nw_dst_masked(match, htonl(0), htonl(0));
1812 memset(&match->wc.masks.ipv6_src, 0, sizeof match->wc.masks.ipv6_src);
1813 memset(&match->flow.ipv6_src, 0, sizeof match->flow.ipv6_src);
1817 memset(&match->wc.masks.ipv6_dst, 0, sizeof match->wc.masks.ipv6_dst);
1818 memset(&match->flow.ipv6_dst, 0, sizeof match->flow.ipv6_dst);
1821 case MFF_IPV6_LABEL:
1822 match->wc.masks.ipv6_label = htonl(0);
1823 match->flow.ipv6_label = htonl(0);
1827 match->wc.masks.nw_proto = 0;
1828 match->flow.nw_proto = 0;
1832 case MFF_IP_DSCP_SHIFTED:
1833 match->wc.masks.nw_tos &= ~IP_DSCP_MASK;
1834 match->flow.nw_tos &= ~IP_DSCP_MASK;
1838 match->wc.masks.nw_tos &= ~IP_ECN_MASK;
1839 match->flow.nw_tos &= ~IP_ECN_MASK;
1843 match->wc.masks.nw_ttl = 0;
1844 match->flow.nw_ttl = 0;
1848 match->wc.masks.nw_frag |= FLOW_NW_FRAG_MASK;
1849 match->flow.nw_frag &= ~FLOW_NW_FRAG_MASK;
1853 match->wc.masks.nw_proto = 0;
1854 match->flow.nw_proto = 0;
1859 memset(match->flow.arp_sha, 0, ETH_ADDR_LEN);
1860 memset(match->wc.masks.arp_sha, 0, ETH_ADDR_LEN);
1865 memset(match->flow.arp_tha, 0, ETH_ADDR_LEN);
1866 memset(match->wc.masks.arp_tha, 0, ETH_ADDR_LEN);
1872 case MFF_ICMPV4_TYPE:
1873 case MFF_ICMPV6_TYPE:
1874 match->wc.masks.tp_src = htons(0);
1875 match->flow.tp_src = htons(0);
1881 case MFF_ICMPV4_CODE:
1882 case MFF_ICMPV6_CODE:
1883 match->wc.masks.tp_dst = htons(0);
1884 match->flow.tp_dst = htons(0);
1888 memset(&match->wc.masks.nd_target, 0,
1889 sizeof match->wc.masks.nd_target);
1890 memset(&match->flow.nd_target, 0, sizeof match->flow.nd_target);
1899 /* Makes 'match' match field 'mf' with the specified 'value' and 'mask'.
1900 * 'value' specifies a value to match and 'mask' specifies a wildcard pattern,
1901 * with a 1-bit indicating that the corresponding value bit must match and a
1902 * 0-bit indicating a don't-care.
1904 * If 'mask' is NULL or points to all-1-bits, then this call is equivalent to
1905 * mf_set_value(mf, value, match). If 'mask' points to all-0-bits, then this
1906 * call is equivalent to mf_set_wild(mf, match).
1908 * 'mask' must be a valid mask for 'mf' (see mf_is_mask_valid()). The caller
1909 * is responsible for ensuring that 'match' meets 'mf''s prerequisites. */
1910 enum ofputil_protocol
1911 mf_set(const struct mf_field *mf,
1912 const union mf_value *value, const union mf_value *mask,
1913 struct match *match)
1915 if (!mask || is_all_ones((const uint8_t *) mask, mf->n_bytes)) {
1916 mf_set_value(mf, value, match);
1917 return mf->usable_protocols;
1918 } else if (is_all_zeros((const uint8_t *) mask, mf->n_bytes)) {
1919 mf_set_wild(mf, match);
1920 return OFPUTIL_P_ANY;
1925 case MFF_IN_PORT_OXM:
1926 case MFF_SKB_PRIORITY:
1929 case MFF_DL_VLAN_PCP:
1931 case MFF_MPLS_LABEL:
1937 case MFF_IP_DSCP_SHIFTED:
1940 case MFF_ICMPV4_TYPE:
1941 case MFF_ICMPV4_CODE:
1942 case MFF_ICMPV6_TYPE:
1943 case MFF_ICMPV6_CODE:
1947 match_set_tun_id_masked(match, value->be64, mask->be64);
1950 match_set_tun_src_masked(match, value->be32, mask->be32);
1953 match_set_tun_dst_masked(match, value->be32, mask->be32);
1956 match_set_tun_flags_masked(match, ntohs(value->be16), ntohs(mask->be16));
1959 match_set_tun_ttl_masked(match, value->u8, mask->u8);
1962 match_set_tun_tos_masked(match, value->u8, mask->u8);
1966 match_set_metadata_masked(match, value->be64, mask->be64);
1970 match_set_reg_masked(match, mf->id - MFF_REG0,
1971 ntohl(value->be32), ntohl(mask->be32));
1975 match_set_pkt_mark_masked(match, ntohl(value->be32),
1980 match_set_dl_dst_masked(match, value->mac, mask->mac);
1984 match_set_dl_src_masked(match, value->mac, mask->mac);
1989 match_set_arp_sha_masked(match, value->mac, mask->mac);
1994 match_set_arp_tha_masked(match, value->mac, mask->mac);
1998 match_set_dl_tci_masked(match, value->be16, mask->be16);
2002 match_set_vlan_vid_masked(match, value->be16, mask->be16);
2006 match_set_nw_src_masked(match, value->be32, mask->be32);
2010 match_set_nw_dst_masked(match, value->be32, mask->be32);
2014 match_set_ipv6_src_masked(match, &value->ipv6, &mask->ipv6);
2018 match_set_ipv6_dst_masked(match, &value->ipv6, &mask->ipv6);
2021 case MFF_IPV6_LABEL:
2022 if ((mask->be32 & htonl(IPV6_LABEL_MASK)) == htonl(IPV6_LABEL_MASK)) {
2023 mf_set_value(mf, value, match);
2025 match_set_ipv6_label_masked(match, value->be32, mask->be32);
2030 match_set_nd_target_masked(match, &value->ipv6, &mask->ipv6);
2034 match_set_nw_frag_masked(match, value->u8, mask->u8);
2038 match_set_nw_src_masked(match, value->be32, mask->be32);
2042 match_set_nw_dst_masked(match, value->be32, mask->be32);
2048 match_set_tp_src_masked(match, value->be16, mask->be16);
2054 match_set_tp_dst_masked(match, value->be16, mask->be16);
2062 return mf->usable_protocols_bitwise;
2065 return ip_is_cidr(mask->be32) ? mf->usable_protocols :
2066 mf->usable_protocols_bitwise;
2070 mf_check__(const struct mf_subfield *sf, const struct flow *flow,
2074 VLOG_WARN_RL(&rl, "unknown %s field", type);
2075 return OFPERR_OFPBAC_BAD_SET_TYPE;
2076 } else if (!sf->n_bits) {
2077 VLOG_WARN_RL(&rl, "zero bit %s field %s", type, sf->field->name);
2078 return OFPERR_OFPBAC_BAD_SET_LEN;
2079 } else if (sf->ofs >= sf->field->n_bits) {
2080 VLOG_WARN_RL(&rl, "bit offset %d exceeds %d-bit width of %s field %s",
2081 sf->ofs, sf->field->n_bits, type, sf->field->name);
2082 return OFPERR_OFPBAC_BAD_SET_LEN;
2083 } else if (sf->ofs + sf->n_bits > sf->field->n_bits) {
2084 VLOG_WARN_RL(&rl, "bit offset %d and width %d exceeds %d-bit width "
2085 "of %s field %s", sf->ofs, sf->n_bits,
2086 sf->field->n_bits, type, sf->field->name);
2087 return OFPERR_OFPBAC_BAD_SET_LEN;
2088 } else if (flow && !mf_are_prereqs_ok(sf->field, flow)) {
2089 VLOG_WARN_RL(&rl, "%s field %s lacks correct prerequisites",
2090 type, sf->field->name);
2091 return OFPERR_OFPBAC_MATCH_INCONSISTENT;
2097 /* Checks whether 'sf' is valid for reading a subfield out of 'flow'. Returns
2098 * 0 if so, otherwise an OpenFlow error code (e.g. as returned by
2101 mf_check_src(const struct mf_subfield *sf, const struct flow *flow)
2103 return mf_check__(sf, flow, "source");
2106 /* Checks whether 'sf' is valid for writing a subfield into 'flow'. Returns 0
2107 * if so, otherwise an OpenFlow error code (e.g. as returned by
2110 mf_check_dst(const struct mf_subfield *sf, const struct flow *flow)
2112 int error = mf_check__(sf, flow, "destination");
2113 if (!error && !sf->field->writable) {
2114 VLOG_WARN_RL(&rl, "destination field %s is not writable",
2116 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
2121 /* Copies the value and wildcard bit pattern for 'mf' from 'match' into the
2122 * 'value' and 'mask', respectively. */
2124 mf_get(const struct mf_field *mf, const struct match *match,
2125 union mf_value *value, union mf_value *mask)
2127 mf_get_value(mf, &match->flow, value);
2128 mf_get_mask(mf, &match->wc, mask);
2131 /* Assigns a random value for field 'mf' to 'value'. */
2133 mf_random_value(const struct mf_field *mf, union mf_value *value)
2135 random_bytes(value, mf->n_bytes);
2147 case MFF_SKB_PRIORITY:
2169 case MFF_ICMPV4_TYPE:
2170 case MFF_ICMPV4_CODE:
2171 case MFF_ICMPV6_TYPE:
2172 case MFF_ICMPV6_CODE:
2178 case MFF_IN_PORT_OXM:
2179 value->be32 = ofputil_port_to_ofp11(u16_to_ofp(ntohs(value->be16)));
2182 case MFF_IPV6_LABEL:
2183 value->be32 &= ~htonl(IPV6_LABEL_MASK);
2187 value->u8 &= IP_DSCP_MASK;
2190 case MFF_IP_DSCP_SHIFTED:
2191 value->u8 &= IP_DSCP_MASK >> 2;
2195 value->u8 &= IP_ECN_MASK;
2199 value->u8 &= FLOW_NW_FRAG_MASK;
2203 value->be16 &= htons(0xff);
2207 value->be16 &= htons(VLAN_VID_MASK);
2210 value->be16 &= htons(VLAN_VID_MASK | VLAN_CFI);
2213 case MFF_DL_VLAN_PCP:
2218 case MFF_MPLS_LABEL:
2219 value->be32 &= htonl(MPLS_LABEL_MASK >> MPLS_LABEL_SHIFT);
2223 value->u8 &= MPLS_TC_MASK >> MPLS_TC_SHIFT;
2227 value->u8 &= MPLS_BOS_MASK >> MPLS_BOS_SHIFT;
2237 mf_from_integer_string(const struct mf_field *mf, const char *s,
2238 uint8_t *valuep, uint8_t *maskp)
2240 unsigned long long int integer, mask;
2245 integer = strtoull(s, &tail, 0);
2246 if (errno || (*tail != '\0' && *tail != '/')) {
2251 mask = strtoull(tail + 1, &tail, 0);
2252 if (errno || *tail != '\0') {
2259 for (i = mf->n_bytes - 1; i >= 0; i--) {
2260 valuep[i] = integer;
2266 return xasprintf("%s: value too large for %u-byte field %s",
2267 s, mf->n_bytes, mf->name);
2272 return xasprintf("%s: bad syntax for %s", s, mf->name);
2276 mf_from_ethernet_string(const struct mf_field *mf, const char *s,
2277 uint8_t mac[ETH_ADDR_LEN],
2278 uint8_t mask[ETH_ADDR_LEN])
2282 ovs_assert(mf->n_bytes == ETH_ADDR_LEN);
2285 if (sscanf(s, ETH_ADDR_SCAN_FMT"%n", ETH_ADDR_SCAN_ARGS(mac), &n) > 0
2286 && n == strlen(s)) {
2287 memset(mask, 0xff, ETH_ADDR_LEN);
2292 if (sscanf(s, ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT"%n",
2293 ETH_ADDR_SCAN_ARGS(mac), ETH_ADDR_SCAN_ARGS(mask), &n) > 0
2294 && n == strlen(s)) {
2298 return xasprintf("%s: invalid Ethernet address", s);
2302 mf_from_ipv4_string(const struct mf_field *mf, const char *s,
2303 ovs_be32 *ip, ovs_be32 *mask)
2307 ovs_assert(mf->n_bytes == sizeof *ip);
2309 if (sscanf(s, IP_SCAN_FMT"/"IP_SCAN_FMT,
2310 IP_SCAN_ARGS(ip), IP_SCAN_ARGS(mask)) == IP_SCAN_COUNT * 2) {
2312 } else if (sscanf(s, IP_SCAN_FMT"/%d",
2313 IP_SCAN_ARGS(ip), &prefix) == IP_SCAN_COUNT + 1) {
2314 if (prefix <= 0 || prefix > 32) {
2315 return xasprintf("%s: network prefix bits not between 1 and "
2317 } else if (prefix == 32) {
2318 *mask = htonl(UINT32_MAX);
2320 *mask = htonl(((1u << prefix) - 1) << (32 - prefix));
2322 } else if (sscanf(s, IP_SCAN_FMT, IP_SCAN_ARGS(ip)) == IP_SCAN_COUNT) {
2323 *mask = htonl(UINT32_MAX);
2325 return xasprintf("%s: invalid IP address", s);
2331 mf_from_ipv6_string(const struct mf_field *mf, const char *s,
2332 struct in6_addr *value, struct in6_addr *mask)
2334 char *str = xstrdup(s);
2335 char *save_ptr = NULL;
2336 const char *name, *netmask;
2339 ovs_assert(mf->n_bytes == sizeof *value);
2341 name = strtok_r(str, "/", &save_ptr);
2342 retval = name ? lookup_ipv6(name, value) : EINVAL;
2346 err = xasprintf("%s: could not convert to IPv6 address", str);
2352 netmask = strtok_r(NULL, "/", &save_ptr);
2354 if (inet_pton(AF_INET6, netmask, mask) != 1) {
2355 int prefix = atoi(netmask);
2356 if (prefix <= 0 || prefix > 128) {
2358 return xasprintf("%s: prefix bits not between 1 and 128", s);
2360 *mask = ipv6_create_mask(prefix);
2364 *mask = in6addr_exact;
2372 mf_from_ofp_port_string(const struct mf_field *mf, const char *s,
2373 ovs_be16 *valuep, ovs_be16 *maskp)
2377 ovs_assert(mf->n_bytes == sizeof(ovs_be16));
2379 if (ofputil_port_from_string(s, &port)) {
2380 *valuep = htons(ofp_to_u16(port));
2381 *maskp = htons(UINT16_MAX);
2384 return xasprintf("%s: port value out of range for %s", s, mf->name);
2388 mf_from_ofp_port_string32(const struct mf_field *mf, const char *s,
2389 ovs_be32 *valuep, ovs_be32 *maskp)
2393 ovs_assert(mf->n_bytes == sizeof(ovs_be32));
2394 if (ofputil_port_from_string(s, &port)) {
2395 *valuep = ofputil_port_to_ofp11(port);
2396 *maskp = htonl(UINT32_MAX);
2399 return xasprintf("%s: port value out of range for %s", s, mf->name);
2402 struct frag_handling {
2408 static const struct frag_handling all_frags[] = {
2409 #define A FLOW_NW_FRAG_ANY
2410 #define L FLOW_NW_FRAG_LATER
2411 /* name mask value */
2414 { "first", A|L, A },
2415 { "later", A|L, A|L },
2420 { "not_later", L, 0 },
2427 mf_from_frag_string(const char *s, uint8_t *valuep, uint8_t *maskp)
2429 const struct frag_handling *h;
2431 for (h = all_frags; h < &all_frags[ARRAY_SIZE(all_frags)]; h++) {
2432 if (!strcasecmp(s, h->name)) {
2433 /* We force the upper bits of the mask on to make mf_parse_value()
2434 * happy (otherwise it will never think it's an exact match.) */
2435 *maskp = h->mask | ~FLOW_NW_FRAG_MASK;
2441 return xasprintf("%s: unknown fragment type (valid types are \"no\", "
2442 "\"yes\", \"first\", \"later\", \"not_first\"", s);
2446 parse_flow_tun_flags(const char *s_, const char *(*bit_to_string)(uint32_t),
2449 uint32_t result = 0;
2450 char *save_ptr = NULL;
2453 char *s = xstrdup(s_);
2455 for (name = strtok_r((char *)s, " |", &save_ptr); name;
2456 name = strtok_r(NULL, " |", &save_ptr)) {
2458 unsigned long long int flags;
2462 if (sscanf(name, "%lli%n", &flags, &n0) > 0 && n0 > 0) {
2466 name_len = strlen(name);
2467 for (bit = 1; bit; bit <<= 1) {
2468 const char *fname = bit_to_string(bit);
2475 len = strlen(fname);
2476 if (len != name_len) {
2479 if (!strncmp(name, fname, len)) {
2491 *res = htons(result);
2498 mf_from_tun_flags_string(const char *s, ovs_be16 *valuep, ovs_be16 *maskp)
2500 if (!parse_flow_tun_flags(s, flow_tun_flag_to_string, valuep)) {
2501 *maskp = htons(UINT16_MAX);
2505 return xasprintf("%s: unknown tunnel flags (valid flags are \"df\", "
2506 "\"csum\", \"key\"", s);
2509 /* Parses 's', a string value for field 'mf', into 'value' and 'mask'. Returns
2510 * NULL if successful, otherwise a malloc()'d string describing the error. */
2512 mf_parse(const struct mf_field *mf, const char *s,
2513 union mf_value *value, union mf_value *mask)
2515 if (!strcmp(s, "*")) {
2516 memset(value, 0, mf->n_bytes);
2517 memset(mask, 0, mf->n_bytes);
2521 switch (mf->string) {
2523 case MFS_HEXADECIMAL:
2524 return mf_from_integer_string(mf, s,
2525 (uint8_t *) value, (uint8_t *) mask);
2528 return mf_from_ethernet_string(mf, s, value->mac, mask->mac);
2531 return mf_from_ipv4_string(mf, s, &value->be32, &mask->be32);
2534 return mf_from_ipv6_string(mf, s, &value->ipv6, &mask->ipv6);
2537 return mf_from_ofp_port_string(mf, s, &value->be16, &mask->be16);
2539 case MFS_OFP_PORT_OXM:
2540 return mf_from_ofp_port_string32(mf, s, &value->be32, &mask->be32);
2543 return mf_from_frag_string(s, &value->u8, &mask->u8);
2546 ovs_assert(mf->n_bytes == sizeof(ovs_be16));
2547 return mf_from_tun_flags_string(s, &value->be16, &mask->be16);
2552 /* Parses 's', a string value for field 'mf', into 'value'. Returns NULL if
2553 * successful, otherwise a malloc()'d string describing the error. */
2555 mf_parse_value(const struct mf_field *mf, const char *s, union mf_value *value)
2557 union mf_value mask;
2560 error = mf_parse(mf, s, value, &mask);
2565 if (!is_all_ones((const uint8_t *) &mask, mf->n_bytes)) {
2566 return xasprintf("%s: wildcards not allowed here", s);
2572 mf_format_integer_string(const struct mf_field *mf, const uint8_t *valuep,
2573 const uint8_t *maskp, struct ds *s)
2575 unsigned long long int integer;
2578 ovs_assert(mf->n_bytes <= 8);
2581 for (i = 0; i < mf->n_bytes; i++) {
2582 integer = (integer << 8) | valuep[i];
2584 if (mf->string == MFS_HEXADECIMAL) {
2585 ds_put_format(s, "%#llx", integer);
2587 ds_put_format(s, "%lld", integer);
2591 unsigned long long int mask;
2594 for (i = 0; i < mf->n_bytes; i++) {
2595 mask = (mask << 8) | maskp[i];
2598 /* I guess we could write the mask in decimal for MFS_DECIMAL but I'm
2599 * not sure that that a bit-mask written in decimal is ever easier to
2600 * understand than the same bit-mask written in hexadecimal. */
2601 ds_put_format(s, "/%#llx", mask);
2606 mf_format_frag_string(uint8_t value, uint8_t mask, struct ds *s)
2608 const struct frag_handling *h;
2610 mask &= FLOW_NW_FRAG_MASK;
2613 for (h = all_frags; h < &all_frags[ARRAY_SIZE(all_frags)]; h++) {
2614 if (value == h->value && mask == h->mask) {
2615 ds_put_cstr(s, h->name);
2619 ds_put_cstr(s, "<error>");
2623 mf_format_tnl_flags_string(const ovs_be16 *valuep, struct ds *s)
2625 format_flags(s, flow_tun_flag_to_string, ntohs(*valuep), '|');
2628 /* Appends to 's' a string representation of field 'mf' whose value is in
2629 * 'value' and 'mask'. 'mask' may be NULL to indicate an exact match. */
2631 mf_format(const struct mf_field *mf,
2632 const union mf_value *value, const union mf_value *mask,
2636 if (is_all_zeros((const uint8_t *) mask, mf->n_bytes)) {
2637 ds_put_cstr(s, "ANY");
2639 } else if (is_all_ones((const uint8_t *) mask, mf->n_bytes)) {
2644 switch (mf->string) {
2645 case MFS_OFP_PORT_OXM:
2648 ofputil_port_from_ofp11(value->be32, &port);
2649 ofputil_format_port(port, s);
2655 ofputil_format_port(u16_to_ofp(ntohs(value->be16)), s);
2660 case MFS_HEXADECIMAL:
2661 mf_format_integer_string(mf, (uint8_t *) value, (uint8_t *) mask, s);
2665 eth_format_masked(value->mac, mask->mac, s);
2669 ip_format_masked(value->be32, mask ? mask->be32 : htonl(UINT32_MAX),
2674 print_ipv6_masked(s, &value->ipv6, mask ? &mask->ipv6 : NULL);
2678 mf_format_frag_string(value->u8, mask ? mask->u8 : UINT8_MAX, s);
2682 mf_format_tnl_flags_string(&value->be16, s);
2690 /* Makes subfield 'sf' within 'flow' exactly match the 'sf->n_bits'
2691 * least-significant bits in 'x'.
2694 mf_write_subfield_flow(const struct mf_subfield *sf,
2695 const union mf_subvalue *x, struct flow *flow)
2697 const struct mf_field *field = sf->field;
2698 union mf_value value;
2700 mf_get_value(field, flow, &value);
2701 bitwise_copy(x, sizeof *x, 0, &value, field->n_bytes,
2702 sf->ofs, sf->n_bits);
2703 mf_set_flow_value(field, &value, flow);
2706 /* Makes subfield 'sf' within 'match' exactly match the 'sf->n_bits'
2707 * least-significant bits in 'x'.
2710 mf_write_subfield(const struct mf_subfield *sf, const union mf_subvalue *x,
2711 struct match *match)
2713 const struct mf_field *field = sf->field;
2714 union mf_value value, mask;
2716 mf_get(field, match, &value, &mask);
2717 bitwise_copy(x, sizeof *x, 0, &value, field->n_bytes, sf->ofs, sf->n_bits);
2718 bitwise_one ( &mask, field->n_bytes, sf->ofs, sf->n_bits);
2719 mf_set(field, &value, &mask, match);
2722 /* Initializes 'x' to the value of 'sf' within 'flow'. 'sf' must be valid for
2723 * reading 'flow', e.g. as checked by mf_check_src(). */
2725 mf_read_subfield(const struct mf_subfield *sf, const struct flow *flow,
2726 union mf_subvalue *x)
2728 union mf_value value;
2730 mf_get_value(sf->field, flow, &value);
2732 memset(x, 0, sizeof *x);
2733 bitwise_copy(&value, sf->field->n_bytes, sf->ofs,
2738 /* Returns the value of 'sf' within 'flow'. 'sf' must be valid for reading
2739 * 'flow', e.g. as checked by mf_check_src() and sf->n_bits must be 64 or
2742 mf_get_subfield(const struct mf_subfield *sf, const struct flow *flow)
2744 union mf_value value;
2746 mf_get_value(sf->field, flow, &value);
2747 return bitwise_get(&value, sf->field->n_bytes, sf->ofs, sf->n_bits);
2750 /* Formats 'sf' into 's' in a format normally acceptable to
2751 * mf_parse_subfield(). (It won't be acceptable if sf->field is NULL or if
2752 * sf->field has no NXM name.) */
2754 mf_format_subfield(const struct mf_subfield *sf, struct ds *s)
2757 ds_put_cstr(s, "<unknown>");
2758 } else if (sf->field->nxm_name) {
2759 ds_put_cstr(s, sf->field->nxm_name);
2760 } else if (sf->field->nxm_header) {
2761 uint32_t header = sf->field->nxm_header;
2762 ds_put_format(s, "%d:%d", NXM_VENDOR(header), NXM_FIELD(header));
2764 ds_put_cstr(s, sf->field->name);
2767 if (sf->field && sf->ofs == 0 && sf->n_bits == sf->field->n_bits) {
2768 ds_put_cstr(s, "[]");
2769 } else if (sf->n_bits == 1) {
2770 ds_put_format(s, "[%d]", sf->ofs);
2772 ds_put_format(s, "[%d..%d]", sf->ofs, sf->ofs + sf->n_bits - 1);
2776 static const struct mf_field *
2777 mf_parse_subfield_name(const char *name, int name_len, bool *wild)
2781 *wild = name_len > 2 && !memcmp(&name[name_len - 2], "_W", 2);
2786 for (i = 0; i < MFF_N_IDS; i++) {
2787 const struct mf_field *mf = mf_from_id(i);
2790 && !strncmp(mf->nxm_name, name, name_len)
2791 && mf->nxm_name[name_len] == '\0') {
2795 && !strncmp(mf->oxm_name, name, name_len)
2796 && mf->oxm_name[name_len] == '\0') {
2804 /* Parses a subfield from the beginning of '*sp' into 'sf'. If successful,
2805 * returns NULL and advances '*sp' to the first byte following the parsed
2806 * string. On failure, returns a malloc()'d error message, does not modify
2807 * '*sp', and does not properly initialize 'sf'.
2809 * The syntax parsed from '*sp' takes the form "header[start..end]" where
2810 * 'header' is the name of an NXM field and 'start' and 'end' are (inclusive)
2811 * bit indexes. "..end" may be omitted to indicate a single bit. "start..end"
2812 * may both be omitted (the [] are still required) to indicate an entire
2814 char * WARN_UNUSED_RESULT
2815 mf_parse_subfield__(struct mf_subfield *sf, const char **sp)
2817 const struct mf_field *field;
2826 name_len = strcspn(s, "[");
2827 if (s[name_len] != '[') {
2828 return xasprintf("%s: missing [ looking for field name", *sp);
2831 field = mf_parse_subfield_name(name, name_len, &wild);
2833 return xasprintf("%s: unknown field `%.*s'", *sp, name_len, s);
2837 if (sscanf(s, "[%d..%d]", &start, &end) == 2) {
2838 /* Nothing to do. */
2839 } else if (sscanf(s, "[%d]", &start) == 1) {
2841 } else if (!strncmp(s, "[]", 2)) {
2843 end = field->n_bits - 1;
2845 return xasprintf("%s: syntax error expecting [] or [<bit>] or "
2846 "[<start>..<end>]", *sp);
2848 s = strchr(s, ']') + 1;
2851 return xasprintf("%s: starting bit %d is after ending bit %d",
2853 } else if (start >= field->n_bits) {
2854 return xasprintf("%s: starting bit %d is not valid because field is "
2855 "only %d bits wide", *sp, start, field->n_bits);
2856 } else if (end >= field->n_bits){
2857 return xasprintf("%s: ending bit %d is not valid because field is "
2858 "only %d bits wide", *sp, end, field->n_bits);
2863 sf->n_bits = end - start + 1;
2869 /* Parses a subfield from the entirety of 's' into 'sf'. Returns NULL if
2870 * successful, otherwise a malloc()'d string describing the error. The caller
2871 * is responsible for freeing the returned string.
2873 * The syntax parsed from 's' takes the form "header[start..end]" where
2874 * 'header' is the name of an NXM field and 'start' and 'end' are (inclusive)
2875 * bit indexes. "..end" may be omitted to indicate a single bit. "start..end"
2876 * may both be omitted (the [] are still required) to indicate an entire
2878 char * WARN_UNUSED_RESULT
2879 mf_parse_subfield(struct mf_subfield *sf, const char *s)
2881 char *error = mf_parse_subfield__(sf, &s);
2882 if (!error && s[0]) {
2883 error = xstrdup("unexpected input following field syntax");
2889 mf_format_subvalue(const union mf_subvalue *subvalue, struct ds *s)
2893 for (i = 0; i < ARRAY_SIZE(subvalue->u8); i++) {
2894 if (subvalue->u8[i]) {
2895 ds_put_format(s, "0x%"PRIx8, subvalue->u8[i]);
2896 for (i++; i < ARRAY_SIZE(subvalue->u8); i++) {
2897 ds_put_format(s, "%02"PRIx8, subvalue->u8[i]);
2902 ds_put_char(s, '0');