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_METADATA, "metadata", NULL,
65 OXM_OF_METADATA, "OXM_OF_METADATA",
66 OXM_OF_METADATA, "OXM_OF_METADATA",
68 MFF_IN_PORT, "in_port", NULL,
70 MFM_NONE, FWW_IN_PORT,
74 NXM_OF_IN_PORT, "NXM_OF_IN_PORT",
75 OXM_OF_IN_PORT, "OXM_OF_IN_PORT",
78 #define REGISTER(IDX) \
80 MFF_REG##IDX, "reg" #IDX, NULL, \
81 MF_FIELD_SIZES(be32), \
86 NXM_NX_REG(IDX), "NXM_NX_REG" #IDX, \
87 NXM_NX_REG(IDX), "NXM_NX_REG" #IDX, \
122 MFF_ETH_SRC, "eth_src", "dl_src",
128 NXM_OF_ETH_SRC, "NXM_OF_ETH_SRC",
129 OXM_OF_ETH_SRC, "OXM_OF_ETH_SRC",
131 MFF_ETH_DST, "eth_dst", "dl_dst",
137 NXM_OF_ETH_DST, "NXM_OF_ETH_DST",
138 OXM_OF_ETH_DST, "OXM_OF_ETH_DST",
140 MFF_ETH_TYPE, "eth_type", "dl_type",
141 MF_FIELD_SIZES(be16),
142 MFM_NONE, FWW_DL_TYPE,
146 NXM_OF_ETH_TYPE, "NXM_OF_ETH_TYPE",
147 OXM_OF_ETH_TYPE, "OXM_OF_ETH_TYPE",
151 MFF_VLAN_TCI, "vlan_tci", NULL,
152 MF_FIELD_SIZES(be16),
157 NXM_OF_VLAN_TCI, "NXM_OF_VLAN_TCI",
158 NXM_OF_VLAN_TCI, "NXM_OF_VLAN_TCI",
160 MFF_DL_VLAN, "dl_vlan", NULL,
161 sizeof(ovs_be16), 12,
169 MFF_VLAN_VID, "vlan_vid", NULL,
170 sizeof(ovs_be16), 12,
175 OXM_OF_VLAN_VID, "OXM_OF_VLAN_VID",
176 OXM_OF_VLAN_VID, "OXM_OF_VLAN_VID",
178 MFF_DL_VLAN_PCP, "dl_vlan_pcp", NULL,
187 MFF_VLAN_PCP, "vlan_pcp", NULL,
193 OXM_OF_VLAN_PCP, "OXM_OF_VLAN_PCP",
194 OXM_OF_VLAN_PCP, "OXM_OF_VLAN_PCP",
202 MFF_IPV4_SRC, "ip_src", "nw_src",
203 MF_FIELD_SIZES(be32),
208 NXM_OF_IP_SRC, "NXM_OF_IP_SRC",
209 OXM_OF_IPV4_SRC, "OXM_OF_IPV4_SRC",
211 MFF_IPV4_DST, "ip_dst", "nw_dst",
212 MF_FIELD_SIZES(be32),
217 NXM_OF_IP_DST, "NXM_OF_IP_DST",
218 OXM_OF_IPV4_DST, "OXM_OF_IPV4_DST",
222 MFF_IPV6_SRC, "ipv6_src", NULL,
223 MF_FIELD_SIZES(ipv6),
228 NXM_NX_IPV6_SRC, "NXM_NX_IPV6_SRC",
229 OXM_OF_IPV6_SRC, "OXM_OF_IPV6_SRC",
231 MFF_IPV6_DST, "ipv6_dst", NULL,
232 MF_FIELD_SIZES(ipv6),
237 NXM_NX_IPV6_DST, "NXM_NX_IPV6_DST",
238 OXM_OF_IPV6_DST, "OXM_OF_IPV6_DST",
241 MFF_IPV6_LABEL, "ipv6_label", NULL,
247 NXM_NX_IPV6_LABEL, "NXM_NX_IPV6_LABEL",
248 OXM_OF_IPV6_FLABEL, "OXM_OF_IPV6_FLABEL",
252 MFF_IP_PROTO, "nw_proto", NULL,
254 MFM_NONE, FWW_NW_PROTO,
258 NXM_OF_IP_PROTO, "NXM_OF_IP_PROTO",
259 OXM_OF_IP_PROTO, "OXM_OF_IP_PROTO",
261 MFF_IP_DSCP, "nw_tos", NULL,
263 MFM_NONE, FWW_NW_DSCP,
267 NXM_OF_IP_TOS, "NXM_OF_IP_TOS",
268 OXM_OF_IP_DSCP, "OXM_OF_IP_DSCP",
270 MFF_IP_ECN, "nw_ecn", NULL,
272 MFM_NONE, FWW_NW_ECN,
276 NXM_NX_IP_ECN, "NXM_NX_IP_ECN",
277 OXM_OF_IP_ECN, "OXM_OF_IP_ECN",
279 MFF_IP_TTL, "nw_ttl", NULL,
281 MFM_NONE, FWW_NW_TTL,
285 NXM_NX_IP_TTL, "NXM_NX_IP_TTL",
286 NXM_NX_IP_TTL, "NXM_NX_IP_TTL",
288 MFF_IP_FRAG, "ip_frag", NULL,
294 NXM_NX_IP_FRAG, "NXM_NX_IP_FRAG",
295 NXM_NX_IP_FRAG, "NXM_NX_IP_FRAG",
299 MFF_ARP_OP, "arp_op", NULL,
300 MF_FIELD_SIZES(be16),
301 MFM_NONE, FWW_NW_PROTO,
305 NXM_OF_ARP_OP, "NXM_OF_ARP_OP",
306 OXM_OF_ARP_OP, "OXM_OF_ARP_OP",
308 MFF_ARP_SPA, "arp_spa", NULL,
309 MF_FIELD_SIZES(be32),
314 NXM_OF_ARP_SPA, "NXM_OF_ARP_SPA",
315 OXM_OF_ARP_SPA, "OXM_OF_ARP_SPA",
317 MFF_ARP_TPA, "arp_tpa", NULL,
318 MF_FIELD_SIZES(be32),
323 NXM_OF_ARP_TPA, "NXM_OF_ARP_TPA",
324 OXM_OF_ARP_TPA, "OXM_OF_ARP_TPA",
326 MFF_ARP_SHA, "arp_sha", NULL,
332 NXM_NX_ARP_SHA, "NXM_NX_ARP_SHA",
333 OXM_OF_ARP_SHA, "OXM_OF_ARP_SHA",
335 MFF_ARP_THA, "arp_tha", NULL,
341 NXM_NX_ARP_THA, "NXM_NX_ARP_THA",
342 OXM_OF_ARP_THA, "OXM_OF_ARP_THA",
350 MFF_TCP_SRC, "tcp_src", "tp_src",
351 MF_FIELD_SIZES(be16),
356 NXM_OF_TCP_SRC, "NXM_OF_TCP_SRC",
357 OXM_OF_TCP_SRC, "OXM_OF_TCP_SRC",
359 MFF_TCP_DST, "tcp_dst", "tp_dst",
360 MF_FIELD_SIZES(be16),
365 NXM_OF_TCP_DST, "NXM_OF_TCP_DST",
366 OXM_OF_TCP_DST, "OXM_OF_TCP_DST",
370 MFF_UDP_SRC, "udp_src", NULL,
371 MF_FIELD_SIZES(be16),
376 NXM_OF_UDP_SRC, "NXM_OF_UDP_SRC",
377 OXM_OF_UDP_SRC, "OXM_OF_UDP_SRC",
379 MFF_UDP_DST, "udp_dst", NULL,
380 MF_FIELD_SIZES(be16),
385 NXM_OF_UDP_DST, "NXM_OF_UDP_DST",
386 OXM_OF_UDP_DST, "OXM_OF_UDP_DST",
390 MFF_ICMPV4_TYPE, "icmp_type", NULL,
396 NXM_OF_ICMP_TYPE, "NXM_OF_ICMP_TYPE",
397 OXM_OF_ICMPV4_TYPE, "OXM_OF_ICMPV4_TYPE",
399 MFF_ICMPV4_CODE, "icmp_code", NULL,
405 NXM_OF_ICMP_CODE, "NXM_OF_ICMP_CODE",
406 OXM_OF_ICMPV4_CODE, "OXM_OF_ICMPV4_CODE",
410 MFF_ICMPV6_TYPE, "icmpv6_type", NULL,
416 NXM_NX_ICMPV6_TYPE, "NXM_NX_ICMPV6_TYPE",
417 OXM_OF_ICMPV6_TYPE, "OXM_OF_ICMPV6_TYPE",
419 MFF_ICMPV6_CODE, "icmpv6_code", NULL,
425 NXM_NX_ICMPV6_CODE, "NXM_NX_ICMPV6_CODE",
426 OXM_OF_ICMPV6_CODE, "OXM_OF_ICMPV6_CODE",
434 MFF_ND_TARGET, "nd_target", NULL,
435 MF_FIELD_SIZES(ipv6),
440 NXM_NX_ND_TARGET, "NXM_NX_ND_TARGET",
441 OXM_OF_IPV6_ND_TARGET, "OXM_OF_IPV6_ND_TARGET",
443 MFF_ND_SLL, "nd_sll", NULL,
449 NXM_NX_ND_SLL, "NXM_NX_ND_SLL",
450 OXM_OF_IPV6_ND_SLL, "OXM_OF_IPV6_ND_SLL",
452 MFF_ND_TLL, "nd_tll", NULL,
458 NXM_NX_ND_TLL, "NXM_NX_ND_TLL",
459 OXM_OF_IPV6_ND_TLL, "OXM_OF_IPV6_ND_TLL",
463 /* Maps an NXM or OXM header value to an mf_field. */
465 struct hmap_node hmap_node; /* In 'all_fields' hmap. */
466 uint32_t header; /* NXM or OXM header value. */
467 const struct mf_field *mf;
470 /* Contains 'struct nxm_field's. */
471 static struct hmap all_fields = HMAP_INITIALIZER(&all_fields);
473 /* Rate limit for parse errors. These always indicate a bug in an OpenFlow
474 * controller and so there's not much point in showing a lot of them. */
475 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
477 const struct mf_field *mf_from_nxm_header__(uint32_t header);
479 /* Returns the field with the given 'id'. */
480 const struct mf_field *
481 mf_from_id(enum mf_field_id id)
483 assert((unsigned int) id < MFF_N_IDS);
484 return &mf_fields[id];
487 /* Returns the field with the given 'name', or a null pointer if no field has
489 const struct mf_field *
490 mf_from_name(const char *name)
492 static struct shash mf_by_name = SHASH_INITIALIZER(&mf_by_name);
494 if (shash_is_empty(&mf_by_name)) {
495 const struct mf_field *mf;
497 for (mf = mf_fields; mf < &mf_fields[MFF_N_IDS]; mf++) {
498 shash_add_once(&mf_by_name, mf->name, mf);
499 if (mf->extra_name) {
500 shash_add_once(&mf_by_name, mf->extra_name, mf);
505 return shash_find_data(&mf_by_name, name);
509 add_nxm_field(uint32_t header, const struct mf_field *mf)
513 f = xmalloc(sizeof *f);
514 hmap_insert(&all_fields, &f->hmap_node, hash_int(header, 0));
520 nxm_init_add_field(const struct mf_field *mf, uint32_t header)
523 assert(!mf_from_nxm_header__(header));
524 add_nxm_field(header, mf);
525 if (mf->maskable != MFM_NONE) {
526 add_nxm_field(NXM_MAKE_WILD_HEADER(header), mf);
534 const struct mf_field *mf;
536 for (mf = mf_fields; mf < &mf_fields[MFF_N_IDS]; mf++) {
537 nxm_init_add_field(mf, mf->nxm_header);
538 if (mf->oxm_header != mf->nxm_header) {
539 nxm_init_add_field(mf, mf->oxm_header);
544 const struct mf_field *
545 mf_from_nxm_header(uint32_t header)
547 if (hmap_is_empty(&all_fields)) {
550 return mf_from_nxm_header__(header);
553 const struct mf_field *
554 mf_from_nxm_header__(uint32_t header)
556 const struct nxm_field *f;
558 HMAP_FOR_EACH_IN_BUCKET (f, hmap_node, hash_int(header, 0), &all_fields) {
559 if (f->header == header) {
567 /* Returns true if 'wc' wildcards all the bits in field 'mf', false if 'wc'
568 * specifies at least one bit in the field.
570 * The caller is responsible for ensuring that 'wc' corresponds to a flow that
571 * meets 'mf''s prerequisites. */
573 mf_is_all_wild(const struct mf_field *mf, const struct flow_wildcards *wc)
583 assert(mf->fww_bit != 0);
584 return (wc->wildcards & mf->fww_bit) != 0;
587 return !wc->tun_id_mask;
589 return !wc->metadata_mask;
592 return !wc->reg_masks[mf->id - MFF_REG0];
595 return eth_addr_is_zero(wc->dl_src_mask);
597 return eth_addr_is_zero(wc->dl_dst_mask);
601 return eth_addr_is_zero(wc->arp_sha_mask);
605 return eth_addr_is_zero(wc->arp_tha_mask);
608 return !wc->vlan_tci_mask;
610 return !(wc->vlan_tci_mask & htons(VLAN_VID_MASK));
612 return !(wc->vlan_tci_mask & htons(VLAN_VID_MASK | VLAN_CFI));
613 case MFF_DL_VLAN_PCP:
615 return !(wc->vlan_tci_mask & htons(VLAN_PCP_MASK));
618 return !wc->nw_src_mask;
620 return !wc->nw_dst_mask;
623 return ipv6_mask_is_any(&wc->ipv6_src_mask);
625 return ipv6_mask_is_any(&wc->ipv6_dst_mask);
628 return !wc->ipv6_label_mask;
631 return ipv6_mask_is_any(&wc->nd_target_mask);
634 return !(wc->nw_frag_mask & FLOW_NW_FRAG_MASK);
637 return !wc->nw_src_mask;
639 return !wc->nw_dst_mask;
643 case MFF_ICMPV4_TYPE:
644 case MFF_ICMPV6_TYPE:
645 return !wc->tp_src_mask;
648 case MFF_ICMPV4_CODE:
649 case MFF_ICMPV6_CODE:
650 return !wc->tp_dst_mask;
658 /* Initializes 'mask' with the wildcard bit pattern for field 'mf' within 'wc'.
659 * Each bit in 'mask' will be set to 1 if the bit is significant for matching
660 * purposes, or to 0 if it is wildcarded.
662 * The caller is responsible for ensuring that 'wc' corresponds to a flow that
663 * meets 'mf''s prerequisites. */
665 mf_get_mask(const struct mf_field *mf, const struct flow_wildcards *wc,
666 union mf_value *mask)
676 assert(mf->fww_bit != 0);
677 memset(mask, wc->wildcards & mf->fww_bit ? 0x00 : 0xff, mf->n_bytes);
681 mask->be64 = wc->tun_id_mask;
684 mask->be64 = wc->metadata_mask;
688 mask->be32 = htonl(wc->reg_masks[mf->id - MFF_REG0]);
692 memcpy(mask->mac, wc->dl_dst_mask, ETH_ADDR_LEN);
696 memcpy(mask->mac, wc->dl_src_mask, ETH_ADDR_LEN);
700 mask->be16 = wc->vlan_tci_mask;
703 mask->be16 = wc->vlan_tci_mask & htons(VLAN_VID_MASK);
706 mask->be16 = wc->vlan_tci_mask & htons(VLAN_VID_MASK | VLAN_CFI);
708 case MFF_DL_VLAN_PCP:
710 mask->u8 = vlan_tci_to_pcp(wc->vlan_tci_mask);
714 mask->be32 = wc->nw_src_mask;
717 mask->be32 = wc->nw_dst_mask;
721 mask->ipv6 = wc->ipv6_src_mask;
724 mask->ipv6 = wc->ipv6_dst_mask;
727 mask->be32 = wc->ipv6_label_mask;
731 mask->ipv6 = wc->nd_target_mask;
735 mask->u8 = wc->nw_frag_mask & FLOW_NW_FRAG_MASK;
739 mask->be32 = wc->nw_src_mask;
742 mask->be32 = wc->nw_dst_mask;
746 memcpy(mask->mac, wc->arp_sha_mask, ETH_ADDR_LEN);
750 memcpy(mask->mac, wc->arp_tha_mask, ETH_ADDR_LEN);
755 mask->be16 = wc->tp_src_mask;
759 mask->be16 = wc->tp_dst_mask;
762 case MFF_ICMPV4_TYPE:
763 case MFF_ICMPV6_TYPE:
764 mask->u8 = ntohs(wc->tp_src_mask);
766 case MFF_ICMPV4_CODE:
767 case MFF_ICMPV6_CODE:
768 mask->u8 = ntohs(wc->tp_dst_mask);
777 /* Tests whether 'mask' is a valid wildcard bit pattern for 'mf'. Returns true
778 * if the mask is valid, false otherwise. */
780 mf_is_mask_valid(const struct mf_field *mf, const union mf_value *mask)
782 switch (mf->maskable) {
784 return (is_all_zeros((const uint8_t *) mask, mf->n_bytes) ||
785 is_all_ones((const uint8_t *) mask, mf->n_bytes));
795 is_ip_any(const struct flow *flow)
797 return (flow->dl_type == htons(ETH_TYPE_IP) ||
798 flow->dl_type == htons(ETH_TYPE_IPV6));
802 is_icmpv4(const struct flow *flow)
804 return (flow->dl_type == htons(ETH_TYPE_IP)
805 && flow->nw_proto == IPPROTO_ICMP);
809 is_icmpv6(const struct flow *flow)
811 return (flow->dl_type == htons(ETH_TYPE_IPV6)
812 && flow->nw_proto == IPPROTO_ICMPV6);
815 /* Returns true if 'flow' meets the prerequisites for 'mf', false otherwise. */
817 mf_are_prereqs_ok(const struct mf_field *mf, const struct flow *flow)
819 switch (mf->prereqs) {
824 return flow->dl_type == htons(ETH_TYPE_ARP);
826 return flow->dl_type == htons(ETH_TYPE_IP);
828 return flow->dl_type == htons(ETH_TYPE_IPV6);
830 return is_ip_any(flow);
833 return is_ip_any(flow) && flow->nw_proto == IPPROTO_TCP;
835 return is_ip_any(flow) && flow->nw_proto == IPPROTO_UDP;
837 return is_icmpv4(flow);
839 return is_icmpv6(flow);
842 return (is_icmpv6(flow)
843 && flow->tp_dst == htons(0)
844 && (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT) ||
845 flow->tp_src == htons(ND_NEIGHBOR_ADVERT)));
847 return (is_icmpv6(flow)
848 && flow->tp_dst == htons(0)
849 && (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT)));
851 return (is_icmpv6(flow)
852 && flow->tp_dst == htons(0)
853 && (flow->tp_src == htons(ND_NEIGHBOR_ADVERT)));
859 /* Returns true if 'value' may be a valid value *as part of a masked match*,
862 * A value is not rejected just because it is not valid for the field in
863 * question, but only if it doesn't make sense to test the bits in question at
864 * all. For example, the MFF_VLAN_TCI field will never have a nonzero value
865 * without the VLAN_CFI bit being set, but we can't reject those values because
866 * it is still legitimate to test just for those bits (see the documentation
867 * for NXM_OF_VLAN_TCI in nicira-ext.h). On the other hand, there is never a
868 * reason to set the low bit of MFF_IP_DSCP to 1, so we reject that. */
870 mf_is_value_valid(const struct mf_field *mf, const union mf_value *value)
895 case MFF_ICMPV4_TYPE:
896 case MFF_ICMPV4_CODE:
897 case MFF_ICMPV6_TYPE:
898 case MFF_ICMPV6_CODE:
905 return !(value->u8 & ~IP_DSCP_MASK);
907 return !(value->u8 & ~IP_ECN_MASK);
909 return !(value->u8 & ~FLOW_NW_FRAG_MASK);
912 return !(value->be16 & htons(0xff00));
915 return !(value->be16 & htons(VLAN_CFI | VLAN_PCP_MASK));
917 return !(value->be16 & htons(VLAN_PCP_MASK));
919 case MFF_DL_VLAN_PCP:
921 return !(value->u8 & ~(VLAN_PCP_MASK >> VLAN_PCP_SHIFT));
924 return !(value->be32 & ~htonl(IPV6_LABEL_MASK));
932 /* Copies the value of field 'mf' from 'flow' into 'value'. The caller is
933 * responsible for ensuring that 'flow' meets 'mf''s prerequisites. */
935 mf_get_value(const struct mf_field *mf, const struct flow *flow,
936 union mf_value *value)
940 value->be64 = flow->tun_id;
943 value->be64 = flow->metadata;
947 value->be16 = htons(flow->in_port);
951 value->be32 = htonl(flow->regs[mf->id - MFF_REG0]);
955 memcpy(value->mac, flow->dl_src, ETH_ADDR_LEN);
959 memcpy(value->mac, flow->dl_dst, ETH_ADDR_LEN);
963 value->be16 = flow->dl_type;
967 value->be16 = flow->vlan_tci;
971 value->be16 = flow->vlan_tci & htons(VLAN_VID_MASK);
974 value->be16 = flow->vlan_tci & htons(VLAN_VID_MASK | VLAN_CFI);
977 case MFF_DL_VLAN_PCP:
979 value->u8 = vlan_tci_to_pcp(flow->vlan_tci);
983 value->be32 = flow->nw_src;
987 value->be32 = flow->nw_dst;
991 value->ipv6 = flow->ipv6_src;
995 value->ipv6 = flow->ipv6_dst;
999 value->be32 = flow->ipv6_label;
1003 value->u8 = flow->nw_proto;
1007 value->u8 = flow->nw_tos & IP_DSCP_MASK;
1011 value->u8 = flow->nw_tos & IP_ECN_MASK;
1015 value->u8 = flow->nw_ttl;
1019 value->u8 = flow->nw_frag;
1023 value->be16 = htons(flow->nw_proto);
1027 value->be32 = flow->nw_src;
1031 value->be32 = flow->nw_dst;
1036 memcpy(value->mac, flow->arp_sha, ETH_ADDR_LEN);
1041 memcpy(value->mac, flow->arp_tha, ETH_ADDR_LEN);
1046 value->be16 = flow->tp_src;
1051 value->be16 = flow->tp_dst;
1054 case MFF_ICMPV4_TYPE:
1055 case MFF_ICMPV6_TYPE:
1056 value->u8 = ntohs(flow->tp_src);
1059 case MFF_ICMPV4_CODE:
1060 case MFF_ICMPV6_CODE:
1061 value->u8 = ntohs(flow->tp_dst);
1065 value->ipv6 = flow->nd_target;
1074 /* Makes 'rule' match field 'mf' exactly, with the value matched taken from
1075 * 'value'. The caller is responsible for ensuring that 'rule' meets 'mf''s
1078 mf_set_value(const struct mf_field *mf,
1079 const union mf_value *value, struct cls_rule *rule)
1083 cls_rule_set_tun_id(rule, value->be64);
1086 cls_rule_set_metadata(rule, value->be64);
1090 cls_rule_set_in_port(rule, ntohs(value->be16));
1094 cls_rule_set_reg(rule, mf->id - MFF_REG0, ntohl(value->be32));
1098 cls_rule_set_dl_src(rule, value->mac);
1102 cls_rule_set_dl_dst(rule, value->mac);
1106 cls_rule_set_dl_type(rule, value->be16);
1110 cls_rule_set_dl_tci(rule, value->be16);
1114 cls_rule_set_dl_vlan(rule, value->be16);
1117 cls_rule_set_vlan_vid(rule, value->be16);
1120 case MFF_DL_VLAN_PCP:
1122 cls_rule_set_dl_vlan_pcp(rule, value->u8);
1126 cls_rule_set_nw_src(rule, value->be32);
1130 cls_rule_set_nw_dst(rule, value->be32);
1134 cls_rule_set_ipv6_src(rule, &value->ipv6);
1138 cls_rule_set_ipv6_dst(rule, &value->ipv6);
1141 case MFF_IPV6_LABEL:
1142 cls_rule_set_ipv6_label(rule, value->be32);
1146 cls_rule_set_nw_proto(rule, value->u8);
1150 cls_rule_set_nw_dscp(rule, value->u8);
1154 cls_rule_set_nw_ecn(rule, value->u8);
1158 cls_rule_set_nw_ttl(rule, value->u8);
1162 cls_rule_set_nw_frag(rule, value->u8);
1166 cls_rule_set_nw_proto(rule, ntohs(value->be16));
1170 cls_rule_set_nw_src(rule, value->be32);
1174 cls_rule_set_nw_dst(rule, value->be32);
1179 cls_rule_set_arp_sha(rule, value->mac);
1184 cls_rule_set_arp_tha(rule, value->mac);
1189 cls_rule_set_tp_src(rule, value->be16);
1194 cls_rule_set_tp_dst(rule, value->be16);
1197 case MFF_ICMPV4_TYPE:
1198 case MFF_ICMPV6_TYPE:
1199 cls_rule_set_icmp_type(rule, value->u8);
1202 case MFF_ICMPV4_CODE:
1203 case MFF_ICMPV6_CODE:
1204 cls_rule_set_icmp_code(rule, value->u8);
1208 cls_rule_set_nd_target(rule, &value->ipv6);
1217 /* Makes 'rule' match field 'mf' exactly, with the value matched taken from
1218 * 'value'. The caller is responsible for ensuring that 'rule' meets 'mf''s
1221 mf_set_flow_value(const struct mf_field *mf,
1222 const union mf_value *value, struct flow *flow)
1226 flow->tun_id = value->be64;
1229 flow->metadata = value->be64;
1233 flow->in_port = ntohs(value->be16);
1237 flow->regs[mf->id - MFF_REG0] = ntohl(value->be32);
1241 memcpy(flow->dl_src, value->mac, ETH_ADDR_LEN);
1245 memcpy(flow->dl_dst, value->mac, ETH_ADDR_LEN);
1249 flow->dl_type = value->be16;
1253 flow->vlan_tci = value->be16;
1257 flow_set_dl_vlan(flow, value->be16);
1260 flow_set_vlan_vid(flow, value->be16);
1263 case MFF_DL_VLAN_PCP:
1265 flow_set_vlan_pcp(flow, value->u8);
1269 flow->nw_src = value->be32;
1273 flow->nw_dst = value->be32;
1277 flow->ipv6_src = value->ipv6;
1281 flow->ipv6_dst = value->ipv6;
1284 case MFF_IPV6_LABEL:
1285 flow->ipv6_label = value->be32 & ~htonl(IPV6_LABEL_MASK);
1289 flow->nw_proto = value->u8;
1293 flow->nw_tos &= ~IP_DSCP_MASK;
1294 flow->nw_tos |= value->u8 & IP_DSCP_MASK;
1298 flow->nw_tos &= ~IP_ECN_MASK;
1299 flow->nw_tos |= value->u8 & IP_ECN_MASK;
1303 flow->nw_ttl = value->u8;
1307 flow->nw_frag &= value->u8;
1311 flow->nw_proto = ntohs(value->be16);
1315 flow->nw_src = value->be32;
1319 flow->nw_dst = value->be32;
1324 memcpy(flow->arp_sha, value->mac, ETH_ADDR_LEN);
1329 memcpy(flow->arp_tha, value->mac, ETH_ADDR_LEN);
1334 flow->tp_src = value->be16;
1339 flow->tp_dst = value->be16;
1342 case MFF_ICMPV4_TYPE:
1343 case MFF_ICMPV6_TYPE:
1344 flow->tp_src = htons(value->u8);
1347 case MFF_ICMPV4_CODE:
1348 case MFF_ICMPV6_CODE:
1349 flow->tp_dst = htons(value->u8);
1353 flow->nd_target = value->ipv6;
1362 /* Returns true if 'mf' has a zero value in 'flow', false if it is nonzero.
1364 * The caller is responsible for ensuring that 'flow' meets 'mf''s
1367 mf_is_zero(const struct mf_field *mf, const struct flow *flow)
1369 union mf_value value;
1371 mf_get_value(mf, flow, &value);
1372 return is_all_zeros((const uint8_t *) &value, mf->n_bytes);
1375 /* Makes 'rule' wildcard field 'mf'.
1377 * The caller is responsible for ensuring that 'rule' meets 'mf''s
1380 mf_set_wild(const struct mf_field *mf, struct cls_rule *rule)
1384 cls_rule_set_tun_id_masked(rule, htonll(0), htonll(0));
1387 cls_rule_set_metadata_masked(rule, htonll(0), htonll(0));
1390 rule->wc.wildcards |= FWW_IN_PORT;
1391 rule->flow.in_port = 0;
1395 cls_rule_set_reg_masked(rule, mf->id - MFF_REG0, 0, 0);
1399 memset(rule->flow.dl_src, 0, ETH_ADDR_LEN);
1400 memset(rule->wc.dl_src_mask, 0, ETH_ADDR_LEN);
1404 memset(rule->flow.dl_dst, 0, ETH_ADDR_LEN);
1405 memset(rule->wc.dl_dst_mask, 0, ETH_ADDR_LEN);
1409 rule->wc.wildcards |= FWW_DL_TYPE;
1410 rule->flow.dl_type = htons(0);
1414 cls_rule_set_dl_tci_masked(rule, htons(0), htons(0));
1419 cls_rule_set_any_vid(rule);
1422 case MFF_DL_VLAN_PCP:
1424 cls_rule_set_any_pcp(rule);
1429 cls_rule_set_nw_src_masked(rule, htonl(0), htonl(0));
1434 cls_rule_set_nw_dst_masked(rule, htonl(0), htonl(0));
1438 memset(&rule->wc.ipv6_src_mask, 0, sizeof rule->wc.ipv6_src_mask);
1439 memset(&rule->flow.ipv6_src, 0, sizeof rule->flow.ipv6_src);
1443 memset(&rule->wc.ipv6_dst_mask, 0, sizeof rule->wc.ipv6_dst_mask);
1444 memset(&rule->flow.ipv6_dst, 0, sizeof rule->flow.ipv6_dst);
1447 case MFF_IPV6_LABEL:
1448 rule->wc.ipv6_label_mask = 0;
1449 rule->flow.ipv6_label = 0;
1453 rule->wc.wildcards |= FWW_NW_PROTO;
1454 rule->flow.nw_proto = 0;
1458 rule->wc.wildcards |= FWW_NW_DSCP;
1459 rule->flow.nw_tos &= ~IP_DSCP_MASK;
1463 rule->wc.wildcards |= FWW_NW_ECN;
1464 rule->flow.nw_tos &= ~IP_ECN_MASK;
1468 rule->wc.wildcards |= FWW_NW_TTL;
1469 rule->flow.nw_ttl = 0;
1473 rule->wc.nw_frag_mask |= FLOW_NW_FRAG_MASK;
1474 rule->flow.nw_frag &= ~FLOW_NW_FRAG_MASK;
1478 rule->wc.wildcards |= FWW_NW_PROTO;
1479 rule->flow.nw_proto = 0;
1484 memset(rule->flow.arp_sha, 0, ETH_ADDR_LEN);
1485 memset(rule->wc.arp_sha_mask, 0, ETH_ADDR_LEN);
1490 memset(rule->flow.arp_tha, 0, ETH_ADDR_LEN);
1491 memset(rule->wc.arp_tha_mask, 0, ETH_ADDR_LEN);
1496 case MFF_ICMPV4_TYPE:
1497 case MFF_ICMPV6_TYPE:
1498 rule->wc.tp_src_mask = htons(0);
1499 rule->flow.tp_src = htons(0);
1504 case MFF_ICMPV4_CODE:
1505 case MFF_ICMPV6_CODE:
1506 rule->wc.tp_dst_mask = htons(0);
1507 rule->flow.tp_dst = htons(0);
1511 memset(&rule->wc.nd_target_mask, 0, sizeof rule->wc.nd_target_mask);
1512 memset(&rule->flow.nd_target, 0, sizeof rule->flow.nd_target);
1521 /* Makes 'rule' match field 'mf' with the specified 'value' and 'mask'.
1522 * 'value' specifies a value to match and 'mask' specifies a wildcard pattern,
1523 * with a 1-bit indicating that the corresponding value bit must match and a
1524 * 0-bit indicating a don't-care.
1526 * If 'mask' is NULL or points to all-1-bits, then this call is equivalent to
1527 * mf_set_value(mf, value, rule). If 'mask' points to all-0-bits, then this
1528 * call is equivalent to mf_set_wild(mf, rule).
1530 * 'mask' must be a valid mask for 'mf' (see mf_is_mask_valid()). The caller
1531 * is responsible for ensuring that 'rule' meets 'mf''s prerequisites. */
1533 mf_set(const struct mf_field *mf,
1534 const union mf_value *value, const union mf_value *mask,
1535 struct cls_rule *rule)
1537 if (!mask || is_all_ones((const uint8_t *) mask, mf->n_bytes)) {
1538 mf_set_value(mf, value, rule);
1540 } else if (is_all_zeros((const uint8_t *) mask, mf->n_bytes)) {
1541 mf_set_wild(mf, rule);
1549 case MFF_DL_VLAN_PCP:
1556 case MFF_ICMPV4_TYPE:
1557 case MFF_ICMPV4_CODE:
1558 case MFF_ICMPV6_TYPE:
1559 case MFF_ICMPV6_CODE:
1563 cls_rule_set_tun_id_masked(rule, value->be64, mask->be64);
1566 cls_rule_set_metadata_masked(rule, value->be64, mask->be64);
1570 cls_rule_set_reg_masked(rule, mf->id - MFF_REG0,
1571 ntohl(value->be32), ntohl(mask->be32));
1575 cls_rule_set_dl_dst_masked(rule, value->mac, mask->mac);
1579 cls_rule_set_dl_src_masked(rule, value->mac, mask->mac);
1584 cls_rule_set_arp_sha_masked(rule, value->mac, mask->mac);
1589 cls_rule_set_arp_tha_masked(rule, value->mac, mask->mac);
1593 cls_rule_set_dl_tci_masked(rule, value->be16, mask->be16);
1597 cls_rule_set_vlan_vid_masked(rule, value->be16, mask->be16);
1601 cls_rule_set_nw_src_masked(rule, value->be32, mask->be32);
1605 cls_rule_set_nw_dst_masked(rule, value->be32, mask->be32);
1609 cls_rule_set_ipv6_src_masked(rule, &value->ipv6, &mask->ipv6);
1613 cls_rule_set_ipv6_dst_masked(rule, &value->ipv6, &mask->ipv6);
1616 case MFF_IPV6_LABEL:
1617 if ((mask->be32 & htonl(IPV6_LABEL_MASK)) == htonl(IPV6_LABEL_MASK)) {
1618 mf_set_value(mf, value, rule);
1620 cls_rule_set_ipv6_label_masked(rule, value->be32, mask->be32);
1625 cls_rule_set_nd_target_masked(rule, &value->ipv6, &mask->ipv6);
1629 cls_rule_set_nw_frag_masked(rule, value->u8, mask->u8);
1633 cls_rule_set_nw_src_masked(rule, value->be32, mask->be32);
1637 cls_rule_set_nw_dst_masked(rule, value->be32, mask->be32);
1642 cls_rule_set_tp_src_masked(rule, value->be16, mask->be16);
1647 cls_rule_set_tp_dst_masked(rule, value->be16, mask->be16);
1657 mf_check__(const struct mf_subfield *sf, const struct flow *flow,
1661 VLOG_WARN_RL(&rl, "unknown %s field", type);
1662 } else if (!sf->n_bits) {
1663 VLOG_WARN_RL(&rl, "zero bit %s field %s", type, sf->field->name);
1664 } else if (sf->ofs >= sf->field->n_bits) {
1665 VLOG_WARN_RL(&rl, "bit offset %d exceeds %d-bit width of %s field %s",
1666 sf->ofs, sf->field->n_bits, type, sf->field->name);
1667 } else if (sf->ofs + sf->n_bits > sf->field->n_bits) {
1668 VLOG_WARN_RL(&rl, "bit offset %d and width %d exceeds %d-bit width "
1669 "of %s field %s", sf->ofs, sf->n_bits,
1670 sf->field->n_bits, type, sf->field->name);
1671 } else if (flow && !mf_are_prereqs_ok(sf->field, flow)) {
1672 VLOG_WARN_RL(&rl, "%s field %s lacks correct prerequisites",
1673 type, sf->field->name);
1678 return OFPERR_OFPBAC_BAD_ARGUMENT;
1681 /* Checks whether 'sf' is valid for reading a subfield out of 'flow'. Returns
1682 * 0 if so, otherwise an OpenFlow error code (e.g. as returned by
1685 mf_check_src(const struct mf_subfield *sf, const struct flow *flow)
1687 return mf_check__(sf, flow, "source");
1690 /* Checks whether 'sf' is valid for writing a subfield into 'flow'. Returns 0
1691 * if so, otherwise an OpenFlow error code (e.g. as returned by
1694 mf_check_dst(const struct mf_subfield *sf, const struct flow *flow)
1696 int error = mf_check__(sf, flow, "destination");
1697 if (!error && !sf->field->writable) {
1698 VLOG_WARN_RL(&rl, "destination field %s is not writable",
1700 return OFPERR_OFPBAC_BAD_ARGUMENT;
1705 /* Copies the value and wildcard bit pattern for 'mf' from 'rule' into the
1706 * 'value' and 'mask', respectively. */
1708 mf_get(const struct mf_field *mf, const struct cls_rule *rule,
1709 union mf_value *value, union mf_value *mask)
1711 mf_get_value(mf, &rule->flow, value);
1712 mf_get_mask(mf, &rule->wc, mask);
1715 /* Assigns a random value for field 'mf' to 'value'. */
1717 mf_random_value(const struct mf_field *mf, union mf_value *value)
1719 random_bytes(value, mf->n_bytes);
1744 case MFF_ICMPV4_TYPE:
1745 case MFF_ICMPV4_CODE:
1746 case MFF_ICMPV6_TYPE:
1747 case MFF_ICMPV6_CODE:
1753 case MFF_IPV6_LABEL:
1754 value->be32 &= ~htonl(IPV6_LABEL_MASK);
1758 value->u8 &= IP_DSCP_MASK;
1762 value->u8 &= IP_ECN_MASK;
1766 value->u8 &= FLOW_NW_FRAG_MASK;
1770 value->be16 &= htons(0xff);
1774 value->be16 &= htons(VLAN_VID_MASK);
1777 value->be16 &= htons(VLAN_VID_MASK | VLAN_CFI);
1780 case MFF_DL_VLAN_PCP:
1792 mf_from_integer_string(const struct mf_field *mf, const char *s,
1793 uint8_t *valuep, uint8_t *maskp)
1795 unsigned long long int integer, mask;
1800 integer = strtoull(s, &tail, 0);
1801 if (errno || (*tail != '\0' && *tail != '/')) {
1806 mask = strtoull(tail + 1, &tail, 0);
1807 if (errno || *tail != '\0') {
1814 for (i = mf->n_bytes - 1; i >= 0; i--) {
1815 valuep[i] = integer;
1821 return xasprintf("%s: value too large for %u-byte field %s",
1822 s, mf->n_bytes, mf->name);
1827 return xasprintf("%s: bad syntax for %s", s, mf->name);
1831 mf_from_ethernet_string(const struct mf_field *mf, const char *s,
1832 uint8_t mac[ETH_ADDR_LEN],
1833 uint8_t mask[ETH_ADDR_LEN])
1835 assert(mf->n_bytes == ETH_ADDR_LEN);
1837 switch (sscanf(s, ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT,
1838 ETH_ADDR_SCAN_ARGS(mac), ETH_ADDR_SCAN_ARGS(mask))){
1839 case ETH_ADDR_SCAN_COUNT * 2:
1842 case ETH_ADDR_SCAN_COUNT:
1843 memset(mask, 0xff, ETH_ADDR_LEN);
1847 return xasprintf("%s: invalid Ethernet address", s);
1852 mf_from_ipv4_string(const struct mf_field *mf, const char *s,
1853 ovs_be32 *ip, ovs_be32 *mask)
1857 assert(mf->n_bytes == sizeof *ip);
1859 if (sscanf(s, IP_SCAN_FMT"/"IP_SCAN_FMT,
1860 IP_SCAN_ARGS(ip), IP_SCAN_ARGS(mask)) == IP_SCAN_COUNT * 2) {
1862 } else if (sscanf(s, IP_SCAN_FMT"/%d",
1863 IP_SCAN_ARGS(ip), &prefix) == IP_SCAN_COUNT + 1) {
1864 if (prefix <= 0 || prefix > 32) {
1865 return xasprintf("%s: network prefix bits not between 1 and "
1867 } else if (prefix == 32) {
1868 *mask = htonl(UINT32_MAX);
1870 *mask = htonl(((1u << prefix) - 1) << (32 - prefix));
1872 } else if (sscanf(s, IP_SCAN_FMT, IP_SCAN_ARGS(ip)) == IP_SCAN_COUNT) {
1873 *mask = htonl(UINT32_MAX);
1875 return xasprintf("%s: invalid IP address", s);
1881 mf_from_ipv6_string(const struct mf_field *mf, const char *s,
1882 struct in6_addr *value, struct in6_addr *mask)
1884 char *str = xstrdup(s);
1885 char *save_ptr = NULL;
1886 const char *name, *netmask;
1889 assert(mf->n_bytes == sizeof *value);
1891 name = strtok_r(str, "/", &save_ptr);
1892 retval = name ? lookup_ipv6(name, value) : EINVAL;
1896 err = xasprintf("%s: could not convert to IPv6 address", str);
1902 netmask = strtok_r(NULL, "/", &save_ptr);
1904 if (inet_pton(AF_INET6, netmask, mask) != 1) {
1905 int prefix = atoi(netmask);
1906 if (prefix <= 0 || prefix > 128) {
1908 return xasprintf("%s: prefix bits not between 1 and 128", s);
1910 *mask = ipv6_create_mask(prefix);
1914 *mask = in6addr_exact;
1922 mf_from_ofp_port_string(const struct mf_field *mf, const char *s,
1923 ovs_be16 *valuep, ovs_be16 *maskp)
1927 assert(mf->n_bytes == sizeof(ovs_be16));
1928 if (ofputil_port_from_string(s, &port)) {
1929 *valuep = htons(port);
1930 *maskp = htons(UINT16_MAX);
1933 return mf_from_integer_string(mf, s,
1934 (uint8_t *) valuep, (uint8_t *) maskp);
1938 struct frag_handling {
1944 static const struct frag_handling all_frags[] = {
1945 #define A FLOW_NW_FRAG_ANY
1946 #define L FLOW_NW_FRAG_LATER
1947 /* name mask value */
1950 { "first", A|L, A },
1951 { "later", A|L, A|L },
1956 { "not_later", L, 0 },
1963 mf_from_frag_string(const char *s, uint8_t *valuep, uint8_t *maskp)
1965 const struct frag_handling *h;
1967 for (h = all_frags; h < &all_frags[ARRAY_SIZE(all_frags)]; h++) {
1968 if (!strcasecmp(s, h->name)) {
1969 /* We force the upper bits of the mask on to make mf_parse_value()
1970 * happy (otherwise it will never think it's an exact match.) */
1971 *maskp = h->mask | ~FLOW_NW_FRAG_MASK;
1977 return xasprintf("%s: unknown fragment type (valid types are \"no\", "
1978 "\"yes\", \"first\", \"later\", \"not_first\"", s);
1981 /* Parses 's', a string value for field 'mf', into 'value' and 'mask'. Returns
1982 * NULL if successful, otherwise a malloc()'d string describing the error. */
1984 mf_parse(const struct mf_field *mf, const char *s,
1985 union mf_value *value, union mf_value *mask)
1987 if (!strcasecmp(s, "any") || !strcmp(s, "*")) {
1988 memset(value, 0, mf->n_bytes);
1989 memset(mask, 0, mf->n_bytes);
1993 switch (mf->string) {
1995 case MFS_HEXADECIMAL:
1996 return mf_from_integer_string(mf, s,
1997 (uint8_t *) value, (uint8_t *) mask);
2000 return mf_from_ethernet_string(mf, s, value->mac, mask->mac);
2003 return mf_from_ipv4_string(mf, s, &value->be32, &mask->be32);
2006 return mf_from_ipv6_string(mf, s, &value->ipv6, &mask->ipv6);
2009 return mf_from_ofp_port_string(mf, s, &value->be16, &mask->be16);
2012 return mf_from_frag_string(s, &value->u8, &mask->u8);
2017 /* Parses 's', a string value for field 'mf', into 'value'. Returns NULL if
2018 * successful, otherwise a malloc()'d string describing the error. */
2020 mf_parse_value(const struct mf_field *mf, const char *s, union mf_value *value)
2022 union mf_value mask;
2025 error = mf_parse(mf, s, value, &mask);
2030 if (!is_all_ones((const uint8_t *) &mask, mf->n_bytes)) {
2031 return xasprintf("%s: wildcards not allowed here", s);
2037 mf_format_integer_string(const struct mf_field *mf, const uint8_t *valuep,
2038 const uint8_t *maskp, struct ds *s)
2040 unsigned long long int integer;
2043 assert(mf->n_bytes <= 8);
2046 for (i = 0; i < mf->n_bytes; i++) {
2047 integer = (integer << 8) | valuep[i];
2049 if (mf->string == MFS_HEXADECIMAL) {
2050 ds_put_format(s, "%#llx", integer);
2052 ds_put_format(s, "%lld", integer);
2056 unsigned long long int mask;
2059 for (i = 0; i < mf->n_bytes; i++) {
2060 mask = (mask << 8) | maskp[i];
2063 /* I guess we could write the mask in decimal for MFS_DECIMAL but I'm
2064 * not sure that that a bit-mask written in decimal is ever easier to
2065 * understand than the same bit-mask written in hexadecimal. */
2066 ds_put_format(s, "/%#llx", mask);
2071 mf_format_frag_string(const uint8_t *valuep, const uint8_t *maskp,
2074 const struct frag_handling *h;
2075 uint8_t value = *valuep;
2076 uint8_t mask = *maskp;
2079 mask &= FLOW_NW_FRAG_MASK;
2081 for (h = all_frags; h < &all_frags[ARRAY_SIZE(all_frags)]; h++) {
2082 if (value == h->value && mask == h->mask) {
2083 ds_put_cstr(s, h->name);
2087 ds_put_cstr(s, "<error>");
2090 /* Appends to 's' a string representation of field 'mf' whose value is in
2091 * 'value' and 'mask'. 'mask' may be NULL to indicate an exact match. */
2093 mf_format(const struct mf_field *mf,
2094 const union mf_value *value, const union mf_value *mask,
2098 if (is_all_zeros((const uint8_t *) mask, mf->n_bytes)) {
2099 ds_put_cstr(s, "ANY");
2101 } else if (is_all_ones((const uint8_t *) mask, mf->n_bytes)) {
2106 switch (mf->string) {
2109 ofputil_format_port(ntohs(value->be16), s);
2114 case MFS_HEXADECIMAL:
2115 mf_format_integer_string(mf, (uint8_t *) value, (uint8_t *) mask, s);
2119 eth_format_masked(value->mac, mask->mac, s);
2123 ip_format_masked(value->be32, mask ? mask->be32 : htonl(UINT32_MAX),
2128 print_ipv6_masked(s, &value->ipv6, mask ? &mask->ipv6 : NULL);
2132 mf_format_frag_string(&value->u8, &mask->u8, s);
2140 /* Makes subfield 'sf' within 'rule' exactly match the 'sf->n_bits'
2141 * least-significant bits in 'x'.
2144 mf_write_subfield(const struct mf_subfield *sf, const union mf_subvalue *x,
2145 struct cls_rule *rule)
2147 const struct mf_field *field = sf->field;
2148 union mf_value value, mask;
2150 mf_get(field, rule, &value, &mask);
2151 bitwise_copy(x, sizeof *x, 0, &value, field->n_bytes, sf->ofs, sf->n_bits);
2152 bitwise_one ( &mask, field->n_bytes, sf->ofs, sf->n_bits);
2153 mf_set(field, &value, &mask, rule);
2156 /* Initializes 'x' to the value of 'sf' within 'flow'. 'sf' must be valid for
2157 * reading 'flow', e.g. as checked by mf_check_src(). */
2159 mf_read_subfield(const struct mf_subfield *sf, const struct flow *flow,
2160 union mf_subvalue *x)
2162 union mf_value value;
2164 mf_get_value(sf->field, flow, &value);
2166 memset(x, 0, sizeof *x);
2167 bitwise_copy(&value, sf->field->n_bytes, sf->ofs,
2172 /* Returns the value of 'sf' within 'flow'. 'sf' must be valid for reading
2173 * 'flow', e.g. as checked by mf_check_src() and sf->n_bits must be 64 or
2176 mf_get_subfield(const struct mf_subfield *sf, const struct flow *flow)
2178 union mf_value value;
2180 mf_get_value(sf->field, flow, &value);
2181 return bitwise_get(&value, sf->field->n_bytes, sf->ofs, sf->n_bits);
2184 /* Formats 'sf' into 's' in a format normally acceptable to
2185 * mf_parse_subfield(). (It won't be acceptable if sf->field is NULL or if
2186 * sf->field has no NXM name.) */
2188 mf_format_subfield(const struct mf_subfield *sf, struct ds *s)
2191 ds_put_cstr(s, "<unknown>");
2192 } else if (sf->field->nxm_name) {
2193 ds_put_cstr(s, sf->field->nxm_name);
2194 } else if (sf->field->nxm_header) {
2195 uint32_t header = sf->field->nxm_header;
2196 ds_put_format(s, "%d:%d", NXM_VENDOR(header), NXM_FIELD(header));
2198 ds_put_cstr(s, sf->field->name);
2201 if (sf->field && sf->ofs == 0 && sf->n_bits == sf->field->n_bits) {
2202 ds_put_cstr(s, "[]");
2203 } else if (sf->n_bits == 1) {
2204 ds_put_format(s, "[%d]", sf->ofs);
2206 ds_put_format(s, "[%d..%d]", sf->ofs, sf->ofs + sf->n_bits - 1);
2210 static const struct mf_field *
2211 mf_parse_subfield_name(const char *name, int name_len, bool *wild)
2215 *wild = name_len > 2 && !memcmp(&name[name_len - 2], "_W", 2);
2220 for (i = 0; i < MFF_N_IDS; i++) {
2221 const struct mf_field *mf = mf_from_id(i);
2224 && !strncmp(mf->nxm_name, name, name_len)
2225 && mf->nxm_name[name_len] == '\0') {
2229 && !strncmp(mf->oxm_name, name, name_len)
2230 && mf->oxm_name[name_len] == '\0') {
2238 /* Parses a subfield from the beginning of '*sp' into 'sf'. If successful,
2239 * returns NULL and advances '*sp' to the first byte following the parsed
2240 * string. On failure, returns a malloc()'d error message, does not modify
2241 * '*sp', and does not properly initialize 'sf'.
2243 * The syntax parsed from '*sp' takes the form "header[start..end]" where
2244 * 'header' is the name of an NXM field and 'start' and 'end' are (inclusive)
2245 * bit indexes. "..end" may be omitted to indicate a single bit. "start..end"
2246 * may both be omitted (the [] are still required) to indicate an entire
2249 mf_parse_subfield__(struct mf_subfield *sf, const char **sp)
2251 const struct mf_field *field;
2260 name_len = strcspn(s, "[");
2261 if (s[name_len] != '[') {
2262 return xasprintf("%s: missing [ looking for field name", *sp);
2265 field = mf_parse_subfield_name(name, name_len, &wild);
2267 return xasprintf("%s: unknown field `%.*s'", *sp, name_len, s);
2271 if (sscanf(s, "[%d..%d]", &start, &end) == 2) {
2272 /* Nothing to do. */
2273 } else if (sscanf(s, "[%d]", &start) == 1) {
2275 } else if (!strncmp(s, "[]", 2)) {
2277 end = field->n_bits - 1;
2279 return xasprintf("%s: syntax error expecting [] or [<bit>] or "
2280 "[<start>..<end>]", *sp);
2282 s = strchr(s, ']') + 1;
2285 return xasprintf("%s: starting bit %d is after ending bit %d",
2287 } else if (start >= field->n_bits) {
2288 return xasprintf("%s: starting bit %d is not valid because field is "
2289 "only %d bits wide", *sp, start, field->n_bits);
2290 } else if (end >= field->n_bits){
2291 return xasprintf("%s: ending bit %d is not valid because field is "
2292 "only %d bits wide", *sp, end, field->n_bits);
2297 sf->n_bits = end - start + 1;
2303 /* Parses a subfield from the beginning of 's' into 'sf'. Returns the first
2304 * byte in 's' following the parsed string.
2306 * Exits with an error message if 's' has incorrect syntax.
2308 * The syntax parsed from 's' takes the form "header[start..end]" where
2309 * 'header' is the name of an NXM field and 'start' and 'end' are (inclusive)
2310 * bit indexes. "..end" may be omitted to indicate a single bit. "start..end"
2311 * may both be omitted (the [] are still required) to indicate an entire
2314 mf_parse_subfield(struct mf_subfield *sf, const char *s)
2316 char *msg = mf_parse_subfield__(sf, &s);
2318 ovs_fatal(0, "%s", msg);