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));
960 /* Returns true if 'flow' meets the prerequisites for 'mf', false otherwise. */
962 mf_are_prereqs_ok(const struct mf_field *mf, const struct flow *flow)
964 switch (mf->prereqs) {
969 return (flow->dl_type == htons(ETH_TYPE_ARP) ||
970 flow->dl_type == htons(ETH_TYPE_RARP));
972 return flow->dl_type == htons(ETH_TYPE_IP);
974 return flow->dl_type == htons(ETH_TYPE_IPV6);
976 return (flow->vlan_tci & htons(VLAN_CFI)) != 0;
978 return eth_type_mpls(flow->dl_type);
980 return is_ip_any(flow);
983 return is_ip_any(flow) && flow->nw_proto == IPPROTO_TCP;
985 return is_ip_any(flow) && flow->nw_proto == IPPROTO_UDP;
987 return is_ip_any(flow) && flow->nw_proto == IPPROTO_SCTP;
989 return is_icmpv4(flow);
991 return is_icmpv6(flow);
994 return (is_icmpv6(flow)
995 && flow->tp_dst == htons(0)
996 && (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT) ||
997 flow->tp_src == htons(ND_NEIGHBOR_ADVERT)));
999 return (is_icmpv6(flow)
1000 && flow->tp_dst == htons(0)
1001 && (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT)));
1003 return (is_icmpv6(flow)
1004 && flow->tp_dst == htons(0)
1005 && (flow->tp_src == htons(ND_NEIGHBOR_ADVERT)));
1011 /* Returns true if 'value' may be a valid value *as part of a masked match*,
1014 * A value is not rejected just because it is not valid for the field in
1015 * question, but only if it doesn't make sense to test the bits in question at
1016 * all. For example, the MFF_VLAN_TCI field will never have a nonzero value
1017 * without the VLAN_CFI bit being set, but we can't reject those values because
1018 * it is still legitimate to test just for those bits (see the documentation
1019 * for NXM_OF_VLAN_TCI in nicira-ext.h). On the other hand, there is never a
1020 * reason to set the low bit of MFF_IP_DSCP to 1, so we reject that. */
1022 mf_is_value_valid(const struct mf_field *mf, const union mf_value *value)
1033 case MFF_SKB_PRIORITY:
1056 case MFF_ICMPV4_TYPE:
1057 case MFF_ICMPV4_CODE:
1058 case MFF_ICMPV6_TYPE:
1059 case MFF_ICMPV6_CODE:
1065 case MFF_IN_PORT_OXM: {
1067 return !ofputil_port_from_ofp11(value->be32, &port);
1071 return !(value->u8 & ~IP_DSCP_MASK);
1072 case MFF_IP_DSCP_SHIFTED:
1073 return !(value->u8 & (~IP_DSCP_MASK >> 2));
1075 return !(value->u8 & ~IP_ECN_MASK);
1077 return !(value->u8 & ~FLOW_NW_FRAG_MASK);
1080 return !(value->be16 & htons(0xff00));
1083 return !(value->be16 & htons(VLAN_CFI | VLAN_PCP_MASK));
1085 return !(value->be16 & htons(VLAN_PCP_MASK));
1087 case MFF_DL_VLAN_PCP:
1089 return !(value->u8 & ~(VLAN_PCP_MASK >> VLAN_PCP_SHIFT));
1091 case MFF_IPV6_LABEL:
1092 return !(value->be32 & ~htonl(IPV6_LABEL_MASK));
1094 case MFF_MPLS_LABEL:
1095 return !(value->be32 & ~htonl(MPLS_LABEL_MASK >> MPLS_LABEL_SHIFT));
1098 return !(value->u8 & ~(MPLS_TC_MASK >> MPLS_TC_SHIFT));
1101 return !(value->u8 & ~(MPLS_BOS_MASK >> MPLS_BOS_SHIFT));
1109 /* Copies the value of field 'mf' from 'flow' into 'value'. The caller is
1110 * responsible for ensuring that 'flow' meets 'mf''s prerequisites. */
1112 mf_get_value(const struct mf_field *mf, const struct flow *flow,
1113 union mf_value *value)
1117 value->be64 = flow->tunnel.tun_id;
1120 value->be32 = flow->tunnel.ip_src;
1123 value->be32 = flow->tunnel.ip_dst;
1126 value->be16 = htons(flow->tunnel.flags);
1129 value->u8 = flow->tunnel.ip_ttl;
1132 value->u8 = flow->tunnel.ip_tos;
1136 value->be64 = flow->metadata;
1140 value->be16 = htons(ofp_to_u16(flow->in_port.ofp_port));
1142 case MFF_IN_PORT_OXM:
1143 value->be32 = ofputil_port_to_ofp11(flow->in_port.ofp_port);
1146 case MFF_SKB_PRIORITY:
1147 value->be32 = htonl(flow->skb_priority);
1151 value->be32 = htonl(flow->pkt_mark);
1155 value->be32 = htonl(flow->regs[mf->id - MFF_REG0]);
1159 memcpy(value->mac, flow->dl_src, ETH_ADDR_LEN);
1163 memcpy(value->mac, flow->dl_dst, ETH_ADDR_LEN);
1167 value->be16 = flow->dl_type;
1171 value->be16 = flow->vlan_tci;
1175 value->be16 = flow->vlan_tci & htons(VLAN_VID_MASK);
1178 value->be16 = flow->vlan_tci & htons(VLAN_VID_MASK | VLAN_CFI);
1181 case MFF_DL_VLAN_PCP:
1183 value->u8 = vlan_tci_to_pcp(flow->vlan_tci);
1186 case MFF_MPLS_LABEL:
1187 value->be32 = htonl(mpls_lse_to_label(flow->mpls_lse));
1191 value->u8 = mpls_lse_to_tc(flow->mpls_lse);
1195 value->u8 = mpls_lse_to_bos(flow->mpls_lse);
1199 value->be32 = flow->nw_src;
1203 value->be32 = flow->nw_dst;
1207 value->ipv6 = flow->ipv6_src;
1211 value->ipv6 = flow->ipv6_dst;
1214 case MFF_IPV6_LABEL:
1215 value->be32 = flow->ipv6_label;
1219 value->u8 = flow->nw_proto;
1223 value->u8 = flow->nw_tos & IP_DSCP_MASK;
1226 case MFF_IP_DSCP_SHIFTED:
1227 value->u8 = flow->nw_tos >> 2;
1231 value->u8 = flow->nw_tos & IP_ECN_MASK;
1235 value->u8 = flow->nw_ttl;
1239 value->u8 = flow->nw_frag;
1243 value->be16 = htons(flow->nw_proto);
1247 value->be32 = flow->nw_src;
1251 value->be32 = flow->nw_dst;
1256 memcpy(value->mac, flow->arp_sha, ETH_ADDR_LEN);
1261 memcpy(value->mac, flow->arp_tha, ETH_ADDR_LEN);
1267 value->be16 = flow->tp_src;
1273 value->be16 = flow->tp_dst;
1276 case MFF_ICMPV4_TYPE:
1277 case MFF_ICMPV6_TYPE:
1278 value->u8 = ntohs(flow->tp_src);
1281 case MFF_ICMPV4_CODE:
1282 case MFF_ICMPV6_CODE:
1283 value->u8 = ntohs(flow->tp_dst);
1287 value->ipv6 = flow->nd_target;
1296 /* Makes 'match' match field 'mf' exactly, with the value matched taken from
1297 * 'value'. The caller is responsible for ensuring that 'match' meets 'mf''s
1300 mf_set_value(const struct mf_field *mf,
1301 const union mf_value *value, struct match *match)
1305 match_set_tun_id(match, value->be64);
1308 match_set_tun_src(match, value->be32);
1311 match_set_tun_dst(match, value->be32);
1314 match_set_tun_flags(match, ntohs(value->be16));
1317 match_set_tun_tos(match, value->u8);
1320 match_set_tun_ttl(match, value->u8);
1324 match_set_metadata(match, value->be64);
1328 match_set_in_port(match, u16_to_ofp(ntohs(value->be16)));
1331 case MFF_IN_PORT_OXM: {
1333 ofputil_port_from_ofp11(value->be32, &port);
1334 match_set_in_port(match, port);
1338 case MFF_SKB_PRIORITY:
1339 match_set_skb_priority(match, ntohl(value->be32));
1343 match_set_pkt_mark(match, ntohl(value->be32));
1347 match_set_reg(match, mf->id - MFF_REG0, ntohl(value->be32));
1351 match_set_dl_src(match, value->mac);
1355 match_set_dl_dst(match, value->mac);
1359 match_set_dl_type(match, value->be16);
1363 match_set_dl_tci(match, value->be16);
1367 match_set_dl_vlan(match, value->be16);
1370 match_set_vlan_vid(match, value->be16);
1373 case MFF_DL_VLAN_PCP:
1375 match_set_dl_vlan_pcp(match, value->u8);
1378 case MFF_MPLS_LABEL:
1379 match_set_mpls_label(match, value->be32);
1383 match_set_mpls_tc(match, value->u8);
1387 match_set_mpls_bos(match, value->u8);
1391 match_set_nw_src(match, value->be32);
1395 match_set_nw_dst(match, value->be32);
1399 match_set_ipv6_src(match, &value->ipv6);
1403 match_set_ipv6_dst(match, &value->ipv6);
1406 case MFF_IPV6_LABEL:
1407 match_set_ipv6_label(match, value->be32);
1411 match_set_nw_proto(match, value->u8);
1415 match_set_nw_dscp(match, value->u8);
1418 case MFF_IP_DSCP_SHIFTED:
1419 match_set_nw_dscp(match, value->u8 << 2);
1423 match_set_nw_ecn(match, value->u8);
1427 match_set_nw_ttl(match, value->u8);
1431 match_set_nw_frag(match, value->u8);
1435 match_set_nw_proto(match, ntohs(value->be16));
1439 match_set_nw_src(match, value->be32);
1443 match_set_nw_dst(match, value->be32);
1448 match_set_arp_sha(match, value->mac);
1453 match_set_arp_tha(match, value->mac);
1459 match_set_tp_src(match, value->be16);
1465 match_set_tp_dst(match, value->be16);
1468 case MFF_ICMPV4_TYPE:
1469 case MFF_ICMPV6_TYPE:
1470 match_set_icmp_type(match, value->u8);
1473 case MFF_ICMPV4_CODE:
1474 case MFF_ICMPV6_CODE:
1475 match_set_icmp_code(match, value->u8);
1479 match_set_nd_target(match, &value->ipv6);
1488 /* Sets 'flow' member field described by 'mf' to 'value'. The caller is
1489 * responsible for ensuring that 'flow' meets 'mf''s prerequisites.*/
1491 mf_set_flow_value(const struct mf_field *mf,
1492 const union mf_value *value, struct flow *flow)
1496 flow->tunnel.tun_id = value->be64;
1499 flow->tunnel.ip_src = value->be32;
1502 flow->tunnel.ip_dst = value->be32;
1505 flow->tunnel.flags = ntohs(value->be16);
1508 flow->tunnel.ip_tos = value->u8;
1511 flow->tunnel.ip_ttl = value->u8;
1515 flow->metadata = value->be64;
1519 flow->in_port.ofp_port = u16_to_ofp(ntohs(value->be16));
1522 case MFF_IN_PORT_OXM: {
1524 ofputil_port_from_ofp11(value->be32, &port);
1525 flow->in_port.ofp_port = port;
1529 case MFF_SKB_PRIORITY:
1530 flow->skb_priority = ntohl(value->be32);
1534 flow->pkt_mark = ntohl(value->be32);
1538 flow->regs[mf->id - MFF_REG0] = ntohl(value->be32);
1542 memcpy(flow->dl_src, value->mac, ETH_ADDR_LEN);
1546 memcpy(flow->dl_dst, value->mac, ETH_ADDR_LEN);
1550 flow->dl_type = value->be16;
1554 flow->vlan_tci = value->be16;
1558 flow_set_dl_vlan(flow, value->be16);
1561 flow_set_vlan_vid(flow, value->be16);
1564 case MFF_DL_VLAN_PCP:
1566 flow_set_vlan_pcp(flow, value->u8);
1569 case MFF_MPLS_LABEL:
1570 flow_set_mpls_label(flow, value->be32);
1574 flow_set_mpls_tc(flow, value->u8);
1578 flow_set_mpls_bos(flow, value->u8);
1582 flow->nw_src = value->be32;
1586 flow->nw_dst = value->be32;
1590 flow->ipv6_src = value->ipv6;
1594 flow->ipv6_dst = value->ipv6;
1597 case MFF_IPV6_LABEL:
1598 flow->ipv6_label = value->be32 & ~htonl(IPV6_LABEL_MASK);
1602 flow->nw_proto = value->u8;
1606 flow->nw_tos &= ~IP_DSCP_MASK;
1607 flow->nw_tos |= value->u8 & IP_DSCP_MASK;
1610 case MFF_IP_DSCP_SHIFTED:
1611 flow->nw_tos &= ~IP_DSCP_MASK;
1612 flow->nw_tos |= value->u8 << 2;
1616 flow->nw_tos &= ~IP_ECN_MASK;
1617 flow->nw_tos |= value->u8 & IP_ECN_MASK;
1621 flow->nw_ttl = value->u8;
1625 flow->nw_frag &= value->u8;
1629 flow->nw_proto = ntohs(value->be16);
1633 flow->nw_src = value->be32;
1637 flow->nw_dst = value->be32;
1642 memcpy(flow->arp_sha, value->mac, ETH_ADDR_LEN);
1647 memcpy(flow->arp_tha, value->mac, ETH_ADDR_LEN);
1653 flow->tp_src = value->be16;
1659 flow->tp_dst = value->be16;
1662 case MFF_ICMPV4_TYPE:
1663 case MFF_ICMPV6_TYPE:
1664 flow->tp_src = htons(value->u8);
1667 case MFF_ICMPV4_CODE:
1668 case MFF_ICMPV6_CODE:
1669 flow->tp_dst = htons(value->u8);
1673 flow->nd_target = value->ipv6;
1682 /* Returns true if 'mf' has a zero value in 'flow', false if it is nonzero.
1684 * The caller is responsible for ensuring that 'flow' meets 'mf''s
1687 mf_is_zero(const struct mf_field *mf, const struct flow *flow)
1689 union mf_value value;
1691 mf_get_value(mf, flow, &value);
1692 return is_all_zeros((const uint8_t *) &value, mf->n_bytes);
1695 /* Makes 'match' wildcard field 'mf'.
1697 * The caller is responsible for ensuring that 'match' meets 'mf''s
1700 mf_set_wild(const struct mf_field *mf, struct match *match)
1704 match_set_tun_id_masked(match, htonll(0), htonll(0));
1707 match_set_tun_src_masked(match, htonl(0), htonl(0));
1710 match_set_tun_dst_masked(match, htonl(0), htonl(0));
1713 match_set_tun_flags_masked(match, 0, 0);
1716 match_set_tun_tos_masked(match, 0, 0);
1719 match_set_tun_ttl_masked(match, 0, 0);
1723 match_set_metadata_masked(match, htonll(0), htonll(0));
1727 case MFF_IN_PORT_OXM:
1728 match->flow.in_port.ofp_port = 0;
1729 match->wc.masks.in_port.ofp_port = 0;
1732 case MFF_SKB_PRIORITY:
1733 match->flow.skb_priority = 0;
1734 match->wc.masks.skb_priority = 0;
1738 match->flow.pkt_mark = 0;
1739 match->wc.masks.pkt_mark = 0;
1743 match_set_reg_masked(match, mf->id - MFF_REG0, 0, 0);
1747 memset(match->flow.dl_src, 0, ETH_ADDR_LEN);
1748 memset(match->wc.masks.dl_src, 0, ETH_ADDR_LEN);
1752 memset(match->flow.dl_dst, 0, ETH_ADDR_LEN);
1753 memset(match->wc.masks.dl_dst, 0, ETH_ADDR_LEN);
1757 match->flow.dl_type = htons(0);
1758 match->wc.masks.dl_type = htons(0);
1762 match_set_dl_tci_masked(match, htons(0), htons(0));
1767 match_set_any_vid(match);
1770 case MFF_DL_VLAN_PCP:
1772 match_set_any_pcp(match);
1775 case MFF_MPLS_LABEL:
1776 match_set_any_mpls_label(match);
1780 match_set_any_mpls_tc(match);
1784 match_set_any_mpls_bos(match);
1789 match_set_nw_src_masked(match, htonl(0), htonl(0));
1794 match_set_nw_dst_masked(match, htonl(0), htonl(0));
1798 memset(&match->wc.masks.ipv6_src, 0, sizeof match->wc.masks.ipv6_src);
1799 memset(&match->flow.ipv6_src, 0, sizeof match->flow.ipv6_src);
1803 memset(&match->wc.masks.ipv6_dst, 0, sizeof match->wc.masks.ipv6_dst);
1804 memset(&match->flow.ipv6_dst, 0, sizeof match->flow.ipv6_dst);
1807 case MFF_IPV6_LABEL:
1808 match->wc.masks.ipv6_label = htonl(0);
1809 match->flow.ipv6_label = htonl(0);
1813 match->wc.masks.nw_proto = 0;
1814 match->flow.nw_proto = 0;
1818 case MFF_IP_DSCP_SHIFTED:
1819 match->wc.masks.nw_tos &= ~IP_DSCP_MASK;
1820 match->flow.nw_tos &= ~IP_DSCP_MASK;
1824 match->wc.masks.nw_tos &= ~IP_ECN_MASK;
1825 match->flow.nw_tos &= ~IP_ECN_MASK;
1829 match->wc.masks.nw_ttl = 0;
1830 match->flow.nw_ttl = 0;
1834 match->wc.masks.nw_frag |= FLOW_NW_FRAG_MASK;
1835 match->flow.nw_frag &= ~FLOW_NW_FRAG_MASK;
1839 match->wc.masks.nw_proto = 0;
1840 match->flow.nw_proto = 0;
1845 memset(match->flow.arp_sha, 0, ETH_ADDR_LEN);
1846 memset(match->wc.masks.arp_sha, 0, ETH_ADDR_LEN);
1851 memset(match->flow.arp_tha, 0, ETH_ADDR_LEN);
1852 memset(match->wc.masks.arp_tha, 0, ETH_ADDR_LEN);
1858 case MFF_ICMPV4_TYPE:
1859 case MFF_ICMPV6_TYPE:
1860 match->wc.masks.tp_src = htons(0);
1861 match->flow.tp_src = htons(0);
1867 case MFF_ICMPV4_CODE:
1868 case MFF_ICMPV6_CODE:
1869 match->wc.masks.tp_dst = htons(0);
1870 match->flow.tp_dst = htons(0);
1874 memset(&match->wc.masks.nd_target, 0,
1875 sizeof match->wc.masks.nd_target);
1876 memset(&match->flow.nd_target, 0, sizeof match->flow.nd_target);
1885 /* Makes 'match' match field 'mf' with the specified 'value' and 'mask'.
1886 * 'value' specifies a value to match and 'mask' specifies a wildcard pattern,
1887 * with a 1-bit indicating that the corresponding value bit must match and a
1888 * 0-bit indicating a don't-care.
1890 * If 'mask' is NULL or points to all-1-bits, then this call is equivalent to
1891 * mf_set_value(mf, value, match). If 'mask' points to all-0-bits, then this
1892 * call is equivalent to mf_set_wild(mf, match).
1894 * 'mask' must be a valid mask for 'mf' (see mf_is_mask_valid()). The caller
1895 * is responsible for ensuring that 'match' meets 'mf''s prerequisites. */
1896 enum ofputil_protocol
1897 mf_set(const struct mf_field *mf,
1898 const union mf_value *value, const union mf_value *mask,
1899 struct match *match)
1901 if (!mask || is_all_ones((const uint8_t *) mask, mf->n_bytes)) {
1902 mf_set_value(mf, value, match);
1903 return mf->usable_protocols;
1904 } else if (is_all_zeros((const uint8_t *) mask, mf->n_bytes)) {
1905 mf_set_wild(mf, match);
1906 return OFPUTIL_P_ANY;
1911 case MFF_IN_PORT_OXM:
1912 case MFF_SKB_PRIORITY:
1915 case MFF_DL_VLAN_PCP:
1917 case MFF_MPLS_LABEL:
1923 case MFF_IP_DSCP_SHIFTED:
1926 case MFF_ICMPV4_TYPE:
1927 case MFF_ICMPV4_CODE:
1928 case MFF_ICMPV6_TYPE:
1929 case MFF_ICMPV6_CODE:
1933 match_set_tun_id_masked(match, value->be64, mask->be64);
1936 match_set_tun_src_masked(match, value->be32, mask->be32);
1939 match_set_tun_dst_masked(match, value->be32, mask->be32);
1942 match_set_tun_flags_masked(match, ntohs(value->be16), ntohs(mask->be16));
1945 match_set_tun_ttl_masked(match, value->u8, mask->u8);
1948 match_set_tun_tos_masked(match, value->u8, mask->u8);
1952 match_set_metadata_masked(match, value->be64, mask->be64);
1956 match_set_reg_masked(match, mf->id - MFF_REG0,
1957 ntohl(value->be32), ntohl(mask->be32));
1961 match_set_pkt_mark_masked(match, ntohl(value->be32),
1966 match_set_dl_dst_masked(match, value->mac, mask->mac);
1970 match_set_dl_src_masked(match, value->mac, mask->mac);
1975 match_set_arp_sha_masked(match, value->mac, mask->mac);
1980 match_set_arp_tha_masked(match, value->mac, mask->mac);
1984 match_set_dl_tci_masked(match, value->be16, mask->be16);
1988 match_set_vlan_vid_masked(match, value->be16, mask->be16);
1992 match_set_nw_src_masked(match, value->be32, mask->be32);
1996 match_set_nw_dst_masked(match, value->be32, mask->be32);
2000 match_set_ipv6_src_masked(match, &value->ipv6, &mask->ipv6);
2004 match_set_ipv6_dst_masked(match, &value->ipv6, &mask->ipv6);
2007 case MFF_IPV6_LABEL:
2008 if ((mask->be32 & htonl(IPV6_LABEL_MASK)) == htonl(IPV6_LABEL_MASK)) {
2009 mf_set_value(mf, value, match);
2011 match_set_ipv6_label_masked(match, value->be32, mask->be32);
2016 match_set_nd_target_masked(match, &value->ipv6, &mask->ipv6);
2020 match_set_nw_frag_masked(match, value->u8, mask->u8);
2024 match_set_nw_src_masked(match, value->be32, mask->be32);
2028 match_set_nw_dst_masked(match, value->be32, mask->be32);
2034 match_set_tp_src_masked(match, value->be16, mask->be16);
2040 match_set_tp_dst_masked(match, value->be16, mask->be16);
2048 return mf->usable_protocols_bitwise;
2051 return ip_is_cidr(mask->be32) ? mf->usable_protocols :
2052 mf->usable_protocols_bitwise;
2056 mf_check__(const struct mf_subfield *sf, const struct flow *flow,
2060 VLOG_WARN_RL(&rl, "unknown %s field", type);
2061 return OFPERR_OFPBAC_BAD_SET_TYPE;
2062 } else if (!sf->n_bits) {
2063 VLOG_WARN_RL(&rl, "zero bit %s field %s", type, sf->field->name);
2064 return OFPERR_OFPBAC_BAD_SET_LEN;
2065 } else if (sf->ofs >= sf->field->n_bits) {
2066 VLOG_WARN_RL(&rl, "bit offset %d exceeds %d-bit width of %s field %s",
2067 sf->ofs, sf->field->n_bits, type, sf->field->name);
2068 return OFPERR_OFPBAC_BAD_SET_LEN;
2069 } else if (sf->ofs + sf->n_bits > sf->field->n_bits) {
2070 VLOG_WARN_RL(&rl, "bit offset %d and width %d exceeds %d-bit width "
2071 "of %s field %s", sf->ofs, sf->n_bits,
2072 sf->field->n_bits, type, sf->field->name);
2073 return OFPERR_OFPBAC_BAD_SET_LEN;
2074 } else if (flow && !mf_are_prereqs_ok(sf->field, flow)) {
2075 VLOG_WARN_RL(&rl, "%s field %s lacks correct prerequisites",
2076 type, sf->field->name);
2077 return OFPERR_OFPBAC_MATCH_INCONSISTENT;
2083 /* Checks whether 'sf' is valid for reading a subfield out of 'flow'. Returns
2084 * 0 if so, otherwise an OpenFlow error code (e.g. as returned by
2087 mf_check_src(const struct mf_subfield *sf, const struct flow *flow)
2089 return mf_check__(sf, flow, "source");
2092 /* Checks whether 'sf' is valid for writing a subfield into 'flow'. Returns 0
2093 * if so, otherwise an OpenFlow error code (e.g. as returned by
2096 mf_check_dst(const struct mf_subfield *sf, const struct flow *flow)
2098 int error = mf_check__(sf, flow, "destination");
2099 if (!error && !sf->field->writable) {
2100 VLOG_WARN_RL(&rl, "destination field %s is not writable",
2102 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
2107 /* Copies the value and wildcard bit pattern for 'mf' from 'match' into the
2108 * 'value' and 'mask', respectively. */
2110 mf_get(const struct mf_field *mf, const struct match *match,
2111 union mf_value *value, union mf_value *mask)
2113 mf_get_value(mf, &match->flow, value);
2114 mf_get_mask(mf, &match->wc, mask);
2117 /* Assigns a random value for field 'mf' to 'value'. */
2119 mf_random_value(const struct mf_field *mf, union mf_value *value)
2121 random_bytes(value, mf->n_bytes);
2133 case MFF_SKB_PRIORITY:
2155 case MFF_ICMPV4_TYPE:
2156 case MFF_ICMPV4_CODE:
2157 case MFF_ICMPV6_TYPE:
2158 case MFF_ICMPV6_CODE:
2164 case MFF_IN_PORT_OXM:
2165 value->be32 = ofputil_port_to_ofp11(u16_to_ofp(ntohs(value->be16)));
2168 case MFF_IPV6_LABEL:
2169 value->be32 &= ~htonl(IPV6_LABEL_MASK);
2173 value->u8 &= IP_DSCP_MASK;
2176 case MFF_IP_DSCP_SHIFTED:
2177 value->u8 &= IP_DSCP_MASK >> 2;
2181 value->u8 &= IP_ECN_MASK;
2185 value->u8 &= FLOW_NW_FRAG_MASK;
2189 value->be16 &= htons(0xff);
2193 value->be16 &= htons(VLAN_VID_MASK);
2196 value->be16 &= htons(VLAN_VID_MASK | VLAN_CFI);
2199 case MFF_DL_VLAN_PCP:
2204 case MFF_MPLS_LABEL:
2205 value->be32 &= htonl(MPLS_LABEL_MASK >> MPLS_LABEL_SHIFT);
2209 value->u8 &= MPLS_TC_MASK >> MPLS_TC_SHIFT;
2213 value->u8 &= MPLS_BOS_MASK >> MPLS_BOS_SHIFT;
2223 mf_from_integer_string(const struct mf_field *mf, const char *s,
2224 uint8_t *valuep, uint8_t *maskp)
2226 unsigned long long int integer, mask;
2231 integer = strtoull(s, &tail, 0);
2232 if (errno || (*tail != '\0' && *tail != '/')) {
2237 mask = strtoull(tail + 1, &tail, 0);
2238 if (errno || *tail != '\0') {
2245 for (i = mf->n_bytes - 1; i >= 0; i--) {
2246 valuep[i] = integer;
2252 return xasprintf("%s: value too large for %u-byte field %s",
2253 s, mf->n_bytes, mf->name);
2258 return xasprintf("%s: bad syntax for %s", s, mf->name);
2262 mf_from_ethernet_string(const struct mf_field *mf, const char *s,
2263 uint8_t mac[ETH_ADDR_LEN],
2264 uint8_t mask[ETH_ADDR_LEN])
2268 ovs_assert(mf->n_bytes == ETH_ADDR_LEN);
2271 if (sscanf(s, ETH_ADDR_SCAN_FMT"%n", ETH_ADDR_SCAN_ARGS(mac), &n) > 0
2272 && n == strlen(s)) {
2273 memset(mask, 0xff, ETH_ADDR_LEN);
2278 if (sscanf(s, ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT"%n",
2279 ETH_ADDR_SCAN_ARGS(mac), ETH_ADDR_SCAN_ARGS(mask), &n) > 0
2280 && n == strlen(s)) {
2284 return xasprintf("%s: invalid Ethernet address", s);
2288 mf_from_ipv4_string(const struct mf_field *mf, const char *s,
2289 ovs_be32 *ip, ovs_be32 *mask)
2293 ovs_assert(mf->n_bytes == sizeof *ip);
2295 if (sscanf(s, IP_SCAN_FMT"/"IP_SCAN_FMT,
2296 IP_SCAN_ARGS(ip), IP_SCAN_ARGS(mask)) == IP_SCAN_COUNT * 2) {
2298 } else if (sscanf(s, IP_SCAN_FMT"/%d",
2299 IP_SCAN_ARGS(ip), &prefix) == IP_SCAN_COUNT + 1) {
2300 if (prefix <= 0 || prefix > 32) {
2301 return xasprintf("%s: network prefix bits not between 1 and "
2303 } else if (prefix == 32) {
2304 *mask = htonl(UINT32_MAX);
2306 *mask = htonl(((1u << prefix) - 1) << (32 - prefix));
2308 } else if (sscanf(s, IP_SCAN_FMT, IP_SCAN_ARGS(ip)) == IP_SCAN_COUNT) {
2309 *mask = htonl(UINT32_MAX);
2311 return xasprintf("%s: invalid IP address", s);
2317 mf_from_ipv6_string(const struct mf_field *mf, const char *s,
2318 struct in6_addr *value, struct in6_addr *mask)
2320 char *str = xstrdup(s);
2321 char *save_ptr = NULL;
2322 const char *name, *netmask;
2325 ovs_assert(mf->n_bytes == sizeof *value);
2327 name = strtok_r(str, "/", &save_ptr);
2328 retval = name ? lookup_ipv6(name, value) : EINVAL;
2332 err = xasprintf("%s: could not convert to IPv6 address", str);
2338 netmask = strtok_r(NULL, "/", &save_ptr);
2340 if (inet_pton(AF_INET6, netmask, mask) != 1) {
2341 int prefix = atoi(netmask);
2342 if (prefix <= 0 || prefix > 128) {
2344 return xasprintf("%s: prefix bits not between 1 and 128", s);
2346 *mask = ipv6_create_mask(prefix);
2350 *mask = in6addr_exact;
2358 mf_from_ofp_port_string(const struct mf_field *mf, const char *s,
2359 ovs_be16 *valuep, ovs_be16 *maskp)
2363 ovs_assert(mf->n_bytes == sizeof(ovs_be16));
2365 if (ofputil_port_from_string(s, &port)) {
2366 *valuep = htons(ofp_to_u16(port));
2367 *maskp = htons(UINT16_MAX);
2370 return xasprintf("%s: port value out of range for %s", s, mf->name);
2374 mf_from_ofp_port_string32(const struct mf_field *mf, const char *s,
2375 ovs_be32 *valuep, ovs_be32 *maskp)
2379 ovs_assert(mf->n_bytes == sizeof(ovs_be32));
2380 if (ofputil_port_from_string(s, &port)) {
2381 *valuep = ofputil_port_to_ofp11(port);
2382 *maskp = htonl(UINT32_MAX);
2385 return xasprintf("%s: port value out of range for %s", s, mf->name);
2388 struct frag_handling {
2394 static const struct frag_handling all_frags[] = {
2395 #define A FLOW_NW_FRAG_ANY
2396 #define L FLOW_NW_FRAG_LATER
2397 /* name mask value */
2400 { "first", A|L, A },
2401 { "later", A|L, A|L },
2406 { "not_later", L, 0 },
2413 mf_from_frag_string(const char *s, uint8_t *valuep, uint8_t *maskp)
2415 const struct frag_handling *h;
2417 for (h = all_frags; h < &all_frags[ARRAY_SIZE(all_frags)]; h++) {
2418 if (!strcasecmp(s, h->name)) {
2419 /* We force the upper bits of the mask on to make mf_parse_value()
2420 * happy (otherwise it will never think it's an exact match.) */
2421 *maskp = h->mask | ~FLOW_NW_FRAG_MASK;
2427 return xasprintf("%s: unknown fragment type (valid types are \"no\", "
2428 "\"yes\", \"first\", \"later\", \"not_first\"", s);
2432 parse_flow_tun_flags(const char *s_, const char *(*bit_to_string)(uint32_t),
2435 uint32_t result = 0;
2436 char *save_ptr = NULL;
2439 char *s = xstrdup(s_);
2441 for (name = strtok_r((char *)s, " |", &save_ptr); name;
2442 name = strtok_r(NULL, " |", &save_ptr)) {
2444 unsigned long long int flags;
2448 if (sscanf(name, "%lli%n", &flags, &n0) > 0 && n0 > 0) {
2452 name_len = strlen(name);
2453 for (bit = 1; bit; bit <<= 1) {
2454 const char *fname = bit_to_string(bit);
2461 len = strlen(fname);
2462 if (len != name_len) {
2465 if (!strncmp(name, fname, len)) {
2477 *res = htons(result);
2484 mf_from_tun_flags_string(const char *s, ovs_be16 *valuep, ovs_be16 *maskp)
2486 if (!parse_flow_tun_flags(s, flow_tun_flag_to_string, valuep)) {
2487 *maskp = htons(UINT16_MAX);
2491 return xasprintf("%s: unknown tunnel flags (valid flags are \"df\", "
2492 "\"csum\", \"key\"", s);
2495 /* Parses 's', a string value for field 'mf', into 'value' and 'mask'. Returns
2496 * NULL if successful, otherwise a malloc()'d string describing the error. */
2498 mf_parse(const struct mf_field *mf, const char *s,
2499 union mf_value *value, union mf_value *mask)
2501 if (!strcmp(s, "*")) {
2502 memset(value, 0, mf->n_bytes);
2503 memset(mask, 0, mf->n_bytes);
2507 switch (mf->string) {
2509 case MFS_HEXADECIMAL:
2510 return mf_from_integer_string(mf, s,
2511 (uint8_t *) value, (uint8_t *) mask);
2514 return mf_from_ethernet_string(mf, s, value->mac, mask->mac);
2517 return mf_from_ipv4_string(mf, s, &value->be32, &mask->be32);
2520 return mf_from_ipv6_string(mf, s, &value->ipv6, &mask->ipv6);
2523 return mf_from_ofp_port_string(mf, s, &value->be16, &mask->be16);
2525 case MFS_OFP_PORT_OXM:
2526 return mf_from_ofp_port_string32(mf, s, &value->be32, &mask->be32);
2529 return mf_from_frag_string(s, &value->u8, &mask->u8);
2532 ovs_assert(mf->n_bytes == sizeof(ovs_be16));
2533 return mf_from_tun_flags_string(s, &value->be16, &mask->be16);
2538 /* Parses 's', a string value for field 'mf', into 'value'. Returns NULL if
2539 * successful, otherwise a malloc()'d string describing the error. */
2541 mf_parse_value(const struct mf_field *mf, const char *s, union mf_value *value)
2543 union mf_value mask;
2546 error = mf_parse(mf, s, value, &mask);
2551 if (!is_all_ones((const uint8_t *) &mask, mf->n_bytes)) {
2552 return xasprintf("%s: wildcards not allowed here", s);
2558 mf_format_integer_string(const struct mf_field *mf, const uint8_t *valuep,
2559 const uint8_t *maskp, struct ds *s)
2561 unsigned long long int integer;
2564 ovs_assert(mf->n_bytes <= 8);
2567 for (i = 0; i < mf->n_bytes; i++) {
2568 integer = (integer << 8) | valuep[i];
2570 if (mf->string == MFS_HEXADECIMAL) {
2571 ds_put_format(s, "%#llx", integer);
2573 ds_put_format(s, "%lld", integer);
2577 unsigned long long int mask;
2580 for (i = 0; i < mf->n_bytes; i++) {
2581 mask = (mask << 8) | maskp[i];
2584 /* I guess we could write the mask in decimal for MFS_DECIMAL but I'm
2585 * not sure that that a bit-mask written in decimal is ever easier to
2586 * understand than the same bit-mask written in hexadecimal. */
2587 ds_put_format(s, "/%#llx", mask);
2592 mf_format_frag_string(uint8_t value, uint8_t mask, struct ds *s)
2594 const struct frag_handling *h;
2596 mask &= FLOW_NW_FRAG_MASK;
2599 for (h = all_frags; h < &all_frags[ARRAY_SIZE(all_frags)]; h++) {
2600 if (value == h->value && mask == h->mask) {
2601 ds_put_cstr(s, h->name);
2605 ds_put_cstr(s, "<error>");
2609 mf_format_tnl_flags_string(const ovs_be16 *valuep, struct ds *s)
2611 format_flags(s, flow_tun_flag_to_string, ntohs(*valuep), '|');
2614 /* Appends to 's' a string representation of field 'mf' whose value is in
2615 * 'value' and 'mask'. 'mask' may be NULL to indicate an exact match. */
2617 mf_format(const struct mf_field *mf,
2618 const union mf_value *value, const union mf_value *mask,
2622 if (is_all_zeros((const uint8_t *) mask, mf->n_bytes)) {
2623 ds_put_cstr(s, "ANY");
2625 } else if (is_all_ones((const uint8_t *) mask, mf->n_bytes)) {
2630 switch (mf->string) {
2631 case MFS_OFP_PORT_OXM:
2634 ofputil_port_from_ofp11(value->be32, &port);
2635 ofputil_format_port(port, s);
2641 ofputil_format_port(u16_to_ofp(ntohs(value->be16)), s);
2646 case MFS_HEXADECIMAL:
2647 mf_format_integer_string(mf, (uint8_t *) value, (uint8_t *) mask, s);
2651 eth_format_masked(value->mac, mask->mac, s);
2655 ip_format_masked(value->be32, mask ? mask->be32 : htonl(UINT32_MAX),
2660 print_ipv6_masked(s, &value->ipv6, mask ? &mask->ipv6 : NULL);
2664 mf_format_frag_string(value->u8, mask ? mask->u8 : UINT8_MAX, s);
2668 mf_format_tnl_flags_string(&value->be16, s);
2676 /* Makes subfield 'sf' within 'flow' exactly match the 'sf->n_bits'
2677 * least-significant bits in 'x'.
2680 mf_write_subfield_flow(const struct mf_subfield *sf,
2681 const union mf_subvalue *x, struct flow *flow)
2683 const struct mf_field *field = sf->field;
2684 union mf_value value;
2686 mf_get_value(field, flow, &value);
2687 bitwise_copy(x, sizeof *x, 0, &value, field->n_bytes,
2688 sf->ofs, sf->n_bits);
2689 mf_set_flow_value(field, &value, flow);
2692 /* Makes subfield 'sf' within 'match' exactly match the 'sf->n_bits'
2693 * least-significant bits in 'x'.
2696 mf_write_subfield(const struct mf_subfield *sf, const union mf_subvalue *x,
2697 struct match *match)
2699 const struct mf_field *field = sf->field;
2700 union mf_value value, mask;
2702 mf_get(field, match, &value, &mask);
2703 bitwise_copy(x, sizeof *x, 0, &value, field->n_bytes, sf->ofs, sf->n_bits);
2704 bitwise_one ( &mask, field->n_bytes, sf->ofs, sf->n_bits);
2705 mf_set(field, &value, &mask, match);
2708 /* Initializes 'x' to the value of 'sf' within 'flow'. 'sf' must be valid for
2709 * reading 'flow', e.g. as checked by mf_check_src(). */
2711 mf_read_subfield(const struct mf_subfield *sf, const struct flow *flow,
2712 union mf_subvalue *x)
2714 union mf_value value;
2716 mf_get_value(sf->field, flow, &value);
2718 memset(x, 0, sizeof *x);
2719 bitwise_copy(&value, sf->field->n_bytes, sf->ofs,
2724 /* Returns the value of 'sf' within 'flow'. 'sf' must be valid for reading
2725 * 'flow', e.g. as checked by mf_check_src() and sf->n_bits must be 64 or
2728 mf_get_subfield(const struct mf_subfield *sf, const struct flow *flow)
2730 union mf_value value;
2732 mf_get_value(sf->field, flow, &value);
2733 return bitwise_get(&value, sf->field->n_bytes, sf->ofs, sf->n_bits);
2736 /* Formats 'sf' into 's' in a format normally acceptable to
2737 * mf_parse_subfield(). (It won't be acceptable if sf->field is NULL or if
2738 * sf->field has no NXM name.) */
2740 mf_format_subfield(const struct mf_subfield *sf, struct ds *s)
2743 ds_put_cstr(s, "<unknown>");
2744 } else if (sf->field->nxm_name) {
2745 ds_put_cstr(s, sf->field->nxm_name);
2746 } else if (sf->field->nxm_header) {
2747 uint32_t header = sf->field->nxm_header;
2748 ds_put_format(s, "%d:%d", NXM_VENDOR(header), NXM_FIELD(header));
2750 ds_put_cstr(s, sf->field->name);
2753 if (sf->field && sf->ofs == 0 && sf->n_bits == sf->field->n_bits) {
2754 ds_put_cstr(s, "[]");
2755 } else if (sf->n_bits == 1) {
2756 ds_put_format(s, "[%d]", sf->ofs);
2758 ds_put_format(s, "[%d..%d]", sf->ofs, sf->ofs + sf->n_bits - 1);
2762 static const struct mf_field *
2763 mf_parse_subfield_name(const char *name, int name_len, bool *wild)
2767 *wild = name_len > 2 && !memcmp(&name[name_len - 2], "_W", 2);
2772 for (i = 0; i < MFF_N_IDS; i++) {
2773 const struct mf_field *mf = mf_from_id(i);
2776 && !strncmp(mf->nxm_name, name, name_len)
2777 && mf->nxm_name[name_len] == '\0') {
2781 && !strncmp(mf->oxm_name, name, name_len)
2782 && mf->oxm_name[name_len] == '\0') {
2790 /* Parses a subfield from the beginning of '*sp' into 'sf'. If successful,
2791 * returns NULL and advances '*sp' to the first byte following the parsed
2792 * string. On failure, returns a malloc()'d error message, does not modify
2793 * '*sp', and does not properly initialize 'sf'.
2795 * The syntax parsed from '*sp' takes the form "header[start..end]" where
2796 * 'header' is the name of an NXM field and 'start' and 'end' are (inclusive)
2797 * bit indexes. "..end" may be omitted to indicate a single bit. "start..end"
2798 * may both be omitted (the [] are still required) to indicate an entire
2800 char * WARN_UNUSED_RESULT
2801 mf_parse_subfield__(struct mf_subfield *sf, const char **sp)
2803 const struct mf_field *field;
2812 name_len = strcspn(s, "[");
2813 if (s[name_len] != '[') {
2814 return xasprintf("%s: missing [ looking for field name", *sp);
2817 field = mf_parse_subfield_name(name, name_len, &wild);
2819 return xasprintf("%s: unknown field `%.*s'", *sp, name_len, s);
2823 if (sscanf(s, "[%d..%d]", &start, &end) == 2) {
2824 /* Nothing to do. */
2825 } else if (sscanf(s, "[%d]", &start) == 1) {
2827 } else if (!strncmp(s, "[]", 2)) {
2829 end = field->n_bits - 1;
2831 return xasprintf("%s: syntax error expecting [] or [<bit>] or "
2832 "[<start>..<end>]", *sp);
2834 s = strchr(s, ']') + 1;
2837 return xasprintf("%s: starting bit %d is after ending bit %d",
2839 } else if (start >= field->n_bits) {
2840 return xasprintf("%s: starting bit %d is not valid because field is "
2841 "only %d bits wide", *sp, start, field->n_bits);
2842 } else if (end >= field->n_bits){
2843 return xasprintf("%s: ending bit %d is not valid because field is "
2844 "only %d bits wide", *sp, end, field->n_bits);
2849 sf->n_bits = end - start + 1;
2855 /* Parses a subfield from the entirety of 's' into 'sf'. Returns NULL if
2856 * successful, otherwise a malloc()'d string describing the error. The caller
2857 * is responsible for freeing the returned string.
2859 * The syntax parsed from 's' takes the form "header[start..end]" where
2860 * 'header' is the name of an NXM field and 'start' and 'end' are (inclusive)
2861 * bit indexes. "..end" may be omitted to indicate a single bit. "start..end"
2862 * may both be omitted (the [] are still required) to indicate an entire
2864 char * WARN_UNUSED_RESULT
2865 mf_parse_subfield(struct mf_subfield *sf, const char *s)
2867 char *error = mf_parse_subfield__(sf, &s);
2868 if (!error && s[0]) {
2869 error = xstrdup("unexpected input following field syntax");
2875 mf_format_subvalue(const union mf_subvalue *subvalue, struct ds *s)
2879 for (i = 0; i < ARRAY_SIZE(subvalue->u8); i++) {
2880 if (subvalue->u8[i]) {
2881 ds_put_format(s, "0x%"PRIx8, subvalue->u8[i]);
2882 for (i++; i < ARRAY_SIZE(subvalue->u8); i++) {
2883 ds_put_format(s, "%02"PRIx8, subvalue->u8[i]);
2888 ds_put_char(s, '0');