2 * Copyright (c) 2011, 2012 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"
24 #include <netinet/icmp6.h>
25 #include <netinet/ip6.h>
27 #include "classifier.h"
28 #include "dynamic-string.h"
29 #include "ofp-errors.h"
34 #include "socket-util.h"
35 #include "unaligned.h"
38 VLOG_DEFINE_THIS_MODULE(meta_flow);
40 #define MF_FIELD_SIZES(MEMBER) \
41 sizeof ((union mf_value *)0)->MEMBER, \
42 8 * sizeof ((union mf_value *)0)->MEMBER
44 static const struct mf_field mf_fields[MFF_N_IDS] = {
50 MFF_TUN_ID, "tun_id", NULL,
56 NXM_NX_TUN_ID, "NXM_NX_TUN_ID",
57 NXM_NX_TUN_ID, "NXM_NX_TUN_ID",
59 MFF_TUN_SRC, "tun_src", NULL,
68 MFF_TUN_DST, "tun_dst", NULL,
77 MFF_TUN_FLAGS, "tun_flags", NULL,
86 MFF_TUN_TOS, "tun_tos", NULL,
95 MFF_TUN_TTL, "tun_ttl", NULL,
104 MFF_METADATA, "metadata", NULL,
105 MF_FIELD_SIZES(be64),
110 OXM_OF_METADATA, "OXM_OF_METADATA",
111 OXM_OF_METADATA, "OXM_OF_METADATA",
113 MFF_IN_PORT, "in_port", NULL,
114 MF_FIELD_SIZES(be16),
119 NXM_OF_IN_PORT, "NXM_OF_IN_PORT",
120 OXM_OF_IN_PORT, "OXM_OF_IN_PORT",
123 #define REGISTER(IDX) \
125 MFF_REG##IDX, "reg" #IDX, NULL, \
126 MF_FIELD_SIZES(be32), \
131 NXM_NX_REG(IDX), "NXM_NX_REG" #IDX, \
132 NXM_NX_REG(IDX), "NXM_NX_REG" #IDX, \
167 MFF_ETH_SRC, "eth_src", "dl_src",
173 NXM_OF_ETH_SRC, "NXM_OF_ETH_SRC",
174 OXM_OF_ETH_SRC, "OXM_OF_ETH_SRC",
176 MFF_ETH_DST, "eth_dst", "dl_dst",
182 NXM_OF_ETH_DST, "NXM_OF_ETH_DST",
183 OXM_OF_ETH_DST, "OXM_OF_ETH_DST",
185 MFF_ETH_TYPE, "eth_type", "dl_type",
186 MF_FIELD_SIZES(be16),
191 NXM_OF_ETH_TYPE, "NXM_OF_ETH_TYPE",
192 OXM_OF_ETH_TYPE, "OXM_OF_ETH_TYPE",
196 MFF_VLAN_TCI, "vlan_tci", NULL,
197 MF_FIELD_SIZES(be16),
202 NXM_OF_VLAN_TCI, "NXM_OF_VLAN_TCI",
203 NXM_OF_VLAN_TCI, "NXM_OF_VLAN_TCI",
205 MFF_DL_VLAN, "dl_vlan", NULL,
206 sizeof(ovs_be16), 12,
214 MFF_VLAN_VID, "vlan_vid", NULL,
215 sizeof(ovs_be16), 12,
220 OXM_OF_VLAN_VID, "OXM_OF_VLAN_VID",
221 OXM_OF_VLAN_VID, "OXM_OF_VLAN_VID",
223 MFF_DL_VLAN_PCP, "dl_vlan_pcp", NULL,
232 MFF_VLAN_PCP, "vlan_pcp", NULL,
238 OXM_OF_VLAN_PCP, "OXM_OF_VLAN_PCP",
239 OXM_OF_VLAN_PCP, "OXM_OF_VLAN_PCP",
247 MFF_IPV4_SRC, "ip_src", "nw_src",
248 MF_FIELD_SIZES(be32),
253 NXM_OF_IP_SRC, "NXM_OF_IP_SRC",
254 OXM_OF_IPV4_SRC, "OXM_OF_IPV4_SRC",
256 MFF_IPV4_DST, "ip_dst", "nw_dst",
257 MF_FIELD_SIZES(be32),
262 NXM_OF_IP_DST, "NXM_OF_IP_DST",
263 OXM_OF_IPV4_DST, "OXM_OF_IPV4_DST",
267 MFF_IPV6_SRC, "ipv6_src", NULL,
268 MF_FIELD_SIZES(ipv6),
273 NXM_NX_IPV6_SRC, "NXM_NX_IPV6_SRC",
274 OXM_OF_IPV6_SRC, "OXM_OF_IPV6_SRC",
276 MFF_IPV6_DST, "ipv6_dst", NULL,
277 MF_FIELD_SIZES(ipv6),
282 NXM_NX_IPV6_DST, "NXM_NX_IPV6_DST",
283 OXM_OF_IPV6_DST, "OXM_OF_IPV6_DST",
286 MFF_IPV6_LABEL, "ipv6_label", NULL,
292 NXM_NX_IPV6_LABEL, "NXM_NX_IPV6_LABEL",
293 OXM_OF_IPV6_FLABEL, "OXM_OF_IPV6_FLABEL",
297 MFF_IP_PROTO, "nw_proto", NULL,
303 NXM_OF_IP_PROTO, "NXM_OF_IP_PROTO",
304 OXM_OF_IP_PROTO, "OXM_OF_IP_PROTO",
306 MFF_IP_DSCP, "nw_tos", NULL,
312 NXM_OF_IP_TOS, "NXM_OF_IP_TOS",
313 OXM_OF_IP_DSCP, "OXM_OF_IP_DSCP",
315 MFF_IP_ECN, "nw_ecn", NULL,
321 NXM_NX_IP_ECN, "NXM_NX_IP_ECN",
322 OXM_OF_IP_ECN, "OXM_OF_IP_ECN",
324 MFF_IP_TTL, "nw_ttl", NULL,
330 NXM_NX_IP_TTL, "NXM_NX_IP_TTL",
331 NXM_NX_IP_TTL, "NXM_NX_IP_TTL",
333 MFF_IP_FRAG, "ip_frag", NULL,
339 NXM_NX_IP_FRAG, "NXM_NX_IP_FRAG",
340 NXM_NX_IP_FRAG, "NXM_NX_IP_FRAG",
344 MFF_ARP_OP, "arp_op", NULL,
345 MF_FIELD_SIZES(be16),
350 NXM_OF_ARP_OP, "NXM_OF_ARP_OP",
351 OXM_OF_ARP_OP, "OXM_OF_ARP_OP",
353 MFF_ARP_SPA, "arp_spa", NULL,
354 MF_FIELD_SIZES(be32),
359 NXM_OF_ARP_SPA, "NXM_OF_ARP_SPA",
360 OXM_OF_ARP_SPA, "OXM_OF_ARP_SPA",
362 MFF_ARP_TPA, "arp_tpa", NULL,
363 MF_FIELD_SIZES(be32),
368 NXM_OF_ARP_TPA, "NXM_OF_ARP_TPA",
369 OXM_OF_ARP_TPA, "OXM_OF_ARP_TPA",
371 MFF_ARP_SHA, "arp_sha", NULL,
377 NXM_NX_ARP_SHA, "NXM_NX_ARP_SHA",
378 OXM_OF_ARP_SHA, "OXM_OF_ARP_SHA",
380 MFF_ARP_THA, "arp_tha", NULL,
386 NXM_NX_ARP_THA, "NXM_NX_ARP_THA",
387 OXM_OF_ARP_THA, "OXM_OF_ARP_THA",
395 MFF_TCP_SRC, "tcp_src", "tp_src",
396 MF_FIELD_SIZES(be16),
401 NXM_OF_TCP_SRC, "NXM_OF_TCP_SRC",
402 OXM_OF_TCP_SRC, "OXM_OF_TCP_SRC",
404 MFF_TCP_DST, "tcp_dst", "tp_dst",
405 MF_FIELD_SIZES(be16),
410 NXM_OF_TCP_DST, "NXM_OF_TCP_DST",
411 OXM_OF_TCP_DST, "OXM_OF_TCP_DST",
415 MFF_UDP_SRC, "udp_src", NULL,
416 MF_FIELD_SIZES(be16),
421 NXM_OF_UDP_SRC, "NXM_OF_UDP_SRC",
422 OXM_OF_UDP_SRC, "OXM_OF_UDP_SRC",
424 MFF_UDP_DST, "udp_dst", NULL,
425 MF_FIELD_SIZES(be16),
430 NXM_OF_UDP_DST, "NXM_OF_UDP_DST",
431 OXM_OF_UDP_DST, "OXM_OF_UDP_DST",
435 MFF_ICMPV4_TYPE, "icmp_type", NULL,
441 NXM_OF_ICMP_TYPE, "NXM_OF_ICMP_TYPE",
442 OXM_OF_ICMPV4_TYPE, "OXM_OF_ICMPV4_TYPE",
444 MFF_ICMPV4_CODE, "icmp_code", NULL,
450 NXM_OF_ICMP_CODE, "NXM_OF_ICMP_CODE",
451 OXM_OF_ICMPV4_CODE, "OXM_OF_ICMPV4_CODE",
455 MFF_ICMPV6_TYPE, "icmpv6_type", NULL,
461 NXM_NX_ICMPV6_TYPE, "NXM_NX_ICMPV6_TYPE",
462 OXM_OF_ICMPV6_TYPE, "OXM_OF_ICMPV6_TYPE",
464 MFF_ICMPV6_CODE, "icmpv6_code", NULL,
470 NXM_NX_ICMPV6_CODE, "NXM_NX_ICMPV6_CODE",
471 OXM_OF_ICMPV6_CODE, "OXM_OF_ICMPV6_CODE",
479 MFF_ND_TARGET, "nd_target", NULL,
480 MF_FIELD_SIZES(ipv6),
485 NXM_NX_ND_TARGET, "NXM_NX_ND_TARGET",
486 OXM_OF_IPV6_ND_TARGET, "OXM_OF_IPV6_ND_TARGET",
488 MFF_ND_SLL, "nd_sll", NULL,
494 NXM_NX_ND_SLL, "NXM_NX_ND_SLL",
495 OXM_OF_IPV6_ND_SLL, "OXM_OF_IPV6_ND_SLL",
497 MFF_ND_TLL, "nd_tll", NULL,
503 NXM_NX_ND_TLL, "NXM_NX_ND_TLL",
504 OXM_OF_IPV6_ND_TLL, "OXM_OF_IPV6_ND_TLL",
508 /* Maps an NXM or OXM header value to an mf_field. */
510 struct hmap_node hmap_node; /* In 'all_fields' hmap. */
511 uint32_t header; /* NXM or OXM header value. */
512 const struct mf_field *mf;
515 /* Contains 'struct nxm_field's. */
516 static struct hmap all_fields = HMAP_INITIALIZER(&all_fields);
518 /* Rate limit for parse errors. These always indicate a bug in an OpenFlow
519 * controller and so there's not much point in showing a lot of them. */
520 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
522 const struct mf_field *mf_from_nxm_header__(uint32_t header);
524 /* Returns the field with the given 'id'. */
525 const struct mf_field *
526 mf_from_id(enum mf_field_id id)
528 assert((unsigned int) id < MFF_N_IDS);
529 return &mf_fields[id];
532 /* Returns the field with the given 'name', or a null pointer if no field has
534 const struct mf_field *
535 mf_from_name(const char *name)
537 static struct shash mf_by_name = SHASH_INITIALIZER(&mf_by_name);
539 if (shash_is_empty(&mf_by_name)) {
540 const struct mf_field *mf;
542 for (mf = mf_fields; mf < &mf_fields[MFF_N_IDS]; mf++) {
543 shash_add_once(&mf_by_name, mf->name, mf);
544 if (mf->extra_name) {
545 shash_add_once(&mf_by_name, mf->extra_name, mf);
550 return shash_find_data(&mf_by_name, name);
554 add_nxm_field(uint32_t header, const struct mf_field *mf)
558 f = xmalloc(sizeof *f);
559 hmap_insert(&all_fields, &f->hmap_node, hash_int(header, 0));
565 nxm_init_add_field(const struct mf_field *mf, uint32_t header)
568 assert(!mf_from_nxm_header__(header));
569 add_nxm_field(header, mf);
570 if (mf->maskable != MFM_NONE) {
571 add_nxm_field(NXM_MAKE_WILD_HEADER(header), mf);
579 const struct mf_field *mf;
581 for (mf = mf_fields; mf < &mf_fields[MFF_N_IDS]; mf++) {
582 nxm_init_add_field(mf, mf->nxm_header);
583 if (mf->oxm_header != mf->nxm_header) {
584 nxm_init_add_field(mf, mf->oxm_header);
589 const struct mf_field *
590 mf_from_nxm_header(uint32_t header)
592 if (hmap_is_empty(&all_fields)) {
595 return mf_from_nxm_header__(header);
598 const struct mf_field *
599 mf_from_nxm_header__(uint32_t header)
601 const struct nxm_field *f;
603 HMAP_FOR_EACH_IN_BUCKET (f, hmap_node, hash_int(header, 0), &all_fields) {
604 if (f->header == header) {
612 /* Returns true if 'wc' wildcards all the bits in field 'mf', false if 'wc'
613 * specifies at least one bit in the field.
615 * The caller is responsible for ensuring that 'wc' corresponds to a flow that
616 * meets 'mf''s prerequisites. */
618 mf_is_all_wild(const struct mf_field *mf, const struct flow_wildcards *wc)
627 return !wc->masks.tunnel.tun_id;
629 return !wc->masks.metadata;
631 return !wc->masks.in_port;
633 return !wc->masks.regs[mf->id - MFF_REG0];
636 return eth_addr_is_zero(wc->masks.dl_src);
638 return eth_addr_is_zero(wc->masks.dl_dst);
640 return !wc->masks.dl_type;
644 return eth_addr_is_zero(wc->masks.arp_sha);
648 return eth_addr_is_zero(wc->masks.arp_tha);
651 return !wc->masks.vlan_tci;
653 return !(wc->masks.vlan_tci & htons(VLAN_VID_MASK));
655 return !(wc->masks.vlan_tci & htons(VLAN_VID_MASK | VLAN_CFI));
656 case MFF_DL_VLAN_PCP:
658 return !(wc->masks.vlan_tci & htons(VLAN_PCP_MASK));
661 return !wc->masks.nw_src;
663 return !wc->masks.nw_dst;
666 return ipv6_mask_is_any(&wc->masks.ipv6_src);
668 return ipv6_mask_is_any(&wc->masks.ipv6_dst);
671 return !wc->masks.ipv6_label;
674 return !wc->masks.nw_proto;
676 return !(wc->masks.nw_tos & IP_DSCP_MASK);
678 return !(wc->masks.nw_tos & IP_ECN_MASK);
680 return !wc->masks.nw_ttl;
683 return ipv6_mask_is_any(&wc->masks.nd_target);
686 return !(wc->masks.nw_frag & FLOW_NW_FRAG_MASK);
689 return !wc->masks.nw_proto;
691 return !wc->masks.nw_src;
693 return !wc->masks.nw_dst;
697 case MFF_ICMPV4_TYPE:
698 case MFF_ICMPV6_TYPE:
699 return !wc->masks.tp_src;
702 case MFF_ICMPV4_CODE:
703 case MFF_ICMPV6_CODE:
704 return !wc->masks.tp_dst;
712 /* Initializes 'mask' with the wildcard bit pattern for field 'mf' within 'wc'.
713 * Each bit in 'mask' will be set to 1 if the bit is significant for matching
714 * purposes, or to 0 if it is wildcarded.
716 * The caller is responsible for ensuring that 'wc' corresponds to a flow that
717 * meets 'mf''s prerequisites. */
719 mf_get_mask(const struct mf_field *mf, const struct flow_wildcards *wc,
720 union mf_value *mask)
729 mask->be64 = wc->masks.tunnel.tun_id;
732 mask->be64 = wc->masks.metadata;
735 mask->be16 = htons(wc->masks.in_port);
738 mask->be32 = htonl(wc->masks.regs[mf->id - MFF_REG0]);
742 memcpy(mask->mac, wc->masks.dl_dst, ETH_ADDR_LEN);
745 memcpy(mask->mac, wc->masks.dl_src, ETH_ADDR_LEN);
748 mask->be16 = wc->masks.dl_type;
752 mask->be16 = wc->masks.vlan_tci;
755 mask->be16 = wc->masks.vlan_tci & htons(VLAN_VID_MASK);
758 mask->be16 = wc->masks.vlan_tci & htons(VLAN_VID_MASK | VLAN_CFI);
760 case MFF_DL_VLAN_PCP:
762 mask->u8 = vlan_tci_to_pcp(wc->masks.vlan_tci);
766 mask->be32 = wc->masks.nw_src;
769 mask->be32 = wc->masks.nw_dst;
773 mask->ipv6 = wc->masks.ipv6_src;
776 mask->ipv6 = wc->masks.ipv6_dst;
779 mask->be32 = wc->masks.ipv6_label;
783 mask->u8 = wc->masks.nw_proto;
786 mask->u8 = wc->masks.nw_tos & IP_DSCP_MASK;
789 mask->u8 = wc->masks.nw_tos & IP_ECN_MASK;
793 mask->ipv6 = wc->masks.nd_target;
797 mask->u8 = wc->masks.nw_ttl;
800 mask->u8 = wc->masks.nw_frag & FLOW_NW_FRAG_MASK;
804 mask->u8 = wc->masks.nw_proto;
807 mask->be32 = wc->masks.nw_src;
810 mask->be32 = wc->masks.nw_dst;
814 memcpy(mask->mac, wc->masks.arp_sha, ETH_ADDR_LEN);
818 memcpy(mask->mac, wc->masks.arp_tha, ETH_ADDR_LEN);
823 mask->be16 = wc->masks.tp_src;
827 mask->be16 = wc->masks.tp_dst;
830 case MFF_ICMPV4_TYPE:
831 case MFF_ICMPV6_TYPE:
832 mask->u8 = ntohs(wc->masks.tp_src);
834 case MFF_ICMPV4_CODE:
835 case MFF_ICMPV6_CODE:
836 mask->u8 = ntohs(wc->masks.tp_dst);
845 /* Tests whether 'mask' is a valid wildcard bit pattern for 'mf'. Returns true
846 * if the mask is valid, false otherwise. */
848 mf_is_mask_valid(const struct mf_field *mf, const union mf_value *mask)
850 switch (mf->maskable) {
852 return (is_all_zeros((const uint8_t *) mask, mf->n_bytes) ||
853 is_all_ones((const uint8_t *) mask, mf->n_bytes));
863 is_ip_any(const struct flow *flow)
865 return (flow->dl_type == htons(ETH_TYPE_IP) ||
866 flow->dl_type == htons(ETH_TYPE_IPV6));
870 is_icmpv4(const struct flow *flow)
872 return (flow->dl_type == htons(ETH_TYPE_IP)
873 && flow->nw_proto == IPPROTO_ICMP);
877 is_icmpv6(const struct flow *flow)
879 return (flow->dl_type == htons(ETH_TYPE_IPV6)
880 && flow->nw_proto == IPPROTO_ICMPV6);
883 /* Returns true if 'flow' meets the prerequisites for 'mf', false otherwise. */
885 mf_are_prereqs_ok(const struct mf_field *mf, const struct flow *flow)
887 switch (mf->prereqs) {
892 return (flow->dl_type == htons(ETH_TYPE_ARP) ||
893 flow->dl_type == htons(ETH_TYPE_RARP));
895 return flow->dl_type == htons(ETH_TYPE_IP);
897 return flow->dl_type == htons(ETH_TYPE_IPV6);
899 return (flow->vlan_tci & htons(VLAN_CFI)) != 0;
901 return is_ip_any(flow);
904 return is_ip_any(flow) && flow->nw_proto == IPPROTO_TCP;
906 return is_ip_any(flow) && flow->nw_proto == IPPROTO_UDP;
908 return is_icmpv4(flow);
910 return is_icmpv6(flow);
913 return (is_icmpv6(flow)
914 && flow->tp_dst == htons(0)
915 && (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT) ||
916 flow->tp_src == htons(ND_NEIGHBOR_ADVERT)));
918 return (is_icmpv6(flow)
919 && flow->tp_dst == htons(0)
920 && (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT)));
922 return (is_icmpv6(flow)
923 && flow->tp_dst == htons(0)
924 && (flow->tp_src == htons(ND_NEIGHBOR_ADVERT)));
930 /* Returns true if 'value' may be a valid value *as part of a masked match*,
933 * A value is not rejected just because it is not valid for the field in
934 * question, but only if it doesn't make sense to test the bits in question at
935 * all. For example, the MFF_VLAN_TCI field will never have a nonzero value
936 * without the VLAN_CFI bit being set, but we can't reject those values because
937 * it is still legitimate to test just for those bits (see the documentation
938 * for NXM_OF_VLAN_TCI in nicira-ext.h). On the other hand, there is never a
939 * reason to set the low bit of MFF_IP_DSCP to 1, so we reject that. */
941 mf_is_value_valid(const struct mf_field *mf, const union mf_value *value)
971 case MFF_ICMPV4_TYPE:
972 case MFF_ICMPV4_CODE:
973 case MFF_ICMPV6_TYPE:
974 case MFF_ICMPV6_CODE:
981 return !(value->u8 & ~IP_DSCP_MASK);
983 return !(value->u8 & ~IP_ECN_MASK);
985 return !(value->u8 & ~FLOW_NW_FRAG_MASK);
988 return !(value->be16 & htons(0xff00));
991 return !(value->be16 & htons(VLAN_CFI | VLAN_PCP_MASK));
993 return !(value->be16 & htons(VLAN_PCP_MASK));
995 case MFF_DL_VLAN_PCP:
997 return !(value->u8 & ~(VLAN_PCP_MASK >> VLAN_PCP_SHIFT));
1000 return !(value->be32 & ~htonl(IPV6_LABEL_MASK));
1008 /* Copies the value of field 'mf' from 'flow' into 'value'. The caller is
1009 * responsible for ensuring that 'flow' meets 'mf''s prerequisites. */
1011 mf_get_value(const struct mf_field *mf, const struct flow *flow,
1012 union mf_value *value)
1016 value->be64 = flow->tunnel.tun_id;
1019 value->be32 = flow->tunnel.ip_src;
1022 value->be32 = flow->tunnel.ip_dst;
1025 value->be16 = htons(flow->tunnel.flags);
1028 value->u8 = flow->tunnel.ip_ttl;
1031 value->u8 = flow->tunnel.ip_tos;
1035 value->be64 = flow->metadata;
1039 value->be16 = htons(flow->in_port);
1043 value->be32 = htonl(flow->regs[mf->id - MFF_REG0]);
1047 memcpy(value->mac, flow->dl_src, ETH_ADDR_LEN);
1051 memcpy(value->mac, flow->dl_dst, ETH_ADDR_LEN);
1055 value->be16 = flow->dl_type;
1059 value->be16 = flow->vlan_tci;
1063 value->be16 = flow->vlan_tci & htons(VLAN_VID_MASK);
1066 value->be16 = flow->vlan_tci & htons(VLAN_VID_MASK | VLAN_CFI);
1069 case MFF_DL_VLAN_PCP:
1071 value->u8 = vlan_tci_to_pcp(flow->vlan_tci);
1075 value->be32 = flow->nw_src;
1079 value->be32 = flow->nw_dst;
1083 value->ipv6 = flow->ipv6_src;
1087 value->ipv6 = flow->ipv6_dst;
1090 case MFF_IPV6_LABEL:
1091 value->be32 = flow->ipv6_label;
1095 value->u8 = flow->nw_proto;
1099 value->u8 = flow->nw_tos & IP_DSCP_MASK;
1103 value->u8 = flow->nw_tos & IP_ECN_MASK;
1107 value->u8 = flow->nw_ttl;
1111 value->u8 = flow->nw_frag;
1115 value->be16 = htons(flow->nw_proto);
1119 value->be32 = flow->nw_src;
1123 value->be32 = flow->nw_dst;
1128 memcpy(value->mac, flow->arp_sha, ETH_ADDR_LEN);
1133 memcpy(value->mac, flow->arp_tha, ETH_ADDR_LEN);
1138 value->be16 = flow->tp_src;
1143 value->be16 = flow->tp_dst;
1146 case MFF_ICMPV4_TYPE:
1147 case MFF_ICMPV6_TYPE:
1148 value->u8 = ntohs(flow->tp_src);
1151 case MFF_ICMPV4_CODE:
1152 case MFF_ICMPV6_CODE:
1153 value->u8 = ntohs(flow->tp_dst);
1157 value->ipv6 = flow->nd_target;
1166 /* Makes 'match' match field 'mf' exactly, with the value matched taken from
1167 * 'value'. The caller is responsible for ensuring that 'match' meets 'mf''s
1170 mf_set_value(const struct mf_field *mf,
1171 const union mf_value *value, struct match *match)
1175 match_set_tun_id(match, value->be64);
1178 match_set_tun_src(match, value->be32);
1181 match_set_tun_dst(match, value->be32);
1184 match_set_tun_flags(match, ntohs(value->be16));
1187 match_set_tun_tos(match, value->u8);
1190 match_set_tun_ttl(match, value->u8);
1194 match_set_metadata(match, value->be64);
1198 match_set_in_port(match, ntohs(value->be16));
1202 match_set_reg(match, mf->id - MFF_REG0, ntohl(value->be32));
1206 match_set_dl_src(match, value->mac);
1210 match_set_dl_dst(match, value->mac);
1214 match_set_dl_type(match, value->be16);
1218 match_set_dl_tci(match, value->be16);
1222 match_set_dl_vlan(match, value->be16);
1225 match_set_vlan_vid(match, value->be16);
1228 case MFF_DL_VLAN_PCP:
1230 match_set_dl_vlan_pcp(match, value->u8);
1234 match_set_nw_src(match, value->be32);
1238 match_set_nw_dst(match, value->be32);
1242 match_set_ipv6_src(match, &value->ipv6);
1246 match_set_ipv6_dst(match, &value->ipv6);
1249 case MFF_IPV6_LABEL:
1250 match_set_ipv6_label(match, value->be32);
1254 match_set_nw_proto(match, value->u8);
1258 match_set_nw_dscp(match, value->u8);
1262 match_set_nw_ecn(match, value->u8);
1266 match_set_nw_ttl(match, value->u8);
1270 match_set_nw_frag(match, value->u8);
1274 match_set_nw_proto(match, ntohs(value->be16));
1278 match_set_nw_src(match, value->be32);
1282 match_set_nw_dst(match, value->be32);
1287 match_set_arp_sha(match, value->mac);
1292 match_set_arp_tha(match, value->mac);
1297 match_set_tp_src(match, value->be16);
1302 match_set_tp_dst(match, value->be16);
1305 case MFF_ICMPV4_TYPE:
1306 case MFF_ICMPV6_TYPE:
1307 match_set_icmp_type(match, value->u8);
1310 case MFF_ICMPV4_CODE:
1311 case MFF_ICMPV6_CODE:
1312 match_set_icmp_code(match, value->u8);
1316 match_set_nd_target(match, &value->ipv6);
1325 /* Makes 'match' match field 'mf' exactly, with the value matched taken from
1326 * 'value'. The caller is responsible for ensuring that 'match' meets 'mf''s
1329 mf_set_flow_value(const struct mf_field *mf,
1330 const union mf_value *value, struct flow *flow)
1334 flow->tunnel.tun_id = value->be64;
1337 flow->tunnel.ip_src = value->be32;
1340 flow->tunnel.ip_dst = value->be32;
1343 flow->tunnel.flags = ntohs(value->be16);
1346 flow->tunnel.ip_tos = value->u8;
1349 flow->tunnel.ip_ttl = value->u8;
1353 flow->metadata = value->be64;
1357 flow->in_port = ntohs(value->be16);
1361 flow->regs[mf->id - MFF_REG0] = ntohl(value->be32);
1365 memcpy(flow->dl_src, value->mac, ETH_ADDR_LEN);
1369 memcpy(flow->dl_dst, value->mac, ETH_ADDR_LEN);
1373 flow->dl_type = value->be16;
1377 flow->vlan_tci = value->be16;
1381 flow_set_dl_vlan(flow, value->be16);
1384 flow_set_vlan_vid(flow, value->be16);
1387 case MFF_DL_VLAN_PCP:
1389 flow_set_vlan_pcp(flow, value->u8);
1393 flow->nw_src = value->be32;
1397 flow->nw_dst = value->be32;
1401 flow->ipv6_src = value->ipv6;
1405 flow->ipv6_dst = value->ipv6;
1408 case MFF_IPV6_LABEL:
1409 flow->ipv6_label = value->be32 & ~htonl(IPV6_LABEL_MASK);
1413 flow->nw_proto = value->u8;
1417 flow->nw_tos &= ~IP_DSCP_MASK;
1418 flow->nw_tos |= value->u8 & IP_DSCP_MASK;
1422 flow->nw_tos &= ~IP_ECN_MASK;
1423 flow->nw_tos |= value->u8 & IP_ECN_MASK;
1427 flow->nw_ttl = value->u8;
1431 flow->nw_frag &= value->u8;
1435 flow->nw_proto = ntohs(value->be16);
1439 flow->nw_src = value->be32;
1443 flow->nw_dst = value->be32;
1448 memcpy(flow->arp_sha, value->mac, ETH_ADDR_LEN);
1453 memcpy(flow->arp_tha, value->mac, ETH_ADDR_LEN);
1458 flow->tp_src = value->be16;
1463 flow->tp_dst = value->be16;
1466 case MFF_ICMPV4_TYPE:
1467 case MFF_ICMPV6_TYPE:
1468 flow->tp_src = htons(value->u8);
1471 case MFF_ICMPV4_CODE:
1472 case MFF_ICMPV6_CODE:
1473 flow->tp_dst = htons(value->u8);
1477 flow->nd_target = value->ipv6;
1486 /* Returns true if 'mf' has a zero value in 'flow', false if it is nonzero.
1488 * The caller is responsible for ensuring that 'flow' meets 'mf''s
1491 mf_is_zero(const struct mf_field *mf, const struct flow *flow)
1493 union mf_value value;
1495 mf_get_value(mf, flow, &value);
1496 return is_all_zeros((const uint8_t *) &value, mf->n_bytes);
1499 /* Makes 'match' wildcard field 'mf'.
1501 * The caller is responsible for ensuring that 'match' meets 'mf''s
1504 mf_set_wild(const struct mf_field *mf, struct match *match)
1508 match_set_tun_id_masked(match, htonll(0), htonll(0));
1511 match_set_tun_src_masked(match, htonl(0), htonl(0));
1514 match_set_tun_dst_masked(match, htonl(0), htonl(0));
1517 match_set_tun_flags_masked(match, 0, 0);
1520 match_set_tun_tos_masked(match, 0, 0);
1523 match_set_tun_ttl_masked(match, 0, 0);
1527 match_set_metadata_masked(match, htonll(0), htonll(0));
1530 match->flow.in_port = 0;
1531 match->wc.masks.in_port = 0;
1535 match_set_reg_masked(match, mf->id - MFF_REG0, 0, 0);
1539 memset(match->flow.dl_src, 0, ETH_ADDR_LEN);
1540 memset(match->wc.masks.dl_src, 0, ETH_ADDR_LEN);
1544 memset(match->flow.dl_dst, 0, ETH_ADDR_LEN);
1545 memset(match->wc.masks.dl_dst, 0, ETH_ADDR_LEN);
1549 match->flow.dl_type = htons(0);
1550 match->wc.masks.dl_type = htons(0);
1554 match_set_dl_tci_masked(match, htons(0), htons(0));
1559 match_set_any_vid(match);
1562 case MFF_DL_VLAN_PCP:
1564 match_set_any_pcp(match);
1569 match_set_nw_src_masked(match, htonl(0), htonl(0));
1574 match_set_nw_dst_masked(match, htonl(0), htonl(0));
1578 memset(&match->wc.masks.ipv6_src, 0, sizeof match->wc.masks.ipv6_src);
1579 memset(&match->flow.ipv6_src, 0, sizeof match->flow.ipv6_src);
1583 memset(&match->wc.masks.ipv6_dst, 0, sizeof match->wc.masks.ipv6_dst);
1584 memset(&match->flow.ipv6_dst, 0, sizeof match->flow.ipv6_dst);
1587 case MFF_IPV6_LABEL:
1588 match->wc.masks.ipv6_label = htonl(0);
1589 match->flow.ipv6_label = htonl(0);
1593 match->wc.masks.nw_proto = 0;
1594 match->flow.nw_proto = 0;
1598 match->wc.masks.nw_tos &= ~IP_DSCP_MASK;
1599 match->flow.nw_tos &= ~IP_DSCP_MASK;
1603 match->wc.masks.nw_tos &= ~IP_ECN_MASK;
1604 match->flow.nw_tos &= ~IP_ECN_MASK;
1608 match->wc.masks.nw_ttl = 0;
1609 match->flow.nw_ttl = 0;
1613 match->wc.masks.nw_frag |= FLOW_NW_FRAG_MASK;
1614 match->flow.nw_frag &= ~FLOW_NW_FRAG_MASK;
1618 match->wc.masks.nw_proto = 0;
1619 match->flow.nw_proto = 0;
1624 memset(match->flow.arp_sha, 0, ETH_ADDR_LEN);
1625 memset(match->wc.masks.arp_sha, 0, ETH_ADDR_LEN);
1630 memset(match->flow.arp_tha, 0, ETH_ADDR_LEN);
1631 memset(match->wc.masks.arp_tha, 0, ETH_ADDR_LEN);
1636 case MFF_ICMPV4_TYPE:
1637 case MFF_ICMPV6_TYPE:
1638 match->wc.masks.tp_src = htons(0);
1639 match->flow.tp_src = htons(0);
1644 case MFF_ICMPV4_CODE:
1645 case MFF_ICMPV6_CODE:
1646 match->wc.masks.tp_dst = htons(0);
1647 match->flow.tp_dst = htons(0);
1651 memset(&match->wc.masks.nd_target, 0,
1652 sizeof match->wc.masks.nd_target);
1653 memset(&match->flow.nd_target, 0, sizeof match->flow.nd_target);
1662 /* Makes 'match' match field 'mf' with the specified 'value' and 'mask'.
1663 * 'value' specifies a value to match and 'mask' specifies a wildcard pattern,
1664 * with a 1-bit indicating that the corresponding value bit must match and a
1665 * 0-bit indicating a don't-care.
1667 * If 'mask' is NULL or points to all-1-bits, then this call is equivalent to
1668 * mf_set_value(mf, value, match). If 'mask' points to all-0-bits, then this
1669 * call is equivalent to mf_set_wild(mf, match).
1671 * 'mask' must be a valid mask for 'mf' (see mf_is_mask_valid()). The caller
1672 * is responsible for ensuring that 'match' meets 'mf''s prerequisites. */
1674 mf_set(const struct mf_field *mf,
1675 const union mf_value *value, const union mf_value *mask,
1676 struct match *match)
1678 if (!mask || is_all_ones((const uint8_t *) mask, mf->n_bytes)) {
1679 mf_set_value(mf, value, match);
1681 } else if (is_all_zeros((const uint8_t *) mask, mf->n_bytes)) {
1682 mf_set_wild(mf, match);
1690 case MFF_DL_VLAN_PCP:
1697 case MFF_ICMPV4_TYPE:
1698 case MFF_ICMPV4_CODE:
1699 case MFF_ICMPV6_TYPE:
1700 case MFF_ICMPV6_CODE:
1704 match_set_tun_id_masked(match, value->be64, mask->be64);
1707 match_set_tun_src_masked(match, value->be32, mask->be32);
1710 match_set_tun_dst_masked(match, value->be32, mask->be32);
1713 match_set_tun_flags_masked(match, ntohs(value->be16), ntohs(mask->be16));
1716 match_set_tun_ttl_masked(match, value->u8, mask->u8);
1719 match_set_tun_tos_masked(match, value->u8, mask->u8);
1723 match_set_metadata_masked(match, value->be64, mask->be64);
1727 match_set_reg_masked(match, mf->id - MFF_REG0,
1728 ntohl(value->be32), ntohl(mask->be32));
1732 match_set_dl_dst_masked(match, value->mac, mask->mac);
1736 match_set_dl_src_masked(match, value->mac, mask->mac);
1741 match_set_arp_sha_masked(match, value->mac, mask->mac);
1746 match_set_arp_tha_masked(match, value->mac, mask->mac);
1750 match_set_dl_tci_masked(match, value->be16, mask->be16);
1754 match_set_vlan_vid_masked(match, value->be16, mask->be16);
1758 match_set_nw_src_masked(match, value->be32, mask->be32);
1762 match_set_nw_dst_masked(match, value->be32, mask->be32);
1766 match_set_ipv6_src_masked(match, &value->ipv6, &mask->ipv6);
1770 match_set_ipv6_dst_masked(match, &value->ipv6, &mask->ipv6);
1773 case MFF_IPV6_LABEL:
1774 if ((mask->be32 & htonl(IPV6_LABEL_MASK)) == htonl(IPV6_LABEL_MASK)) {
1775 mf_set_value(mf, value, match);
1777 match_set_ipv6_label_masked(match, value->be32, mask->be32);
1782 match_set_nd_target_masked(match, &value->ipv6, &mask->ipv6);
1786 match_set_nw_frag_masked(match, value->u8, mask->u8);
1790 match_set_nw_src_masked(match, value->be32, mask->be32);
1794 match_set_nw_dst_masked(match, value->be32, mask->be32);
1799 match_set_tp_src_masked(match, value->be16, mask->be16);
1804 match_set_tp_dst_masked(match, value->be16, mask->be16);
1814 mf_check__(const struct mf_subfield *sf, const struct flow *flow,
1818 VLOG_WARN_RL(&rl, "unknown %s field", type);
1819 } else if (!sf->n_bits) {
1820 VLOG_WARN_RL(&rl, "zero bit %s field %s", type, sf->field->name);
1821 } else if (sf->ofs >= sf->field->n_bits) {
1822 VLOG_WARN_RL(&rl, "bit offset %d exceeds %d-bit width of %s field %s",
1823 sf->ofs, sf->field->n_bits, type, sf->field->name);
1824 } else if (sf->ofs + sf->n_bits > sf->field->n_bits) {
1825 VLOG_WARN_RL(&rl, "bit offset %d and width %d exceeds %d-bit width "
1826 "of %s field %s", sf->ofs, sf->n_bits,
1827 sf->field->n_bits, type, sf->field->name);
1828 } else if (flow && !mf_are_prereqs_ok(sf->field, flow)) {
1829 VLOG_WARN_RL(&rl, "%s field %s lacks correct prerequisites",
1830 type, sf->field->name);
1835 return OFPERR_OFPBAC_BAD_ARGUMENT;
1838 /* Checks whether 'sf' is valid for reading a subfield out of 'flow'. Returns
1839 * 0 if so, otherwise an OpenFlow error code (e.g. as returned by
1842 mf_check_src(const struct mf_subfield *sf, const struct flow *flow)
1844 return mf_check__(sf, flow, "source");
1847 /* Checks whether 'sf' is valid for writing a subfield into 'flow'. Returns 0
1848 * if so, otherwise an OpenFlow error code (e.g. as returned by
1851 mf_check_dst(const struct mf_subfield *sf, const struct flow *flow)
1853 int error = mf_check__(sf, flow, "destination");
1854 if (!error && !sf->field->writable) {
1855 VLOG_WARN_RL(&rl, "destination field %s is not writable",
1857 return OFPERR_OFPBAC_BAD_ARGUMENT;
1862 /* Copies the value and wildcard bit pattern for 'mf' from 'match' into the
1863 * 'value' and 'mask', respectively. */
1865 mf_get(const struct mf_field *mf, const struct match *match,
1866 union mf_value *value, union mf_value *mask)
1868 mf_get_value(mf, &match->flow, value);
1869 mf_get_mask(mf, &match->wc, mask);
1872 /* Assigns a random value for field 'mf' to 'value'. */
1874 mf_random_value(const struct mf_field *mf, union mf_value *value)
1876 random_bytes(value, mf->n_bytes);
1906 case MFF_ICMPV4_TYPE:
1907 case MFF_ICMPV4_CODE:
1908 case MFF_ICMPV6_TYPE:
1909 case MFF_ICMPV6_CODE:
1915 case MFF_IPV6_LABEL:
1916 value->be32 &= ~htonl(IPV6_LABEL_MASK);
1920 value->u8 &= IP_DSCP_MASK;
1924 value->u8 &= IP_ECN_MASK;
1928 value->u8 &= FLOW_NW_FRAG_MASK;
1932 value->be16 &= htons(0xff);
1936 value->be16 &= htons(VLAN_VID_MASK);
1939 value->be16 &= htons(VLAN_VID_MASK | VLAN_CFI);
1942 case MFF_DL_VLAN_PCP:
1954 mf_from_integer_string(const struct mf_field *mf, const char *s,
1955 uint8_t *valuep, uint8_t *maskp)
1957 unsigned long long int integer, mask;
1962 integer = strtoull(s, &tail, 0);
1963 if (errno || (*tail != '\0' && *tail != '/')) {
1968 mask = strtoull(tail + 1, &tail, 0);
1969 if (errno || *tail != '\0') {
1976 for (i = mf->n_bytes - 1; i >= 0; i--) {
1977 valuep[i] = integer;
1983 return xasprintf("%s: value too large for %u-byte field %s",
1984 s, mf->n_bytes, mf->name);
1989 return xasprintf("%s: bad syntax for %s", s, mf->name);
1993 mf_from_ethernet_string(const struct mf_field *mf, const char *s,
1994 uint8_t mac[ETH_ADDR_LEN],
1995 uint8_t mask[ETH_ADDR_LEN])
1997 assert(mf->n_bytes == ETH_ADDR_LEN);
1999 switch (sscanf(s, ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT,
2000 ETH_ADDR_SCAN_ARGS(mac), ETH_ADDR_SCAN_ARGS(mask))){
2001 case ETH_ADDR_SCAN_COUNT * 2:
2004 case ETH_ADDR_SCAN_COUNT:
2005 memset(mask, 0xff, ETH_ADDR_LEN);
2009 return xasprintf("%s: invalid Ethernet address", s);
2014 mf_from_ipv4_string(const struct mf_field *mf, const char *s,
2015 ovs_be32 *ip, ovs_be32 *mask)
2019 assert(mf->n_bytes == sizeof *ip);
2021 if (sscanf(s, IP_SCAN_FMT"/"IP_SCAN_FMT,
2022 IP_SCAN_ARGS(ip), IP_SCAN_ARGS(mask)) == IP_SCAN_COUNT * 2) {
2024 } else if (sscanf(s, IP_SCAN_FMT"/%d",
2025 IP_SCAN_ARGS(ip), &prefix) == IP_SCAN_COUNT + 1) {
2026 if (prefix <= 0 || prefix > 32) {
2027 return xasprintf("%s: network prefix bits not between 1 and "
2029 } else if (prefix == 32) {
2030 *mask = htonl(UINT32_MAX);
2032 *mask = htonl(((1u << prefix) - 1) << (32 - prefix));
2034 } else if (sscanf(s, IP_SCAN_FMT, IP_SCAN_ARGS(ip)) == IP_SCAN_COUNT) {
2035 *mask = htonl(UINT32_MAX);
2037 return xasprintf("%s: invalid IP address", s);
2043 mf_from_ipv6_string(const struct mf_field *mf, const char *s,
2044 struct in6_addr *value, struct in6_addr *mask)
2046 char *str = xstrdup(s);
2047 char *save_ptr = NULL;
2048 const char *name, *netmask;
2051 assert(mf->n_bytes == sizeof *value);
2053 name = strtok_r(str, "/", &save_ptr);
2054 retval = name ? lookup_ipv6(name, value) : EINVAL;
2058 err = xasprintf("%s: could not convert to IPv6 address", str);
2064 netmask = strtok_r(NULL, "/", &save_ptr);
2066 if (inet_pton(AF_INET6, netmask, mask) != 1) {
2067 int prefix = atoi(netmask);
2068 if (prefix <= 0 || prefix > 128) {
2070 return xasprintf("%s: prefix bits not between 1 and 128", s);
2072 *mask = ipv6_create_mask(prefix);
2076 *mask = in6addr_exact;
2084 mf_from_ofp_port_string(const struct mf_field *mf, const char *s,
2085 ovs_be16 *valuep, ovs_be16 *maskp)
2089 assert(mf->n_bytes == sizeof(ovs_be16));
2090 if (ofputil_port_from_string(s, &port)) {
2091 *valuep = htons(port);
2092 *maskp = htons(UINT16_MAX);
2095 return mf_from_integer_string(mf, s,
2096 (uint8_t *) valuep, (uint8_t *) maskp);
2100 struct frag_handling {
2106 static const struct frag_handling all_frags[] = {
2107 #define A FLOW_NW_FRAG_ANY
2108 #define L FLOW_NW_FRAG_LATER
2109 /* name mask value */
2112 { "first", A|L, A },
2113 { "later", A|L, A|L },
2118 { "not_later", L, 0 },
2125 mf_from_frag_string(const char *s, uint8_t *valuep, uint8_t *maskp)
2127 const struct frag_handling *h;
2129 for (h = all_frags; h < &all_frags[ARRAY_SIZE(all_frags)]; h++) {
2130 if (!strcasecmp(s, h->name)) {
2131 /* We force the upper bits of the mask on to make mf_parse_value()
2132 * happy (otherwise it will never think it's an exact match.) */
2133 *maskp = h->mask | ~FLOW_NW_FRAG_MASK;
2139 return xasprintf("%s: unknown fragment type (valid types are \"no\", "
2140 "\"yes\", \"first\", \"later\", \"not_first\"", s);
2144 parse_flow_tun_flags(const char *s_, const char *(*bit_to_string)(uint32_t),
2147 uint32_t result = 0;
2148 char *save_ptr = NULL;
2151 char *s = xstrdup(s_);
2153 for (name = strtok_r((char *)s, " |", &save_ptr); name;
2154 name = strtok_r(NULL, " |", &save_ptr)) {
2156 unsigned long long int flags;
2160 if (sscanf(name, "%lli%n", &flags, &n0) > 0 && n0 > 0) {
2164 name_len = strlen(name);
2165 for (bit = 1; bit; bit <<= 1) {
2166 const char *fname = bit_to_string(bit);
2173 len = strlen(fname);
2174 if (len != name_len) {
2177 if (!strncmp(name, fname, len)) {
2189 *res = htons(result);
2196 mf_from_tun_flags_string(const char *s, ovs_be16 *valuep)
2198 if (!parse_flow_tun_flags(s, flow_tun_flag_to_string, valuep)) {
2202 return xasprintf("%s: unknown tunnel flags (valid flags are \"df\", "
2203 "\"csum\", \"key\"", s);
2206 /* Parses 's', a string value for field 'mf', into 'value' and 'mask'. Returns
2207 * NULL if successful, otherwise a malloc()'d string describing the error. */
2209 mf_parse(const struct mf_field *mf, const char *s,
2210 union mf_value *value, union mf_value *mask)
2212 if (!strcasecmp(s, "any") || !strcmp(s, "*")) {
2213 memset(value, 0, mf->n_bytes);
2214 memset(mask, 0, mf->n_bytes);
2218 switch (mf->string) {
2220 case MFS_HEXADECIMAL:
2221 return mf_from_integer_string(mf, s,
2222 (uint8_t *) value, (uint8_t *) mask);
2225 return mf_from_ethernet_string(mf, s, value->mac, mask->mac);
2228 return mf_from_ipv4_string(mf, s, &value->be32, &mask->be32);
2231 return mf_from_ipv6_string(mf, s, &value->ipv6, &mask->ipv6);
2234 return mf_from_ofp_port_string(mf, s, &value->be16, &mask->be16);
2237 return mf_from_frag_string(s, &value->u8, &mask->u8);
2240 return mf_from_tun_flags_string(s, &value->be16);
2245 /* Parses 's', a string value for field 'mf', into 'value'. Returns NULL if
2246 * successful, otherwise a malloc()'d string describing the error. */
2248 mf_parse_value(const struct mf_field *mf, const char *s, union mf_value *value)
2250 union mf_value mask;
2253 error = mf_parse(mf, s, value, &mask);
2258 if (!is_all_ones((const uint8_t *) &mask, mf->n_bytes)) {
2259 return xasprintf("%s: wildcards not allowed here", s);
2265 mf_format_integer_string(const struct mf_field *mf, const uint8_t *valuep,
2266 const uint8_t *maskp, struct ds *s)
2268 unsigned long long int integer;
2271 assert(mf->n_bytes <= 8);
2274 for (i = 0; i < mf->n_bytes; i++) {
2275 integer = (integer << 8) | valuep[i];
2277 if (mf->string == MFS_HEXADECIMAL) {
2278 ds_put_format(s, "%#llx", integer);
2280 ds_put_format(s, "%lld", integer);
2284 unsigned long long int mask;
2287 for (i = 0; i < mf->n_bytes; i++) {
2288 mask = (mask << 8) | maskp[i];
2291 /* I guess we could write the mask in decimal for MFS_DECIMAL but I'm
2292 * not sure that that a bit-mask written in decimal is ever easier to
2293 * understand than the same bit-mask written in hexadecimal. */
2294 ds_put_format(s, "/%#llx", mask);
2299 mf_format_frag_string(const uint8_t *valuep, const uint8_t *maskp,
2302 const struct frag_handling *h;
2303 uint8_t value = *valuep;
2304 uint8_t mask = *maskp;
2307 mask &= FLOW_NW_FRAG_MASK;
2309 for (h = all_frags; h < &all_frags[ARRAY_SIZE(all_frags)]; h++) {
2310 if (value == h->value && mask == h->mask) {
2311 ds_put_cstr(s, h->name);
2315 ds_put_cstr(s, "<error>");
2319 mf_format_tnl_flags_string(const ovs_be16 *valuep, struct ds *s)
2321 format_flags(s, flow_tun_flag_to_string, ntohs(*valuep), '|');
2324 /* Appends to 's' a string representation of field 'mf' whose value is in
2325 * 'value' and 'mask'. 'mask' may be NULL to indicate an exact match. */
2327 mf_format(const struct mf_field *mf,
2328 const union mf_value *value, const union mf_value *mask,
2332 if (is_all_zeros((const uint8_t *) mask, mf->n_bytes)) {
2333 ds_put_cstr(s, "ANY");
2335 } else if (is_all_ones((const uint8_t *) mask, mf->n_bytes)) {
2340 switch (mf->string) {
2343 ofputil_format_port(ntohs(value->be16), s);
2348 case MFS_HEXADECIMAL:
2349 mf_format_integer_string(mf, (uint8_t *) value, (uint8_t *) mask, s);
2353 eth_format_masked(value->mac, mask->mac, s);
2357 ip_format_masked(value->be32, mask ? mask->be32 : htonl(UINT32_MAX),
2362 print_ipv6_masked(s, &value->ipv6, mask ? &mask->ipv6 : NULL);
2366 mf_format_frag_string(&value->u8, &mask->u8, s);
2370 mf_format_tnl_flags_string(&value->be16, s);
2378 /* Makes subfield 'sf' within 'flow' exactly match the 'sf->n_bits'
2379 * least-significant bits in 'x'.
2382 mf_write_subfield_flow(const struct mf_subfield *sf,
2383 const union mf_subvalue *x, struct flow *flow)
2385 const struct mf_field *field = sf->field;
2386 union mf_value value;
2388 mf_get_value(field, flow, &value);
2389 bitwise_copy(x, sizeof *x, 0, &value, field->n_bytes,
2390 sf->ofs, sf->n_bits);
2391 mf_set_flow_value(field, &value, flow);
2394 /* Makes subfield 'sf' within 'match' exactly match the 'sf->n_bits'
2395 * least-significant bits in 'x'.
2398 mf_write_subfield(const struct mf_subfield *sf, const union mf_subvalue *x,
2399 struct match *match)
2401 const struct mf_field *field = sf->field;
2402 union mf_value value, mask;
2404 mf_get(field, match, &value, &mask);
2405 bitwise_copy(x, sizeof *x, 0, &value, field->n_bytes, sf->ofs, sf->n_bits);
2406 bitwise_one ( &mask, field->n_bytes, sf->ofs, sf->n_bits);
2407 mf_set(field, &value, &mask, match);
2410 /* Initializes 'x' to the value of 'sf' within 'flow'. 'sf' must be valid for
2411 * reading 'flow', e.g. as checked by mf_check_src(). */
2413 mf_read_subfield(const struct mf_subfield *sf, const struct flow *flow,
2414 union mf_subvalue *x)
2416 union mf_value value;
2418 mf_get_value(sf->field, flow, &value);
2420 memset(x, 0, sizeof *x);
2421 bitwise_copy(&value, sf->field->n_bytes, sf->ofs,
2426 /* Returns the value of 'sf' within 'flow'. 'sf' must be valid for reading
2427 * 'flow', e.g. as checked by mf_check_src() and sf->n_bits must be 64 or
2430 mf_get_subfield(const struct mf_subfield *sf, const struct flow *flow)
2432 union mf_value value;
2434 mf_get_value(sf->field, flow, &value);
2435 return bitwise_get(&value, sf->field->n_bytes, sf->ofs, sf->n_bits);
2438 /* Formats 'sf' into 's' in a format normally acceptable to
2439 * mf_parse_subfield(). (It won't be acceptable if sf->field is NULL or if
2440 * sf->field has no NXM name.) */
2442 mf_format_subfield(const struct mf_subfield *sf, struct ds *s)
2445 ds_put_cstr(s, "<unknown>");
2446 } else if (sf->field->nxm_name) {
2447 ds_put_cstr(s, sf->field->nxm_name);
2448 } else if (sf->field->nxm_header) {
2449 uint32_t header = sf->field->nxm_header;
2450 ds_put_format(s, "%d:%d", NXM_VENDOR(header), NXM_FIELD(header));
2452 ds_put_cstr(s, sf->field->name);
2455 if (sf->field && sf->ofs == 0 && sf->n_bits == sf->field->n_bits) {
2456 ds_put_cstr(s, "[]");
2457 } else if (sf->n_bits == 1) {
2458 ds_put_format(s, "[%d]", sf->ofs);
2460 ds_put_format(s, "[%d..%d]", sf->ofs, sf->ofs + sf->n_bits - 1);
2464 static const struct mf_field *
2465 mf_parse_subfield_name(const char *name, int name_len, bool *wild)
2469 *wild = name_len > 2 && !memcmp(&name[name_len - 2], "_W", 2);
2474 for (i = 0; i < MFF_N_IDS; i++) {
2475 const struct mf_field *mf = mf_from_id(i);
2478 && !strncmp(mf->nxm_name, name, name_len)
2479 && mf->nxm_name[name_len] == '\0') {
2483 && !strncmp(mf->oxm_name, name, name_len)
2484 && mf->oxm_name[name_len] == '\0') {
2492 /* Parses a subfield from the beginning of '*sp' into 'sf'. If successful,
2493 * returns NULL and advances '*sp' to the first byte following the parsed
2494 * string. On failure, returns a malloc()'d error message, does not modify
2495 * '*sp', and does not properly initialize 'sf'.
2497 * The syntax parsed from '*sp' takes the form "header[start..end]" where
2498 * 'header' is the name of an NXM field and 'start' and 'end' are (inclusive)
2499 * bit indexes. "..end" may be omitted to indicate a single bit. "start..end"
2500 * may both be omitted (the [] are still required) to indicate an entire
2503 mf_parse_subfield__(struct mf_subfield *sf, const char **sp)
2505 const struct mf_field *field;
2514 name_len = strcspn(s, "[");
2515 if (s[name_len] != '[') {
2516 return xasprintf("%s: missing [ looking for field name", *sp);
2519 field = mf_parse_subfield_name(name, name_len, &wild);
2521 return xasprintf("%s: unknown field `%.*s'", *sp, name_len, s);
2525 if (sscanf(s, "[%d..%d]", &start, &end) == 2) {
2526 /* Nothing to do. */
2527 } else if (sscanf(s, "[%d]", &start) == 1) {
2529 } else if (!strncmp(s, "[]", 2)) {
2531 end = field->n_bits - 1;
2533 return xasprintf("%s: syntax error expecting [] or [<bit>] or "
2534 "[<start>..<end>]", *sp);
2536 s = strchr(s, ']') + 1;
2539 return xasprintf("%s: starting bit %d is after ending bit %d",
2541 } else if (start >= field->n_bits) {
2542 return xasprintf("%s: starting bit %d is not valid because field is "
2543 "only %d bits wide", *sp, start, field->n_bits);
2544 } else if (end >= field->n_bits){
2545 return xasprintf("%s: ending bit %d is not valid because field is "
2546 "only %d bits wide", *sp, end, field->n_bits);
2551 sf->n_bits = end - start + 1;
2557 /* Parses a subfield from the beginning of 's' into 'sf'. Returns the first
2558 * byte in 's' following the parsed string.
2560 * Exits with an error message if 's' has incorrect syntax.
2562 * The syntax parsed from 's' takes the form "header[start..end]" where
2563 * 'header' is the name of an NXM field and 'start' and 'end' are (inclusive)
2564 * bit indexes. "..end" may be omitted to indicate a single bit. "start..end"
2565 * may both be omitted (the [] are still required) to indicate an entire
2568 mf_parse_subfield(struct mf_subfield *sf, const char *s)
2570 char *msg = mf_parse_subfield__(sf, &s);
2572 ovs_fatal(0, "%s", msg);
2578 mf_format_subvalue(const union mf_subvalue *subvalue, struct ds *s)
2582 for (i = 0; i < ARRAY_SIZE(subvalue->u8); i++) {
2583 if (subvalue->u8[i]) {
2584 ds_put_format(s, "0x%"PRIx8, subvalue->u8[i]);
2585 for (i++; i < ARRAY_SIZE(subvalue->u8); i++) {
2586 ds_put_format(s, "%02"PRIx8, subvalue->u8[i]);
2591 ds_put_char(s, '0');