2 * Copyright (c) 2011, 2012, 2013 Nicira, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include "meta-flow.h"
23 #include <netinet/icmp6.h>
24 #include <netinet/ip6.h>
26 #include "classifier.h"
27 #include "dynamic-string.h"
28 #include "ofp-errors.h"
30 #include "ovs-thread.h"
34 #include "socket-util.h"
35 #include "unaligned.h"
38 VLOG_DEFINE_THIS_MODULE(meta_flow);
40 #define MF_FIELD_SIZES(MEMBER) \
41 sizeof ((union mf_value *)0)->MEMBER, \
42 8 * sizeof ((union mf_value *)0)->MEMBER
44 extern const struct mf_field mf_fields[MFF_N_IDS]; /* Silence a warning. */
46 const struct mf_field mf_fields[MFF_N_IDS] = {
52 MFF_TUN_ID, "tun_id", "tunnel_id",
58 NXM_NX_TUN_ID, "NXM_NX_TUN_ID",
59 OXM_OF_TUNNEL_ID, "OXM_OF_TUNNEL_ID",
60 OFPUTIL_P_NXM_OXM_ANY,
61 OFPUTIL_P_NXM_OXM_ANY,
63 MFF_TUN_SRC, "tun_src", NULL,
69 NXM_NX_TUN_IPV4_SRC, "NXM_NX_TUN_IPV4_SRC",
70 NXM_NX_TUN_IPV4_SRC, "NXM_NX_TUN_IPV4_SRC",
71 OFPUTIL_P_NXM_OXM_ANY,
72 OFPUTIL_P_NXM_OXM_ANY,
74 MFF_TUN_DST, "tun_dst", NULL,
80 NXM_NX_TUN_IPV4_DST, "NXM_NX_TUN_IPV4_DST",
81 NXM_NX_TUN_IPV4_DST, "NXM_NX_TUN_IPV4_DST",
82 OFPUTIL_P_NXM_OXM_ANY,
83 OFPUTIL_P_NXM_OXM_ANY,
85 MFF_TUN_FLAGS, "tun_flags", NULL,
96 MFF_TUN_TTL, "tun_ttl", NULL,
107 MFF_TUN_TOS, "tun_tos", NULL,
118 MFF_METADATA, "metadata", NULL,
119 MF_FIELD_SIZES(be64),
124 OXM_OF_METADATA, "OXM_OF_METADATA",
125 OXM_OF_METADATA, "OXM_OF_METADATA",
126 OFPUTIL_P_NXM_OF11_UP,
127 OFPUTIL_P_NXM_OF11_UP,
129 MFF_IN_PORT, "in_port", NULL,
130 MF_FIELD_SIZES(be16),
135 NXM_OF_IN_PORT, "NXM_OF_IN_PORT",
136 NXM_OF_IN_PORT, "NXM_OF_IN_PORT",
137 OFPUTIL_P_ANY, /* OF11+ via mapping to 32 bits. */
140 MFF_IN_PORT_OXM, "in_port_oxm", NULL,
141 MF_FIELD_SIZES(be32),
146 OXM_OF_IN_PORT, "OXM_OF_IN_PORT",
147 OXM_OF_IN_PORT, "OXM_OF_IN_PORT",
151 MFF_SKB_PRIORITY, "skb_priority", NULL,
152 MF_FIELD_SIZES(be32),
162 MFF_PKT_MARK, "pkt_mark", NULL,
163 MF_FIELD_SIZES(be32),
168 NXM_NX_PKT_MARK, "NXM_NX_PKT_MARK",
169 NXM_NX_PKT_MARK, "NXM_NX_PKT_MARK",
170 OFPUTIL_P_NXM_OXM_ANY,
171 OFPUTIL_P_NXM_OXM_ANY,
174 #define REGISTER(IDX) \
176 MFF_REG##IDX, "reg" #IDX, NULL, \
177 MF_FIELD_SIZES(be32), \
182 NXM_NX_REG(IDX), "NXM_NX_REG" #IDX, \
183 NXM_NX_REG(IDX), "NXM_NX_REG" #IDX, \
184 OFPUTIL_P_NXM_OXM_ANY, \
185 OFPUTIL_P_NXM_OXM_ANY, \
220 MFF_ETH_SRC, "eth_src", "dl_src",
226 NXM_OF_ETH_SRC, "NXM_OF_ETH_SRC",
227 OXM_OF_ETH_SRC, "OXM_OF_ETH_SRC",
229 OFPUTIL_P_NXM_OF11_UP, /* Bitwise masking only with NXM and OF11+! */
231 MFF_ETH_DST, "eth_dst", "dl_dst",
237 NXM_OF_ETH_DST, "NXM_OF_ETH_DST",
238 OXM_OF_ETH_DST, "OXM_OF_ETH_DST",
240 OFPUTIL_P_NXM_OF11_UP, /* Bitwise masking only with NXM and OF11+! */
242 MFF_ETH_TYPE, "eth_type", "dl_type",
243 MF_FIELD_SIZES(be16),
248 NXM_OF_ETH_TYPE, "NXM_OF_ETH_TYPE",
249 OXM_OF_ETH_TYPE, "OXM_OF_ETH_TYPE",
255 MFF_VLAN_TCI, "vlan_tci", NULL,
256 MF_FIELD_SIZES(be16),
261 NXM_OF_VLAN_TCI, "NXM_OF_VLAN_TCI",
262 NXM_OF_VLAN_TCI, "NXM_OF_VLAN_TCI",
264 OFPUTIL_P_NXM_OXM_ANY,
266 MFF_DL_VLAN, "dl_vlan", NULL,
267 sizeof(ovs_be16), 12,
275 OFPUTIL_P_NXM_OXM_ANY,
277 MFF_VLAN_VID, "vlan_vid", NULL,
278 sizeof(ovs_be16), 12,
283 OXM_OF_VLAN_VID, "OXM_OF_VLAN_VID",
284 OXM_OF_VLAN_VID, "OXM_OF_VLAN_VID",
286 OFPUTIL_P_NXM_OXM_ANY,
288 MFF_DL_VLAN_PCP, "dl_vlan_pcp", NULL,
296 OFPUTIL_P_ANY, /* Will be mapped to NXM and OXM. */
299 MFF_VLAN_PCP, "vlan_pcp", NULL,
305 OXM_OF_VLAN_PCP, "OXM_OF_VLAN_PCP",
306 OXM_OF_VLAN_PCP, "OXM_OF_VLAN_PCP",
307 OFPUTIL_P_ANY, /* Will be mapped to OF10 and NXM. */
315 MFF_MPLS_LABEL, "mpls_label", NULL,
321 OXM_OF_MPLS_LABEL, "OXM_OF_MPLS_LABEL",
322 OXM_OF_MPLS_LABEL, "OXM_OF_MPLS_LABEL",
323 OFPUTIL_P_NXM_OF11_UP,
326 MFF_MPLS_TC, "mpls_tc", NULL,
332 OXM_OF_MPLS_TC, "OXM_OF_MPLS_TC",
333 OXM_OF_MPLS_TC, "OXM_OF_MPLS_TC",
334 OFPUTIL_P_NXM_OF11_UP,
337 MFF_MPLS_BOS, "mpls_bos", NULL,
343 OXM_OF_MPLS_BOS, "OXM_OF_MPLS_BOS",
344 OXM_OF_MPLS_BOS, "OXM_OF_MPLS_BOS",
345 OFPUTIL_P_NXM_OXM_ANY,
354 MFF_IPV4_SRC, "ip_src", "nw_src",
355 MF_FIELD_SIZES(be32),
360 NXM_OF_IP_SRC, "NXM_OF_IP_SRC",
361 OXM_OF_IPV4_SRC, "OXM_OF_IPV4_SRC",
363 OFPUTIL_P_NXM_OF11_UP,
365 MFF_IPV4_DST, "ip_dst", "nw_dst",
366 MF_FIELD_SIZES(be32),
371 NXM_OF_IP_DST, "NXM_OF_IP_DST",
372 OXM_OF_IPV4_DST, "OXM_OF_IPV4_DST",
374 OFPUTIL_P_NXM_OF11_UP,
378 MFF_IPV6_SRC, "ipv6_src", NULL,
379 MF_FIELD_SIZES(ipv6),
384 NXM_NX_IPV6_SRC, "NXM_NX_IPV6_SRC",
385 OXM_OF_IPV6_SRC, "OXM_OF_IPV6_SRC",
386 OFPUTIL_P_NXM_OXM_ANY,
387 OFPUTIL_P_NXM_OXM_ANY,
389 MFF_IPV6_DST, "ipv6_dst", NULL,
390 MF_FIELD_SIZES(ipv6),
395 NXM_NX_IPV6_DST, "NXM_NX_IPV6_DST",
396 OXM_OF_IPV6_DST, "OXM_OF_IPV6_DST",
397 OFPUTIL_P_NXM_OXM_ANY,
398 OFPUTIL_P_NXM_OXM_ANY,
401 MFF_IPV6_LABEL, "ipv6_label", NULL,
407 NXM_NX_IPV6_LABEL, "NXM_NX_IPV6_LABEL",
408 OXM_OF_IPV6_FLABEL, "OXM_OF_IPV6_FLABEL",
409 OFPUTIL_P_NXM_OXM_ANY,
410 OFPUTIL_P_NXM_OXM_ANY,
414 MFF_IP_PROTO, "nw_proto", "ip_proto",
420 NXM_OF_IP_PROTO, "NXM_OF_IP_PROTO",
421 OXM_OF_IP_PROTO, "OXM_OF_IP_PROTO",
425 MFF_IP_DSCP, "nw_tos", NULL,
431 NXM_OF_IP_TOS, "NXM_OF_IP_TOS",
432 NXM_OF_IP_TOS, "NXM_OF_IP_TOS",
433 OFPUTIL_P_ANY, /* Will be shifted for OXM. */
436 MFF_IP_DSCP_SHIFTED, "ip_dscp", NULL,
442 OXM_OF_IP_DSCP, "OXM_OF_IP_DSCP",
443 OXM_OF_IP_DSCP, "OXM_OF_IP_DSCP",
444 OFPUTIL_P_ANY, /* Will be shifted for non-OXM. */
447 MFF_IP_ECN, "nw_ecn", "ip_ecn",
453 NXM_NX_IP_ECN, "NXM_NX_IP_ECN",
454 OXM_OF_IP_ECN, "OXM_OF_IP_ECN",
455 OFPUTIL_P_NXM_OXM_ANY,
458 MFF_IP_TTL, "nw_ttl", NULL,
464 NXM_NX_IP_TTL, "NXM_NX_IP_TTL",
465 NXM_NX_IP_TTL, "NXM_NX_IP_TTL",
466 OFPUTIL_P_NXM_OXM_ANY,
469 MFF_IP_FRAG, "ip_frag", NULL,
475 NXM_NX_IP_FRAG, "NXM_NX_IP_FRAG",
476 NXM_NX_IP_FRAG, "NXM_NX_IP_FRAG",
477 OFPUTIL_P_NXM_OXM_ANY,
478 OFPUTIL_P_NXM_OXM_ANY,
482 MFF_ARP_OP, "arp_op", NULL,
483 MF_FIELD_SIZES(be16),
488 NXM_OF_ARP_OP, "NXM_OF_ARP_OP",
489 OXM_OF_ARP_OP, "OXM_OF_ARP_OP",
493 MFF_ARP_SPA, "arp_spa", NULL,
494 MF_FIELD_SIZES(be32),
499 NXM_OF_ARP_SPA, "NXM_OF_ARP_SPA",
500 OXM_OF_ARP_SPA, "OXM_OF_ARP_SPA",
502 OFPUTIL_P_NXM_OF11_UP,
504 MFF_ARP_TPA, "arp_tpa", NULL,
505 MF_FIELD_SIZES(be32),
510 NXM_OF_ARP_TPA, "NXM_OF_ARP_TPA",
511 OXM_OF_ARP_TPA, "OXM_OF_ARP_TPA",
513 OFPUTIL_P_NXM_OF11_UP,
515 MFF_ARP_SHA, "arp_sha", NULL,
521 NXM_NX_ARP_SHA, "NXM_NX_ARP_SHA",
522 OXM_OF_ARP_SHA, "OXM_OF_ARP_SHA",
523 OFPUTIL_P_NXM_OXM_ANY,
524 OFPUTIL_P_NXM_OXM_ANY,
526 MFF_ARP_THA, "arp_tha", NULL,
532 NXM_NX_ARP_THA, "NXM_NX_ARP_THA",
533 OXM_OF_ARP_THA, "OXM_OF_ARP_THA",
534 OFPUTIL_P_NXM_OXM_ANY,
535 OFPUTIL_P_NXM_OXM_ANY,
543 MFF_TCP_SRC, "tcp_src", "tp_src",
544 MF_FIELD_SIZES(be16),
549 NXM_OF_TCP_SRC, "NXM_OF_TCP_SRC",
550 OXM_OF_TCP_SRC, "OXM_OF_TCP_SRC",
552 OFPUTIL_P_NXM_OXM_ANY,
554 MFF_TCP_DST, "tcp_dst", "tp_dst",
555 MF_FIELD_SIZES(be16),
560 NXM_OF_TCP_DST, "NXM_OF_TCP_DST",
561 OXM_OF_TCP_DST, "OXM_OF_TCP_DST",
563 OFPUTIL_P_NXM_OXM_ANY,
565 MFF_TCP_FLAGS, "tcp_flags", NULL,
571 NXM_NX_TCP_FLAGS, "NXM_NX_TCP_FLAGS",
572 NXM_NX_TCP_FLAGS, "NXM_NX_TCP_FLAGS",
573 OFPUTIL_P_NXM_OXM_ANY,
574 OFPUTIL_P_NXM_OXM_ANY,
578 MFF_UDP_SRC, "udp_src", NULL,
579 MF_FIELD_SIZES(be16),
584 NXM_OF_UDP_SRC, "NXM_OF_UDP_SRC",
585 OXM_OF_UDP_SRC, "OXM_OF_UDP_SRC",
587 OFPUTIL_P_NXM_OXM_ANY,
589 MFF_UDP_DST, "udp_dst", NULL,
590 MF_FIELD_SIZES(be16),
595 NXM_OF_UDP_DST, "NXM_OF_UDP_DST",
596 OXM_OF_UDP_DST, "OXM_OF_UDP_DST",
598 OFPUTIL_P_NXM_OXM_ANY,
602 MFF_SCTP_SRC, "sctp_src", NULL,
603 MF_FIELD_SIZES(be16),
608 OXM_OF_SCTP_SRC, "OXM_OF_SCTP_SRC",
609 OXM_OF_SCTP_SRC, "OXM_OF_SCTP_SRC",
610 OFPUTIL_P_NXM_OF11_UP,
611 OFPUTIL_P_NXM_OXM_ANY,
613 MFF_SCTP_DST, "sctp_dst", NULL,
614 MF_FIELD_SIZES(be16),
619 OXM_OF_SCTP_DST, "OXM_OF_SCTP_DST",
620 OXM_OF_SCTP_DST, "OXM_OF_SCTP_DST",
621 OFPUTIL_P_NXM_OF11_UP,
622 OFPUTIL_P_NXM_OXM_ANY,
626 MFF_ICMPV4_TYPE, "icmp_type", NULL,
632 NXM_OF_ICMP_TYPE, "NXM_OF_ICMP_TYPE",
633 OXM_OF_ICMPV4_TYPE, "OXM_OF_ICMPV4_TYPE",
637 MFF_ICMPV4_CODE, "icmp_code", NULL,
643 NXM_OF_ICMP_CODE, "NXM_OF_ICMP_CODE",
644 OXM_OF_ICMPV4_CODE, "OXM_OF_ICMPV4_CODE",
650 MFF_ICMPV6_TYPE, "icmpv6_type", NULL,
656 NXM_NX_ICMPV6_TYPE, "NXM_NX_ICMPV6_TYPE",
657 OXM_OF_ICMPV6_TYPE, "OXM_OF_ICMPV6_TYPE",
658 OFPUTIL_P_NXM_OXM_ANY,
661 MFF_ICMPV6_CODE, "icmpv6_code", NULL,
667 NXM_NX_ICMPV6_CODE, "NXM_NX_ICMPV6_CODE",
668 OXM_OF_ICMPV6_CODE, "OXM_OF_ICMPV6_CODE",
669 OFPUTIL_P_NXM_OXM_ANY,
678 MFF_ND_TARGET, "nd_target", NULL,
679 MF_FIELD_SIZES(ipv6),
684 NXM_NX_ND_TARGET, "NXM_NX_ND_TARGET",
685 OXM_OF_IPV6_ND_TARGET, "OXM_OF_IPV6_ND_TARGET",
686 OFPUTIL_P_NXM_OXM_ANY,
687 OFPUTIL_P_NXM_OXM_ANY,
689 MFF_ND_SLL, "nd_sll", NULL,
695 NXM_NX_ND_SLL, "NXM_NX_ND_SLL",
696 OXM_OF_IPV6_ND_SLL, "OXM_OF_IPV6_ND_SLL",
697 OFPUTIL_P_NXM_OXM_ANY,
698 OFPUTIL_P_NXM_OXM_ANY,
700 MFF_ND_TLL, "nd_tll", NULL,
706 NXM_NX_ND_TLL, "NXM_NX_ND_TLL",
707 OXM_OF_IPV6_ND_TLL, "OXM_OF_IPV6_ND_TLL",
708 OFPUTIL_P_NXM_OXM_ANY,
709 OFPUTIL_P_NXM_OXM_ANY,
713 /* Maps an NXM or OXM header value to an mf_field. */
715 struct hmap_node hmap_node; /* In 'all_fields' hmap. */
716 uint32_t header; /* NXM or OXM header value. */
717 const struct mf_field *mf;
720 /* Contains 'struct nxm_field's. */
721 static struct hmap all_fields;
723 /* Maps from an mf_field's 'name' or 'extra_name' to the mf_field. */
724 static struct shash mf_by_name;
726 /* Rate limit for parse errors. These always indicate a bug in an OpenFlow
727 * controller and so there's not much point in showing a lot of them. */
728 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
730 const struct mf_field *mf_from_nxm_header__(uint32_t header);
731 static void nxm_init(void);
733 /* Returns the field with the given 'name', or a null pointer if no field has
735 const struct mf_field *
736 mf_from_name(const char *name)
739 return shash_find_data(&mf_by_name, name);
743 add_nxm_field(uint32_t header, const struct mf_field *mf)
747 f = xmalloc(sizeof *f);
748 hmap_insert(&all_fields, &f->hmap_node, hash_int(header, 0));
754 nxm_init_add_field(const struct mf_field *mf, uint32_t header)
757 ovs_assert(!mf_from_nxm_header__(header));
758 add_nxm_field(header, mf);
759 if (mf->maskable != MFM_NONE) {
760 add_nxm_field(NXM_MAKE_WILD_HEADER(header), mf);
770 hmap_init(&all_fields);
771 shash_init(&mf_by_name);
772 for (i = 0; i < MFF_N_IDS; i++) {
773 const struct mf_field *mf = &mf_fields[i];
775 ovs_assert(mf->id == i); /* Fields must be in the enum order. */
777 nxm_init_add_field(mf, mf->nxm_header);
778 if (mf->oxm_header != mf->nxm_header) {
779 nxm_init_add_field(mf, mf->oxm_header);
782 shash_add_once(&mf_by_name, mf->name, mf);
783 if (mf->extra_name) {
784 shash_add_once(&mf_by_name, mf->extra_name, mf);
792 static pthread_once_t once = PTHREAD_ONCE_INIT;
793 pthread_once(&once, nxm_do_init);
796 const struct mf_field *
797 mf_from_nxm_header(uint32_t header)
800 return mf_from_nxm_header__(header);
803 const struct mf_field *
804 mf_from_nxm_header__(uint32_t header)
806 const struct nxm_field *f;
808 HMAP_FOR_EACH_IN_BUCKET (f, hmap_node, hash_int(header, 0), &all_fields) {
809 if (f->header == header) {
817 /* Returns true if 'wc' wildcards all the bits in field 'mf', false if 'wc'
818 * specifies at least one bit in the field.
820 * The caller is responsible for ensuring that 'wc' corresponds to a flow that
821 * meets 'mf''s prerequisites. */
823 mf_is_all_wild(const struct mf_field *mf, const struct flow_wildcards *wc)
827 return !wc->masks.tunnel.ip_src;
829 return !wc->masks.tunnel.ip_dst;
834 return !wc->masks.tunnel.tun_id;
836 return !wc->masks.metadata;
838 case MFF_IN_PORT_OXM:
839 return !wc->masks.in_port.ofp_port;
840 case MFF_SKB_PRIORITY:
841 return !wc->masks.skb_priority;
843 return !wc->masks.pkt_mark;
845 return !wc->masks.regs[mf->id - MFF_REG0];
848 return eth_addr_is_zero(wc->masks.dl_src);
850 return eth_addr_is_zero(wc->masks.dl_dst);
852 return !wc->masks.dl_type;
856 return eth_addr_is_zero(wc->masks.arp_sha);
860 return eth_addr_is_zero(wc->masks.arp_tha);
863 return !wc->masks.vlan_tci;
865 return !(wc->masks.vlan_tci & htons(VLAN_VID_MASK));
867 return !(wc->masks.vlan_tci & htons(VLAN_VID_MASK | VLAN_CFI));
868 case MFF_DL_VLAN_PCP:
870 return !(wc->masks.vlan_tci & htons(VLAN_PCP_MASK));
873 return !(wc->masks.mpls_lse & htonl(MPLS_LABEL_MASK));
875 return !(wc->masks.mpls_lse & htonl(MPLS_TC_MASK));
877 return !(wc->masks.mpls_lse & htonl(MPLS_BOS_MASK));
880 return !wc->masks.nw_src;
882 return !wc->masks.nw_dst;
885 return ipv6_mask_is_any(&wc->masks.ipv6_src);
887 return ipv6_mask_is_any(&wc->masks.ipv6_dst);
890 return !wc->masks.ipv6_label;
893 return !wc->masks.nw_proto;
895 case MFF_IP_DSCP_SHIFTED:
896 return !(wc->masks.nw_tos & IP_DSCP_MASK);
898 return !(wc->masks.nw_tos & IP_ECN_MASK);
900 return !wc->masks.nw_ttl;
903 return ipv6_mask_is_any(&wc->masks.nd_target);
906 return !(wc->masks.nw_frag & FLOW_NW_FRAG_MASK);
909 return !wc->masks.nw_proto;
911 return !wc->masks.nw_src;
913 return !wc->masks.nw_dst;
918 case MFF_ICMPV4_TYPE:
919 case MFF_ICMPV6_TYPE:
920 return !wc->masks.tp_src;
924 case MFF_ICMPV4_CODE:
925 case MFF_ICMPV6_CODE:
926 return !wc->masks.tp_dst;
928 return !wc->masks.tcp_flags;
936 /* Initializes 'mask' with the wildcard bit pattern for field 'mf' within 'wc'.
937 * Each bit in 'mask' will be set to 1 if the bit is significant for matching
938 * purposes, or to 0 if it is wildcarded.
940 * The caller is responsible for ensuring that 'wc' corresponds to a flow that
941 * meets 'mf''s prerequisites. */
943 mf_get_mask(const struct mf_field *mf, const struct flow_wildcards *wc,
944 union mf_value *mask)
946 mf_get_value(mf, &wc->masks, mask);
949 /* Tests whether 'mask' is a valid wildcard bit pattern for 'mf'. Returns true
950 * if the mask is valid, false otherwise. */
952 mf_is_mask_valid(const struct mf_field *mf, const union mf_value *mask)
954 switch (mf->maskable) {
956 return (is_all_zeros((const uint8_t *) mask, mf->n_bytes) ||
957 is_all_ones((const uint8_t *) mask, mf->n_bytes));
967 is_icmpv4(const struct flow *flow)
969 return (flow->dl_type == htons(ETH_TYPE_IP)
970 && flow->nw_proto == IPPROTO_ICMP);
974 is_icmpv6(const struct flow *flow)
976 return (flow->dl_type == htons(ETH_TYPE_IPV6)
977 && flow->nw_proto == IPPROTO_ICMPV6);
980 /* Returns true if 'flow' meets the prerequisites for 'mf', false otherwise. */
982 mf_are_prereqs_ok(const struct mf_field *mf, const struct flow *flow)
984 switch (mf->prereqs) {
989 return (flow->dl_type == htons(ETH_TYPE_ARP) ||
990 flow->dl_type == htons(ETH_TYPE_RARP));
992 return flow->dl_type == htons(ETH_TYPE_IP);
994 return flow->dl_type == htons(ETH_TYPE_IPV6);
996 return (flow->vlan_tci & htons(VLAN_CFI)) != 0;
998 return eth_type_mpls(flow->dl_type);
1000 return is_ip_any(flow);
1003 return is_ip_any(flow) && flow->nw_proto == IPPROTO_TCP;
1005 return is_ip_any(flow) && flow->nw_proto == IPPROTO_UDP;
1007 return is_ip_any(flow) && flow->nw_proto == IPPROTO_SCTP;
1009 return is_icmpv4(flow);
1011 return is_icmpv6(flow);
1014 return (is_icmpv6(flow)
1015 && flow->tp_dst == htons(0)
1016 && (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT) ||
1017 flow->tp_src == htons(ND_NEIGHBOR_ADVERT)));
1018 case MFP_ND_SOLICIT:
1019 return (is_icmpv6(flow)
1020 && flow->tp_dst == htons(0)
1021 && (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT)));
1023 return (is_icmpv6(flow)
1024 && flow->tp_dst == htons(0)
1025 && (flow->tp_src == htons(ND_NEIGHBOR_ADVERT)));
1031 /* Set field and it's prerequisities in the mask.
1032 * This is only ever called for writeable 'mf's, but we do not make the
1033 * distinction here. */
1035 mf_mask_field_and_prereqs(const struct mf_field *mf, struct flow *mask)
1037 static const union mf_value exact_match_mask = MF_EXACT_MASK_INITIALIZER;
1039 mf_set_flow_value(mf, &exact_match_mask, mask);
1041 switch (mf->prereqs) {
1043 case MFP_ND_SOLICIT:
1045 mask->tp_src = OVS_BE16_MAX;
1046 mask->tp_dst = OVS_BE16_MAX;
1053 mask->nw_proto = 0xff;
1060 mask->dl_type = OVS_BE16_MAX;
1063 mask->vlan_tci |= htons(VLAN_CFI);
1071 /* Returns true if 'value' may be a valid value *as part of a masked match*,
1074 * A value is not rejected just because it is not valid for the field in
1075 * question, but only if it doesn't make sense to test the bits in question at
1076 * all. For example, the MFF_VLAN_TCI field will never have a nonzero value
1077 * without the VLAN_CFI bit being set, but we can't reject those values because
1078 * it is still legitimate to test just for those bits (see the documentation
1079 * for NXM_OF_VLAN_TCI in nicira-ext.h). On the other hand, there is never a
1080 * reason to set the low bit of MFF_IP_DSCP to 1, so we reject that. */
1082 mf_is_value_valid(const struct mf_field *mf, const union mf_value *value)
1093 case MFF_SKB_PRIORITY:
1116 case MFF_ICMPV4_TYPE:
1117 case MFF_ICMPV4_CODE:
1118 case MFF_ICMPV6_TYPE:
1119 case MFF_ICMPV6_CODE:
1125 case MFF_IN_PORT_OXM: {
1127 return !ofputil_port_from_ofp11(value->be32, &port);
1131 return !(value->u8 & ~IP_DSCP_MASK);
1132 case MFF_IP_DSCP_SHIFTED:
1133 return !(value->u8 & (~IP_DSCP_MASK >> 2));
1135 return !(value->u8 & ~IP_ECN_MASK);
1137 return !(value->u8 & ~FLOW_NW_FRAG_MASK);
1139 return !(value->be16 & ~htons(0x0fff));
1142 return !(value->be16 & htons(0xff00));
1145 return !(value->be16 & htons(VLAN_CFI | VLAN_PCP_MASK));
1147 return !(value->be16 & htons(VLAN_PCP_MASK));
1149 case MFF_DL_VLAN_PCP:
1151 return !(value->u8 & ~(VLAN_PCP_MASK >> VLAN_PCP_SHIFT));
1153 case MFF_IPV6_LABEL:
1154 return !(value->be32 & ~htonl(IPV6_LABEL_MASK));
1156 case MFF_MPLS_LABEL:
1157 return !(value->be32 & ~htonl(MPLS_LABEL_MASK >> MPLS_LABEL_SHIFT));
1160 return !(value->u8 & ~(MPLS_TC_MASK >> MPLS_TC_SHIFT));
1163 return !(value->u8 & ~(MPLS_BOS_MASK >> MPLS_BOS_SHIFT));
1171 /* Copies the value of field 'mf' from 'flow' into 'value'. The caller is
1172 * responsible for ensuring that 'flow' meets 'mf''s prerequisites. */
1174 mf_get_value(const struct mf_field *mf, const struct flow *flow,
1175 union mf_value *value)
1179 value->be64 = flow->tunnel.tun_id;
1182 value->be32 = flow->tunnel.ip_src;
1185 value->be32 = flow->tunnel.ip_dst;
1188 value->be16 = htons(flow->tunnel.flags);
1191 value->u8 = flow->tunnel.ip_ttl;
1194 value->u8 = flow->tunnel.ip_tos;
1198 value->be64 = flow->metadata;
1202 value->be16 = htons(ofp_to_u16(flow->in_port.ofp_port));
1204 case MFF_IN_PORT_OXM:
1205 value->be32 = ofputil_port_to_ofp11(flow->in_port.ofp_port);
1208 case MFF_SKB_PRIORITY:
1209 value->be32 = htonl(flow->skb_priority);
1213 value->be32 = htonl(flow->pkt_mark);
1217 value->be32 = htonl(flow->regs[mf->id - MFF_REG0]);
1221 memcpy(value->mac, flow->dl_src, ETH_ADDR_LEN);
1225 memcpy(value->mac, flow->dl_dst, ETH_ADDR_LEN);
1229 value->be16 = flow->dl_type;
1233 value->be16 = flow->vlan_tci;
1237 value->be16 = flow->vlan_tci & htons(VLAN_VID_MASK);
1240 value->be16 = flow->vlan_tci & htons(VLAN_VID_MASK | VLAN_CFI);
1243 case MFF_DL_VLAN_PCP:
1245 value->u8 = vlan_tci_to_pcp(flow->vlan_tci);
1248 case MFF_MPLS_LABEL:
1249 value->be32 = htonl(mpls_lse_to_label(flow->mpls_lse));
1253 value->u8 = mpls_lse_to_tc(flow->mpls_lse);
1257 value->u8 = mpls_lse_to_bos(flow->mpls_lse);
1261 value->be32 = flow->nw_src;
1265 value->be32 = flow->nw_dst;
1269 value->ipv6 = flow->ipv6_src;
1273 value->ipv6 = flow->ipv6_dst;
1276 case MFF_IPV6_LABEL:
1277 value->be32 = flow->ipv6_label;
1281 value->u8 = flow->nw_proto;
1285 value->u8 = flow->nw_tos & IP_DSCP_MASK;
1288 case MFF_IP_DSCP_SHIFTED:
1289 value->u8 = flow->nw_tos >> 2;
1293 value->u8 = flow->nw_tos & IP_ECN_MASK;
1297 value->u8 = flow->nw_ttl;
1301 value->u8 = flow->nw_frag;
1305 value->be16 = htons(flow->nw_proto);
1309 value->be32 = flow->nw_src;
1313 value->be32 = flow->nw_dst;
1318 memcpy(value->mac, flow->arp_sha, ETH_ADDR_LEN);
1323 memcpy(value->mac, flow->arp_tha, ETH_ADDR_LEN);
1329 value->be16 = flow->tp_src;
1335 value->be16 = flow->tp_dst;
1339 value->be16 = flow->tcp_flags;
1342 case MFF_ICMPV4_TYPE:
1343 case MFF_ICMPV6_TYPE:
1344 value->u8 = ntohs(flow->tp_src);
1347 case MFF_ICMPV4_CODE:
1348 case MFF_ICMPV6_CODE:
1349 value->u8 = ntohs(flow->tp_dst);
1353 value->ipv6 = flow->nd_target;
1362 /* Makes 'match' match field 'mf' exactly, with the value matched taken from
1363 * 'value'. The caller is responsible for ensuring that 'match' meets 'mf''s
1366 mf_set_value(const struct mf_field *mf,
1367 const union mf_value *value, struct match *match)
1371 match_set_tun_id(match, value->be64);
1374 match_set_tun_src(match, value->be32);
1377 match_set_tun_dst(match, value->be32);
1380 match_set_tun_flags(match, ntohs(value->be16));
1383 match_set_tun_tos(match, value->u8);
1386 match_set_tun_ttl(match, value->u8);
1390 match_set_metadata(match, value->be64);
1394 match_set_in_port(match, u16_to_ofp(ntohs(value->be16)));
1397 case MFF_IN_PORT_OXM: {
1399 ofputil_port_from_ofp11(value->be32, &port);
1400 match_set_in_port(match, port);
1404 case MFF_SKB_PRIORITY:
1405 match_set_skb_priority(match, ntohl(value->be32));
1409 match_set_pkt_mark(match, ntohl(value->be32));
1413 match_set_reg(match, mf->id - MFF_REG0, ntohl(value->be32));
1417 match_set_dl_src(match, value->mac);
1421 match_set_dl_dst(match, value->mac);
1425 match_set_dl_type(match, value->be16);
1429 match_set_dl_tci(match, value->be16);
1433 match_set_dl_vlan(match, value->be16);
1436 match_set_vlan_vid(match, value->be16);
1439 case MFF_DL_VLAN_PCP:
1441 match_set_dl_vlan_pcp(match, value->u8);
1444 case MFF_MPLS_LABEL:
1445 match_set_mpls_label(match, value->be32);
1449 match_set_mpls_tc(match, value->u8);
1453 match_set_mpls_bos(match, value->u8);
1457 match_set_nw_src(match, value->be32);
1461 match_set_nw_dst(match, value->be32);
1465 match_set_ipv6_src(match, &value->ipv6);
1469 match_set_ipv6_dst(match, &value->ipv6);
1472 case MFF_IPV6_LABEL:
1473 match_set_ipv6_label(match, value->be32);
1477 match_set_nw_proto(match, value->u8);
1481 match_set_nw_dscp(match, value->u8);
1484 case MFF_IP_DSCP_SHIFTED:
1485 match_set_nw_dscp(match, value->u8 << 2);
1489 match_set_nw_ecn(match, value->u8);
1493 match_set_nw_ttl(match, value->u8);
1497 match_set_nw_frag(match, value->u8);
1501 match_set_nw_proto(match, ntohs(value->be16));
1505 match_set_nw_src(match, value->be32);
1509 match_set_nw_dst(match, value->be32);
1514 match_set_arp_sha(match, value->mac);
1519 match_set_arp_tha(match, value->mac);
1525 match_set_tp_src(match, value->be16);
1531 match_set_tp_dst(match, value->be16);
1535 match_set_tcp_flags(match, value->be16);
1538 case MFF_ICMPV4_TYPE:
1539 case MFF_ICMPV6_TYPE:
1540 match_set_icmp_type(match, value->u8);
1543 case MFF_ICMPV4_CODE:
1544 case MFF_ICMPV6_CODE:
1545 match_set_icmp_code(match, value->u8);
1549 match_set_nd_target(match, &value->ipv6);
1558 /* Unwildcard 'mask' member field described by 'mf'. The caller is
1559 * responsible for ensuring that 'mask' meets 'mf''s prerequisites. */
1561 mf_mask_field(const struct mf_field *mf, struct flow *mask)
1563 static const union mf_value exact_match_mask = MF_EXACT_MASK_INITIALIZER;
1565 /* For MFF_DL_VLAN, we cannot send a all 1's to flow_set_dl_vlan()
1566 * as that will be considered as OFP10_VLAN_NONE. So consider it as a
1567 * special case. For the rest, calling mf_set_flow_value() is good
1569 if (mf->id == MFF_DL_VLAN) {
1570 flow_set_dl_vlan(mask, htons(VLAN_VID_MASK));
1572 mf_set_flow_value(mf, &exact_match_mask, mask);
1576 /* Sets 'flow' member field described by 'mf' to 'value'. The caller is
1577 * responsible for ensuring that 'flow' meets 'mf''s prerequisites.*/
1579 mf_set_flow_value(const struct mf_field *mf,
1580 const union mf_value *value, struct flow *flow)
1584 flow->tunnel.tun_id = value->be64;
1587 flow->tunnel.ip_src = value->be32;
1590 flow->tunnel.ip_dst = value->be32;
1593 flow->tunnel.flags = ntohs(value->be16);
1596 flow->tunnel.ip_tos = value->u8;
1599 flow->tunnel.ip_ttl = value->u8;
1603 flow->metadata = value->be64;
1607 flow->in_port.ofp_port = u16_to_ofp(ntohs(value->be16));
1610 case MFF_IN_PORT_OXM: {
1612 ofputil_port_from_ofp11(value->be32, &port);
1613 flow->in_port.ofp_port = port;
1617 case MFF_SKB_PRIORITY:
1618 flow->skb_priority = ntohl(value->be32);
1622 flow->pkt_mark = ntohl(value->be32);
1626 flow->regs[mf->id - MFF_REG0] = ntohl(value->be32);
1630 memcpy(flow->dl_src, value->mac, ETH_ADDR_LEN);
1634 memcpy(flow->dl_dst, value->mac, ETH_ADDR_LEN);
1638 flow->dl_type = value->be16;
1642 flow->vlan_tci = value->be16;
1646 flow_set_dl_vlan(flow, value->be16);
1649 flow_set_vlan_vid(flow, value->be16);
1652 case MFF_DL_VLAN_PCP:
1654 flow_set_vlan_pcp(flow, value->u8);
1657 case MFF_MPLS_LABEL:
1658 flow_set_mpls_label(flow, value->be32);
1662 flow_set_mpls_tc(flow, value->u8);
1666 flow_set_mpls_bos(flow, value->u8);
1670 flow->nw_src = value->be32;
1674 flow->nw_dst = value->be32;
1678 flow->ipv6_src = value->ipv6;
1682 flow->ipv6_dst = value->ipv6;
1685 case MFF_IPV6_LABEL:
1686 flow->ipv6_label = value->be32 & ~htonl(IPV6_LABEL_MASK);
1690 flow->nw_proto = value->u8;
1694 flow->nw_tos &= ~IP_DSCP_MASK;
1695 flow->nw_tos |= value->u8 & IP_DSCP_MASK;
1698 case MFF_IP_DSCP_SHIFTED:
1699 flow->nw_tos &= ~IP_DSCP_MASK;
1700 flow->nw_tos |= value->u8 << 2;
1704 flow->nw_tos &= ~IP_ECN_MASK;
1705 flow->nw_tos |= value->u8 & IP_ECN_MASK;
1709 flow->nw_ttl = value->u8;
1713 flow->nw_frag &= value->u8;
1717 flow->nw_proto = ntohs(value->be16);
1721 flow->nw_src = value->be32;
1725 flow->nw_dst = value->be32;
1730 memcpy(flow->arp_sha, value->mac, ETH_ADDR_LEN);
1735 memcpy(flow->arp_tha, value->mac, ETH_ADDR_LEN);
1741 flow->tp_src = value->be16;
1747 flow->tp_dst = value->be16;
1751 flow->tcp_flags = value->be16;
1754 case MFF_ICMPV4_TYPE:
1755 case MFF_ICMPV6_TYPE:
1756 flow->tp_src = htons(value->u8);
1759 case MFF_ICMPV4_CODE:
1760 case MFF_ICMPV6_CODE:
1761 flow->tp_dst = htons(value->u8);
1765 flow->nd_target = value->ipv6;
1774 /* Returns true if 'mf' has a zero value in 'flow', false if it is nonzero.
1776 * The caller is responsible for ensuring that 'flow' meets 'mf''s
1779 mf_is_zero(const struct mf_field *mf, const struct flow *flow)
1781 union mf_value value;
1783 mf_get_value(mf, flow, &value);
1784 return is_all_zeros((const uint8_t *) &value, mf->n_bytes);
1787 /* Makes 'match' wildcard field 'mf'.
1789 * The caller is responsible for ensuring that 'match' meets 'mf''s
1792 mf_set_wild(const struct mf_field *mf, struct match *match)
1796 match_set_tun_id_masked(match, htonll(0), htonll(0));
1799 match_set_tun_src_masked(match, htonl(0), htonl(0));
1802 match_set_tun_dst_masked(match, htonl(0), htonl(0));
1805 match_set_tun_flags_masked(match, 0, 0);
1808 match_set_tun_tos_masked(match, 0, 0);
1811 match_set_tun_ttl_masked(match, 0, 0);
1815 match_set_metadata_masked(match, htonll(0), htonll(0));
1819 case MFF_IN_PORT_OXM:
1820 match->flow.in_port.ofp_port = 0;
1821 match->wc.masks.in_port.ofp_port = 0;
1824 case MFF_SKB_PRIORITY:
1825 match->flow.skb_priority = 0;
1826 match->wc.masks.skb_priority = 0;
1830 match->flow.pkt_mark = 0;
1831 match->wc.masks.pkt_mark = 0;
1835 match_set_reg_masked(match, mf->id - MFF_REG0, 0, 0);
1839 memset(match->flow.dl_src, 0, ETH_ADDR_LEN);
1840 memset(match->wc.masks.dl_src, 0, ETH_ADDR_LEN);
1844 memset(match->flow.dl_dst, 0, ETH_ADDR_LEN);
1845 memset(match->wc.masks.dl_dst, 0, ETH_ADDR_LEN);
1849 match->flow.dl_type = htons(0);
1850 match->wc.masks.dl_type = htons(0);
1854 match_set_dl_tci_masked(match, htons(0), htons(0));
1859 match_set_any_vid(match);
1862 case MFF_DL_VLAN_PCP:
1864 match_set_any_pcp(match);
1867 case MFF_MPLS_LABEL:
1868 match_set_any_mpls_label(match);
1872 match_set_any_mpls_tc(match);
1876 match_set_any_mpls_bos(match);
1881 match_set_nw_src_masked(match, htonl(0), htonl(0));
1886 match_set_nw_dst_masked(match, htonl(0), htonl(0));
1890 memset(&match->wc.masks.ipv6_src, 0, sizeof match->wc.masks.ipv6_src);
1891 memset(&match->flow.ipv6_src, 0, sizeof match->flow.ipv6_src);
1895 memset(&match->wc.masks.ipv6_dst, 0, sizeof match->wc.masks.ipv6_dst);
1896 memset(&match->flow.ipv6_dst, 0, sizeof match->flow.ipv6_dst);
1899 case MFF_IPV6_LABEL:
1900 match->wc.masks.ipv6_label = htonl(0);
1901 match->flow.ipv6_label = htonl(0);
1905 match->wc.masks.nw_proto = 0;
1906 match->flow.nw_proto = 0;
1910 case MFF_IP_DSCP_SHIFTED:
1911 match->wc.masks.nw_tos &= ~IP_DSCP_MASK;
1912 match->flow.nw_tos &= ~IP_DSCP_MASK;
1916 match->wc.masks.nw_tos &= ~IP_ECN_MASK;
1917 match->flow.nw_tos &= ~IP_ECN_MASK;
1921 match->wc.masks.nw_ttl = 0;
1922 match->flow.nw_ttl = 0;
1926 match->wc.masks.nw_frag |= FLOW_NW_FRAG_MASK;
1927 match->flow.nw_frag &= ~FLOW_NW_FRAG_MASK;
1931 match->wc.masks.nw_proto = 0;
1932 match->flow.nw_proto = 0;
1937 memset(match->flow.arp_sha, 0, ETH_ADDR_LEN);
1938 memset(match->wc.masks.arp_sha, 0, ETH_ADDR_LEN);
1943 memset(match->flow.arp_tha, 0, ETH_ADDR_LEN);
1944 memset(match->wc.masks.arp_tha, 0, ETH_ADDR_LEN);
1950 case MFF_ICMPV4_TYPE:
1951 case MFF_ICMPV6_TYPE:
1952 match->wc.masks.tp_src = htons(0);
1953 match->flow.tp_src = htons(0);
1959 case MFF_ICMPV4_CODE:
1960 case MFF_ICMPV6_CODE:
1961 match->wc.masks.tp_dst = htons(0);
1962 match->flow.tp_dst = htons(0);
1966 match->wc.masks.tcp_flags = htons(0);
1967 match->flow.tcp_flags = htons(0);
1971 memset(&match->wc.masks.nd_target, 0,
1972 sizeof match->wc.masks.nd_target);
1973 memset(&match->flow.nd_target, 0, sizeof match->flow.nd_target);
1982 /* Makes 'match' match field 'mf' with the specified 'value' and 'mask'.
1983 * 'value' specifies a value to match and 'mask' specifies a wildcard pattern,
1984 * with a 1-bit indicating that the corresponding value bit must match and a
1985 * 0-bit indicating a don't-care.
1987 * If 'mask' is NULL or points to all-1-bits, then this call is equivalent to
1988 * mf_set_value(mf, value, match). If 'mask' points to all-0-bits, then this
1989 * call is equivalent to mf_set_wild(mf, match).
1991 * 'mask' must be a valid mask for 'mf' (see mf_is_mask_valid()). The caller
1992 * is responsible for ensuring that 'match' meets 'mf''s prerequisites. */
1993 enum ofputil_protocol
1994 mf_set(const struct mf_field *mf,
1995 const union mf_value *value, const union mf_value *mask,
1996 struct match *match)
1998 if (!mask || is_all_ones((const uint8_t *) mask, mf->n_bytes)) {
1999 mf_set_value(mf, value, match);
2000 return mf->usable_protocols;
2001 } else if (is_all_zeros((const uint8_t *) mask, mf->n_bytes)) {
2002 mf_set_wild(mf, match);
2003 return OFPUTIL_P_ANY;
2008 case MFF_IN_PORT_OXM:
2009 case MFF_SKB_PRIORITY:
2012 case MFF_DL_VLAN_PCP:
2014 case MFF_MPLS_LABEL:
2020 case MFF_IP_DSCP_SHIFTED:
2023 case MFF_ICMPV4_TYPE:
2024 case MFF_ICMPV4_CODE:
2025 case MFF_ICMPV6_TYPE:
2026 case MFF_ICMPV6_CODE:
2027 return OFPUTIL_P_NONE;
2030 match_set_tun_id_masked(match, value->be64, mask->be64);
2033 match_set_tun_src_masked(match, value->be32, mask->be32);
2036 match_set_tun_dst_masked(match, value->be32, mask->be32);
2039 match_set_tun_flags_masked(match, ntohs(value->be16), ntohs(mask->be16));
2042 match_set_tun_ttl_masked(match, value->u8, mask->u8);
2045 match_set_tun_tos_masked(match, value->u8, mask->u8);
2049 match_set_metadata_masked(match, value->be64, mask->be64);
2053 match_set_reg_masked(match, mf->id - MFF_REG0,
2054 ntohl(value->be32), ntohl(mask->be32));
2058 match_set_pkt_mark_masked(match, ntohl(value->be32),
2063 match_set_dl_dst_masked(match, value->mac, mask->mac);
2067 match_set_dl_src_masked(match, value->mac, mask->mac);
2072 match_set_arp_sha_masked(match, value->mac, mask->mac);
2077 match_set_arp_tha_masked(match, value->mac, mask->mac);
2081 match_set_dl_tci_masked(match, value->be16, mask->be16);
2085 match_set_vlan_vid_masked(match, value->be16, mask->be16);
2089 match_set_nw_src_masked(match, value->be32, mask->be32);
2093 match_set_nw_dst_masked(match, value->be32, mask->be32);
2097 match_set_ipv6_src_masked(match, &value->ipv6, &mask->ipv6);
2101 match_set_ipv6_dst_masked(match, &value->ipv6, &mask->ipv6);
2104 case MFF_IPV6_LABEL:
2105 if ((mask->be32 & htonl(IPV6_LABEL_MASK)) == htonl(IPV6_LABEL_MASK)) {
2106 mf_set_value(mf, value, match);
2108 match_set_ipv6_label_masked(match, value->be32, mask->be32);
2113 match_set_nd_target_masked(match, &value->ipv6, &mask->ipv6);
2117 match_set_nw_frag_masked(match, value->u8, mask->u8);
2121 match_set_nw_src_masked(match, value->be32, mask->be32);
2125 match_set_nw_dst_masked(match, value->be32, mask->be32);
2131 match_set_tp_src_masked(match, value->be16, mask->be16);
2137 match_set_tp_dst_masked(match, value->be16, mask->be16);
2141 match_set_tcp_flags_masked(match, value->be16, mask->be16);
2149 return mf->usable_protocols_bitwise;
2152 return ip_is_cidr(mask->be32) ? mf->usable_protocols :
2153 mf->usable_protocols_bitwise;
2157 mf_check__(const struct mf_subfield *sf, const struct flow *flow,
2161 VLOG_WARN_RL(&rl, "unknown %s field", type);
2162 return OFPERR_OFPBAC_BAD_SET_TYPE;
2163 } else if (!sf->n_bits) {
2164 VLOG_WARN_RL(&rl, "zero bit %s field %s", type, sf->field->name);
2165 return OFPERR_OFPBAC_BAD_SET_LEN;
2166 } else if (sf->ofs >= sf->field->n_bits) {
2167 VLOG_WARN_RL(&rl, "bit offset %d exceeds %d-bit width of %s field %s",
2168 sf->ofs, sf->field->n_bits, type, sf->field->name);
2169 return OFPERR_OFPBAC_BAD_SET_LEN;
2170 } else if (sf->ofs + sf->n_bits > sf->field->n_bits) {
2171 VLOG_WARN_RL(&rl, "bit offset %d and width %d exceeds %d-bit width "
2172 "of %s field %s", sf->ofs, sf->n_bits,
2173 sf->field->n_bits, type, sf->field->name);
2174 return OFPERR_OFPBAC_BAD_SET_LEN;
2175 } else if (flow && !mf_are_prereqs_ok(sf->field, flow)) {
2176 VLOG_WARN_RL(&rl, "%s field %s lacks correct prerequisites",
2177 type, sf->field->name);
2178 return OFPERR_OFPBAC_MATCH_INCONSISTENT;
2184 /* Checks whether 'sf' is valid for reading a subfield out of 'flow'. Returns
2185 * 0 if so, otherwise an OpenFlow error code (e.g. as returned by
2188 mf_check_src(const struct mf_subfield *sf, const struct flow *flow)
2190 return mf_check__(sf, flow, "source");
2193 /* Checks whether 'sf' is valid for writing a subfield into 'flow'. Returns 0
2194 * if so, otherwise an OpenFlow error code (e.g. as returned by
2197 mf_check_dst(const struct mf_subfield *sf, const struct flow *flow)
2199 int error = mf_check__(sf, flow, "destination");
2200 if (!error && !sf->field->writable) {
2201 VLOG_WARN_RL(&rl, "destination field %s is not writable",
2203 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
2208 /* Copies the value and wildcard bit pattern for 'mf' from 'match' into the
2209 * 'value' and 'mask', respectively. */
2211 mf_get(const struct mf_field *mf, const struct match *match,
2212 union mf_value *value, union mf_value *mask)
2214 mf_get_value(mf, &match->flow, value);
2215 mf_get_mask(mf, &match->wc, mask);
2218 /* Assigns a random value for field 'mf' to 'value'. */
2220 mf_random_value(const struct mf_field *mf, union mf_value *value)
2222 random_bytes(value, mf->n_bytes);
2234 case MFF_SKB_PRIORITY:
2256 case MFF_ICMPV4_TYPE:
2257 case MFF_ICMPV4_CODE:
2258 case MFF_ICMPV6_TYPE:
2259 case MFF_ICMPV6_CODE:
2266 value->be16 &= htons(0x0fff);
2269 case MFF_IN_PORT_OXM:
2270 value->be32 = ofputil_port_to_ofp11(u16_to_ofp(ntohs(value->be16)));
2273 case MFF_IPV6_LABEL:
2274 value->be32 &= htonl(IPV6_LABEL_MASK);
2278 value->u8 &= IP_DSCP_MASK;
2281 case MFF_IP_DSCP_SHIFTED:
2282 value->u8 &= IP_DSCP_MASK >> 2;
2286 value->u8 &= IP_ECN_MASK;
2290 value->u8 &= FLOW_NW_FRAG_MASK;
2294 value->be16 &= htons(0xff);
2298 value->be16 &= htons(VLAN_VID_MASK);
2301 value->be16 &= htons(VLAN_VID_MASK | VLAN_CFI);
2304 case MFF_DL_VLAN_PCP:
2309 case MFF_MPLS_LABEL:
2310 value->be32 &= htonl(MPLS_LABEL_MASK >> MPLS_LABEL_SHIFT);
2314 value->u8 &= MPLS_TC_MASK >> MPLS_TC_SHIFT;
2318 value->u8 &= MPLS_BOS_MASK >> MPLS_BOS_SHIFT;
2328 mf_from_integer_string(const struct mf_field *mf, const char *s,
2329 uint8_t *valuep, uint8_t *maskp)
2331 unsigned long long int integer, mask;
2336 integer = strtoull(s, &tail, 0);
2337 if (errno || (*tail != '\0' && *tail != '/')) {
2342 mask = strtoull(tail + 1, &tail, 0);
2343 if (errno || *tail != '\0') {
2350 for (i = mf->n_bytes - 1; i >= 0; i--) {
2351 valuep[i] = integer;
2357 return xasprintf("%s: value too large for %u-byte field %s",
2358 s, mf->n_bytes, mf->name);
2363 return xasprintf("%s: bad syntax for %s", s, mf->name);
2367 mf_from_ethernet_string(const struct mf_field *mf, const char *s,
2368 uint8_t mac[ETH_ADDR_LEN],
2369 uint8_t mask[ETH_ADDR_LEN])
2373 ovs_assert(mf->n_bytes == ETH_ADDR_LEN);
2376 if (ovs_scan(s, ETH_ADDR_SCAN_FMT"%n", ETH_ADDR_SCAN_ARGS(mac), &n)
2377 && n == strlen(s)) {
2378 memset(mask, 0xff, ETH_ADDR_LEN);
2383 if (ovs_scan(s, ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT"%n",
2384 ETH_ADDR_SCAN_ARGS(mac), ETH_ADDR_SCAN_ARGS(mask), &n)
2385 && n == strlen(s)) {
2389 return xasprintf("%s: invalid Ethernet address", s);
2393 mf_from_ipv4_string(const struct mf_field *mf, const char *s,
2394 ovs_be32 *ip, ovs_be32 *mask)
2398 ovs_assert(mf->n_bytes == sizeof *ip);
2400 if (ovs_scan(s, IP_SCAN_FMT"/"IP_SCAN_FMT,
2401 IP_SCAN_ARGS(ip), IP_SCAN_ARGS(mask))) {
2403 } else if (ovs_scan(s, IP_SCAN_FMT"/%d", IP_SCAN_ARGS(ip), &prefix)) {
2404 if (prefix <= 0 || prefix > 32) {
2405 return xasprintf("%s: network prefix bits not between 1 and "
2407 } else if (prefix == 32) {
2408 *mask = OVS_BE32_MAX;
2410 *mask = htonl(((1u << prefix) - 1) << (32 - prefix));
2412 } else if (ovs_scan(s, IP_SCAN_FMT, IP_SCAN_ARGS(ip))) {
2413 *mask = OVS_BE32_MAX;
2415 return xasprintf("%s: invalid IP address", s);
2421 mf_from_ipv6_string(const struct mf_field *mf, const char *s,
2422 struct in6_addr *value, struct in6_addr *mask)
2424 char *str = xstrdup(s);
2425 char *save_ptr = NULL;
2426 const char *name, *netmask;
2429 ovs_assert(mf->n_bytes == sizeof *value);
2431 name = strtok_r(str, "/", &save_ptr);
2432 retval = name ? lookup_ipv6(name, value) : EINVAL;
2436 err = xasprintf("%s: could not convert to IPv6 address", str);
2442 netmask = strtok_r(NULL, "/", &save_ptr);
2444 if (inet_pton(AF_INET6, netmask, mask) != 1) {
2445 int prefix = atoi(netmask);
2446 if (prefix <= 0 || prefix > 128) {
2448 return xasprintf("%s: prefix bits not between 1 and 128", s);
2450 *mask = ipv6_create_mask(prefix);
2454 *mask = in6addr_exact;
2462 mf_from_ofp_port_string(const struct mf_field *mf, const char *s,
2463 ovs_be16 *valuep, ovs_be16 *maskp)
2467 ovs_assert(mf->n_bytes == sizeof(ovs_be16));
2469 if (ofputil_port_from_string(s, &port)) {
2470 *valuep = htons(ofp_to_u16(port));
2471 *maskp = OVS_BE16_MAX;
2474 return xasprintf("%s: port value out of range for %s", s, mf->name);
2478 mf_from_ofp_port_string32(const struct mf_field *mf, const char *s,
2479 ovs_be32 *valuep, ovs_be32 *maskp)
2483 ovs_assert(mf->n_bytes == sizeof(ovs_be32));
2484 if (ofputil_port_from_string(s, &port)) {
2485 *valuep = ofputil_port_to_ofp11(port);
2486 *maskp = OVS_BE32_MAX;
2489 return xasprintf("%s: port value out of range for %s", s, mf->name);
2492 struct frag_handling {
2498 static const struct frag_handling all_frags[] = {
2499 #define A FLOW_NW_FRAG_ANY
2500 #define L FLOW_NW_FRAG_LATER
2501 /* name mask value */
2504 { "first", A|L, A },
2505 { "later", A|L, A|L },
2510 { "not_later", L, 0 },
2517 mf_from_frag_string(const char *s, uint8_t *valuep, uint8_t *maskp)
2519 const struct frag_handling *h;
2521 for (h = all_frags; h < &all_frags[ARRAY_SIZE(all_frags)]; h++) {
2522 if (!strcasecmp(s, h->name)) {
2523 /* We force the upper bits of the mask on to make mf_parse_value()
2524 * happy (otherwise it will never think it's an exact match.) */
2525 *maskp = h->mask | ~FLOW_NW_FRAG_MASK;
2531 return xasprintf("%s: unknown fragment type (valid types are \"no\", "
2532 "\"yes\", \"first\", \"later\", \"not_first\"", s);
2536 parse_flow_tun_flags(const char *s_, const char *(*bit_to_string)(uint32_t),
2539 uint32_t result = 0;
2540 char *save_ptr = NULL;
2543 char *s = xstrdup(s_);
2545 for (name = strtok_r((char *)s, " |", &save_ptr); name;
2546 name = strtok_r(NULL, " |", &save_ptr)) {
2548 unsigned long long int flags;
2551 if (ovs_scan(name, "%lli", &flags)) {
2555 name_len = strlen(name);
2556 for (bit = 1; bit; bit <<= 1) {
2557 const char *fname = bit_to_string(bit);
2564 len = strlen(fname);
2565 if (len != name_len) {
2568 if (!strncmp(name, fname, len)) {
2580 *res = htons(result);
2587 mf_from_tun_flags_string(const char *s, ovs_be16 *valuep, ovs_be16 *maskp)
2589 if (!parse_flow_tun_flags(s, flow_tun_flag_to_string, valuep)) {
2590 *maskp = OVS_BE16_MAX;
2594 return xasprintf("%s: unknown tunnel flags (valid flags are \"df\", "
2595 "\"csum\", \"key\")", s);
2598 /* Parses 's', a string value for field 'mf', into 'value' and 'mask'. Returns
2599 * NULL if successful, otherwise a malloc()'d string describing the error. */
2601 mf_parse(const struct mf_field *mf, const char *s,
2602 union mf_value *value, union mf_value *mask)
2606 if (!strcmp(s, "*")) {
2607 memset(value, 0, mf->n_bytes);
2608 memset(mask, 0, mf->n_bytes);
2612 switch (mf->string) {
2614 case MFS_HEXADECIMAL:
2615 error = mf_from_integer_string(mf, s,
2616 (uint8_t *) value, (uint8_t *) mask);
2620 error = mf_from_ethernet_string(mf, s, value->mac, mask->mac);
2624 error = mf_from_ipv4_string(mf, s, &value->be32, &mask->be32);
2628 error = mf_from_ipv6_string(mf, s, &value->ipv6, &mask->ipv6);
2632 error = mf_from_ofp_port_string(mf, s, &value->be16, &mask->be16);
2635 case MFS_OFP_PORT_OXM:
2636 error = mf_from_ofp_port_string32(mf, s, &value->be32, &mask->be32);
2640 error = mf_from_frag_string(s, &value->u8, &mask->u8);
2644 ovs_assert(mf->n_bytes == sizeof(ovs_be16));
2645 error = mf_from_tun_flags_string(s, &value->be16, &mask->be16);
2652 if (!error && !mf_is_mask_valid(mf, mask)) {
2653 error = xasprintf("%s: invalid mask for field %s", s, mf->name);
2658 /* Parses 's', a string value for field 'mf', into 'value'. Returns NULL if
2659 * successful, otherwise a malloc()'d string describing the error. */
2661 mf_parse_value(const struct mf_field *mf, const char *s, union mf_value *value)
2663 union mf_value mask;
2666 error = mf_parse(mf, s, value, &mask);
2671 if (!is_all_ones((const uint8_t *) &mask, mf->n_bytes)) {
2672 return xasprintf("%s: wildcards not allowed here", s);
2678 mf_format_integer_string(const struct mf_field *mf, const uint8_t *valuep,
2679 const uint8_t *maskp, struct ds *s)
2681 unsigned long long int integer;
2684 ovs_assert(mf->n_bytes <= 8);
2687 for (i = 0; i < mf->n_bytes; i++) {
2688 integer = (integer << 8) | valuep[i];
2690 if (mf->string == MFS_HEXADECIMAL) {
2691 ds_put_format(s, "%#llx", integer);
2693 ds_put_format(s, "%lld", integer);
2697 unsigned long long int mask;
2700 for (i = 0; i < mf->n_bytes; i++) {
2701 mask = (mask << 8) | maskp[i];
2704 /* I guess we could write the mask in decimal for MFS_DECIMAL but I'm
2705 * not sure that that a bit-mask written in decimal is ever easier to
2706 * understand than the same bit-mask written in hexadecimal. */
2707 ds_put_format(s, "/%#llx", mask);
2712 mf_format_frag_string(uint8_t value, uint8_t mask, struct ds *s)
2714 const struct frag_handling *h;
2716 mask &= FLOW_NW_FRAG_MASK;
2719 for (h = all_frags; h < &all_frags[ARRAY_SIZE(all_frags)]; h++) {
2720 if (value == h->value && mask == h->mask) {
2721 ds_put_cstr(s, h->name);
2725 ds_put_cstr(s, "<error>");
2729 mf_format_tnl_flags_string(const ovs_be16 *valuep, struct ds *s)
2731 format_flags(s, flow_tun_flag_to_string, ntohs(*valuep), '|');
2734 /* Appends to 's' a string representation of field 'mf' whose value is in
2735 * 'value' and 'mask'. 'mask' may be NULL to indicate an exact match. */
2737 mf_format(const struct mf_field *mf,
2738 const union mf_value *value, const union mf_value *mask,
2742 if (is_all_zeros((const uint8_t *) mask, mf->n_bytes)) {
2743 ds_put_cstr(s, "ANY");
2745 } else if (is_all_ones((const uint8_t *) mask, mf->n_bytes)) {
2750 switch (mf->string) {
2751 case MFS_OFP_PORT_OXM:
2754 ofputil_port_from_ofp11(value->be32, &port);
2755 ofputil_format_port(port, s);
2761 ofputil_format_port(u16_to_ofp(ntohs(value->be16)), s);
2766 case MFS_HEXADECIMAL:
2767 mf_format_integer_string(mf, (uint8_t *) value, (uint8_t *) mask, s);
2771 eth_format_masked(value->mac, mask->mac, s);
2775 ip_format_masked(value->be32, mask ? mask->be32 : OVS_BE32_MAX, s);
2779 print_ipv6_masked(s, &value->ipv6, mask ? &mask->ipv6 : NULL);
2783 mf_format_frag_string(value->u8, mask ? mask->u8 : UINT8_MAX, s);
2787 mf_format_tnl_flags_string(&value->be16, s);
2795 /* Makes subfield 'sf' within 'flow' exactly match the 'sf->n_bits'
2796 * least-significant bits in 'x'.
2799 mf_write_subfield_flow(const struct mf_subfield *sf,
2800 const union mf_subvalue *x, struct flow *flow)
2802 const struct mf_field *field = sf->field;
2803 union mf_value value;
2805 mf_get_value(field, flow, &value);
2806 bitwise_copy(x, sizeof *x, 0, &value, field->n_bytes,
2807 sf->ofs, sf->n_bits);
2808 mf_set_flow_value(field, &value, flow);
2811 /* Makes subfield 'sf' within 'match' exactly match the 'sf->n_bits'
2812 * least-significant bits in 'x'.
2815 mf_write_subfield(const struct mf_subfield *sf, const union mf_subvalue *x,
2816 struct match *match)
2818 const struct mf_field *field = sf->field;
2819 union mf_value value, mask;
2821 mf_get(field, match, &value, &mask);
2822 bitwise_copy(x, sizeof *x, 0, &value, field->n_bytes, sf->ofs, sf->n_bits);
2823 bitwise_one ( &mask, field->n_bytes, sf->ofs, sf->n_bits);
2824 mf_set(field, &value, &mask, match);
2827 /* Initializes 'x' to the value of 'sf' within 'flow'. 'sf' must be valid for
2828 * reading 'flow', e.g. as checked by mf_check_src(). */
2830 mf_read_subfield(const struct mf_subfield *sf, const struct flow *flow,
2831 union mf_subvalue *x)
2833 union mf_value value;
2835 mf_get_value(sf->field, flow, &value);
2837 memset(x, 0, sizeof *x);
2838 bitwise_copy(&value, sf->field->n_bytes, sf->ofs,
2843 /* Returns the value of 'sf' within 'flow'. 'sf' must be valid for reading
2844 * 'flow', e.g. as checked by mf_check_src() and sf->n_bits must be 64 or
2847 mf_get_subfield(const struct mf_subfield *sf, const struct flow *flow)
2849 union mf_value value;
2851 mf_get_value(sf->field, flow, &value);
2852 return bitwise_get(&value, sf->field->n_bytes, sf->ofs, sf->n_bits);
2855 /* Formats 'sf' into 's' in a format normally acceptable to
2856 * mf_parse_subfield(). (It won't be acceptable if sf->field is NULL or if
2857 * sf->field has no NXM name.) */
2859 mf_format_subfield(const struct mf_subfield *sf, struct ds *s)
2862 ds_put_cstr(s, "<unknown>");
2863 } else if (sf->field->nxm_name) {
2864 ds_put_cstr(s, sf->field->nxm_name);
2865 } else if (sf->field->nxm_header) {
2866 uint32_t header = sf->field->nxm_header;
2867 ds_put_format(s, "%d:%d", NXM_VENDOR(header), NXM_FIELD(header));
2869 ds_put_cstr(s, sf->field->name);
2872 if (sf->field && sf->ofs == 0 && sf->n_bits == sf->field->n_bits) {
2873 ds_put_cstr(s, "[]");
2874 } else if (sf->n_bits == 1) {
2875 ds_put_format(s, "[%d]", sf->ofs);
2877 ds_put_format(s, "[%d..%d]", sf->ofs, sf->ofs + sf->n_bits - 1);
2881 static const struct mf_field *
2882 mf_parse_subfield_name(const char *name, int name_len, bool *wild)
2886 *wild = name_len > 2 && !memcmp(&name[name_len - 2], "_W", 2);
2891 for (i = 0; i < MFF_N_IDS; i++) {
2892 const struct mf_field *mf = mf_from_id(i);
2895 && !strncmp(mf->nxm_name, name, name_len)
2896 && mf->nxm_name[name_len] == '\0') {
2900 && !strncmp(mf->oxm_name, name, name_len)
2901 && mf->oxm_name[name_len] == '\0') {
2909 /* Parses a subfield from the beginning of '*sp' into 'sf'. If successful,
2910 * returns NULL and advances '*sp' to the first byte following the parsed
2911 * string. On failure, returns a malloc()'d error message, does not modify
2912 * '*sp', and does not properly initialize 'sf'.
2914 * The syntax parsed from '*sp' takes the form "header[start..end]" where
2915 * 'header' is the name of an NXM field and 'start' and 'end' are (inclusive)
2916 * bit indexes. "..end" may be omitted to indicate a single bit. "start..end"
2917 * may both be omitted (the [] are still required) to indicate an entire
2919 char * WARN_UNUSED_RESULT
2920 mf_parse_subfield__(struct mf_subfield *sf, const char **sp)
2922 const struct mf_field *field;
2931 name_len = strcspn(s, "[");
2932 if (s[name_len] != '[') {
2933 return xasprintf("%s: missing [ looking for field name", *sp);
2936 field = mf_parse_subfield_name(name, name_len, &wild);
2938 return xasprintf("%s: unknown field `%.*s'", *sp, name_len, s);
2942 if (ovs_scan(s, "[%d..%d]", &start, &end)) {
2943 /* Nothing to do. */
2944 } else if (ovs_scan(s, "[%d]", &start)) {
2946 } else if (!strncmp(s, "[]", 2)) {
2948 end = field->n_bits - 1;
2950 return xasprintf("%s: syntax error expecting [] or [<bit>] or "
2951 "[<start>..<end>]", *sp);
2953 s = strchr(s, ']') + 1;
2956 return xasprintf("%s: starting bit %d is after ending bit %d",
2958 } else if (start >= field->n_bits) {
2959 return xasprintf("%s: starting bit %d is not valid because field is "
2960 "only %d bits wide", *sp, start, field->n_bits);
2961 } else if (end >= field->n_bits){
2962 return xasprintf("%s: ending bit %d is not valid because field is "
2963 "only %d bits wide", *sp, end, field->n_bits);
2968 sf->n_bits = end - start + 1;
2974 /* Parses a subfield from the entirety of 's' into 'sf'. Returns NULL if
2975 * successful, otherwise a malloc()'d string describing the error. The caller
2976 * is responsible for freeing the returned string.
2978 * The syntax parsed from 's' takes the form "header[start..end]" where
2979 * 'header' is the name of an NXM field and 'start' and 'end' are (inclusive)
2980 * bit indexes. "..end" may be omitted to indicate a single bit. "start..end"
2981 * may both be omitted (the [] are still required) to indicate an entire
2983 char * WARN_UNUSED_RESULT
2984 mf_parse_subfield(struct mf_subfield *sf, const char *s)
2986 char *error = mf_parse_subfield__(sf, &s);
2987 if (!error && s[0]) {
2988 error = xstrdup("unexpected input following field syntax");
2994 mf_format_subvalue(const union mf_subvalue *subvalue, struct ds *s)
2998 for (i = 0; i < ARRAY_SIZE(subvalue->u8); i++) {
2999 if (subvalue->u8[i]) {
3000 ds_put_format(s, "0x%"PRIx8, subvalue->u8[i]);
3001 for (i++; i < ARRAY_SIZE(subvalue->u8); i++) {
3002 ds_put_format(s, "%02"PRIx8, subvalue->u8[i]);
3007 ds_put_char(s, '0');