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",
59 MFF_IN_PORT, "in_port", NULL,
61 MFM_NONE, FWW_IN_PORT,
65 NXM_OF_IN_PORT, "NXM_OF_IN_PORT",
66 OXM_OF_IN_PORT, "OXM_OF_IN_PORT",
69 #define REGISTER(IDX) \
71 MFF_REG##IDX, "reg" #IDX, NULL, \
72 MF_FIELD_SIZES(be32), \
114 MFF_ETH_SRC, "eth_src", "dl_src",
116 MFM_NONE, FWW_DL_SRC,
120 NXM_OF_ETH_SRC, "NXM_OF_ETH_SRC",
121 OXM_OF_ETH_SRC, "OXM_OF_ETH_SRC",
123 MFF_ETH_DST, "eth_dst", "dl_dst",
129 NXM_OF_ETH_DST, "NXM_OF_ETH_DST",
130 OXM_OF_ETH_DST, "OXM_OF_ETH_DST",
132 MFF_ETH_TYPE, "eth_type", "dl_type",
133 MF_FIELD_SIZES(be16),
134 MFM_NONE, FWW_DL_TYPE,
138 NXM_OF_ETH_TYPE, "NXM_OF_ETH_TYPE",
139 OXM_OF_ETH_TYPE, "OXM_OF_ETH_TYPE",
143 MFF_VLAN_TCI, "vlan_tci", NULL,
144 MF_FIELD_SIZES(be16),
149 NXM_OF_VLAN_TCI, "NXM_OF_VLAN_TCI",
152 MFF_VLAN_VID, "dl_vlan", NULL,
153 sizeof(ovs_be16), 12,
159 OXM_OF_VLAN_VID, "OXM_OF_VLAN_VID",
161 MFF_VLAN_PCP, "dl_vlan_pcp", NULL,
168 OXM_OF_VLAN_PCP, "OXM_OF_VLAN_PCP",
176 MFF_IPV4_SRC, "ip_src", "nw_src",
177 MF_FIELD_SIZES(be32),
182 NXM_OF_IP_SRC, "NXM_OF_IP_SRC",
183 OXM_OF_IPV4_SRC, "OXM_OF_IPV4_SRC",
185 MFF_IPV4_DST, "ip_dst", "nw_dst",
186 MF_FIELD_SIZES(be32),
191 NXM_OF_IP_DST, "NXM_OF_IP_DST",
192 OXM_OF_IPV4_DST, "OXM_OF_IPV4_DST",
196 MFF_IPV6_SRC, "ipv6_src", NULL,
197 MF_FIELD_SIZES(ipv6),
202 NXM_NX_IPV6_SRC, "NXM_NX_IPV6_SRC",
203 OXM_OF_IPV6_SRC, "OXM_OF_IPV6_SRC",
205 MFF_IPV6_DST, "ipv6_dst", NULL,
206 MF_FIELD_SIZES(ipv6),
211 NXM_NX_IPV6_DST, "NXM_NX_IPV6_DST",
212 OXM_OF_IPV6_DST, "OXM_OF_IPV6_DST",
215 MFF_IPV6_LABEL, "ipv6_label", NULL,
217 MFM_NONE, FWW_IPV6_LABEL,
221 NXM_NX_IPV6_LABEL, "NXM_NX_IPV6_LABEL",
222 OXM_OF_IPV6_FLABEL, "OXM_OF_IPV6_FLABEL",
226 MFF_IP_PROTO, "nw_proto", NULL,
228 MFM_NONE, FWW_NW_PROTO,
232 NXM_OF_IP_PROTO, "NXM_OF_IP_PROTO",
233 OXM_OF_IP_PROTO, "OXM_OF_IP_PROTO",
235 MFF_IP_DSCP, "nw_tos", NULL,
237 MFM_NONE, FWW_NW_DSCP,
241 NXM_OF_IP_TOS, "NXM_OF_IP_TOS",
242 OXM_OF_IP_DSCP, "OXM_OF_IP_DSCP",
244 MFF_IP_ECN, "nw_ecn", NULL,
246 MFM_NONE, FWW_NW_ECN,
250 NXM_NX_IP_ECN, "NXM_NX_IP_ECN",
251 OXM_OF_IP_ECN, "OXM_OF_IP_ECN",
253 MFF_IP_TTL, "nw_ttl", NULL,
255 MFM_NONE, FWW_NW_TTL,
259 NXM_NX_IP_TTL, "NXM_NX_IP_TTL",
262 MFF_IP_FRAG, "ip_frag", NULL,
268 NXM_NX_IP_FRAG, "NXM_NX_IP_FRAG",
273 MFF_ARP_OP, "arp_op", NULL,
274 MF_FIELD_SIZES(be16),
275 MFM_NONE, FWW_NW_PROTO,
279 NXM_OF_ARP_OP, "NXM_OF_ARP_OP",
280 OXM_OF_ARP_OP, "OXM_OF_ARP_OP",
282 MFF_ARP_SPA, "arp_spa", NULL,
283 MF_FIELD_SIZES(be32),
288 NXM_OF_ARP_SPA, "NXM_OF_ARP_SPA",
289 OXM_OF_ARP_SPA, "OXM_OF_ARP_SPA",
291 MFF_ARP_TPA, "arp_tpa", NULL,
292 MF_FIELD_SIZES(be32),
297 NXM_OF_ARP_TPA, "NXM_OF_ARP_TPA",
298 OXM_OF_ARP_TPA, "OXM_OF_ARP_TPA",
300 MFF_ARP_SHA, "arp_sha", NULL,
302 MFM_NONE, FWW_ARP_SHA,
306 NXM_NX_ARP_SHA, "NXM_NX_ARP_SHA",
307 OXM_OF_ARP_SHA, "OXM_OF_ARP_SHA",
309 MFF_ARP_THA, "arp_tha", NULL,
311 MFM_NONE, FWW_ARP_THA,
315 NXM_NX_ARP_THA, "NXM_NX_ARP_THA",
316 OXM_OF_ARP_THA, "OXM_OF_ARP_THA",
324 MFF_TCP_SRC, "tcp_src", "tp_src",
325 MF_FIELD_SIZES(be16),
330 NXM_OF_TCP_SRC, "NXM_OF_TCP_SRC",
331 OXM_OF_TCP_SRC, "OXM_OF_TCP_SRC",
333 MFF_TCP_DST, "tcp_dst", "tp_dst",
334 MF_FIELD_SIZES(be16),
339 NXM_OF_TCP_DST, "NXM_OF_TCP_DST",
340 OXM_OF_TCP_DST, "OXM_OF_TCP_DST",
344 MFF_UDP_SRC, "udp_src", NULL,
345 MF_FIELD_SIZES(be16),
350 NXM_OF_UDP_SRC, "NXM_OF_UDP_SRC",
351 OXM_OF_UDP_SRC, "OXM_OF_UDP_SRC",
353 MFF_UDP_DST, "udp_dst", NULL,
354 MF_FIELD_SIZES(be16),
359 NXM_OF_UDP_DST, "NXM_OF_UDP_DST",
360 OXM_OF_UDP_DST, "OXM_OF_UDP_DST",
364 MFF_ICMPV4_TYPE, "icmp_type", NULL,
370 NXM_OF_ICMP_TYPE, "NXM_OF_ICMP_TYPE",
371 OXM_OF_ICMPV4_TYPE, "OXM_OF_ICMPV4_TYPE",
373 MFF_ICMPV4_CODE, "icmp_code", NULL,
379 NXM_OF_ICMP_CODE, "NXM_OF_ICMP_CODE",
380 OXM_OF_ICMPV4_CODE, "OXM_OF_ICMPV4_CODE",
384 MFF_ICMPV6_TYPE, "icmpv6_type", NULL,
390 NXM_NX_ICMPV6_TYPE, "NXM_NX_ICMPV6_TYPE",
391 OXM_OF_ICMPV6_TYPE, "OXM_OF_ICMPV6_TYPE",
393 MFF_ICMPV6_CODE, "icmpv6_code", NULL,
399 NXM_NX_ICMPV6_CODE, "NXM_NX_ICMPV6_CODE",
400 OXM_OF_ICMPV6_CODE, "OXM_OF_ICMPV6_CODE",
408 MFF_ND_TARGET, "nd_target", NULL,
409 MF_FIELD_SIZES(ipv6),
414 NXM_NX_ND_TARGET, "NXM_NX_ND_TARGET",
415 OXM_OF_IPV6_ND_TARGET, "OXM_OF_IPV6_ND_TARGET",
417 MFF_ND_SLL, "nd_sll", NULL,
419 MFM_NONE, FWW_ARP_SHA,
423 NXM_NX_ND_SLL, "NXM_NX_ND_SLL",
424 OXM_OF_IPV6_ND_SLL, "OXM_OF_IPV6_ND_SLL",
426 MFF_ND_TLL, "nd_tll", NULL,
428 MFM_NONE, FWW_ARP_THA,
432 NXM_NX_ND_TLL, "NXM_NX_ND_TLL",
433 OXM_OF_IPV6_ND_TLL, "OXM_OF_IPV6_ND_TLL",
438 struct hmap_node hmap_node;
440 const struct mf_field *mf;
443 static struct hmap all_nxm_fields = HMAP_INITIALIZER(&all_nxm_fields);
445 /* Rate limit for parse errors. These always indicate a bug in an OpenFlow
446 * controller and so there's not much point in showing a lot of them. */
447 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
449 /* Returns the field with the given 'id'. */
450 const struct mf_field *
451 mf_from_id(enum mf_field_id id)
453 assert((unsigned int) id < MFF_N_IDS);
454 return &mf_fields[id];
457 /* Returns the field with the given 'name', or a null pointer if no field has
459 const struct mf_field *
460 mf_from_name(const char *name)
462 static struct shash mf_by_name = SHASH_INITIALIZER(&mf_by_name);
464 if (shash_is_empty(&mf_by_name)) {
465 const struct mf_field *mf;
467 for (mf = mf_fields; mf < &mf_fields[MFF_N_IDS]; mf++) {
468 shash_add_once(&mf_by_name, mf->name, mf);
469 if (mf->extra_name) {
470 shash_add_once(&mf_by_name, mf->extra_name, mf);
475 return shash_find_data(&mf_by_name, name);
479 add_nxm_field(uint32_t nxm_header, const struct mf_field *mf)
483 f = xmalloc(sizeof *f);
484 hmap_insert(&all_nxm_fields, &f->hmap_node, hash_int(nxm_header, 0));
485 f->nxm_header = nxm_header;
492 const struct mf_field *mf;
494 for (mf = mf_fields; mf < &mf_fields[MFF_N_IDS]; mf++) {
495 if (mf->nxm_header) {
496 add_nxm_field(mf->nxm_header, mf);
497 if (mf->maskable != MFM_NONE) {
498 add_nxm_field(NXM_MAKE_WILD_HEADER(mf->nxm_header), mf);
504 /* Verify that the header values are unique. */
505 for (mf = mf_fields; mf < &mf_fields[MFF_N_IDS]; mf++) {
506 if (mf->nxm_header) {
507 assert(mf_from_nxm_header(mf->nxm_header) == mf);
508 if (mf->maskable != MFM_NONE) {
509 assert(mf_from_nxm_header(NXM_MAKE_WILD_HEADER(mf->nxm_header))
517 const struct mf_field *
518 mf_from_nxm_header(uint32_t header)
520 const struct nxm_field *f;
522 if (hmap_is_empty(&all_nxm_fields)) {
526 HMAP_FOR_EACH_IN_BUCKET (f, hmap_node, hash_int(header, 0),
528 if (f->nxm_header == header) {
536 /* Returns true if 'wc' wildcards all the bits in field 'mf', false if 'wc'
537 * specifies at least one bit in the field.
539 * The caller is responsible for ensuring that 'wc' corresponds to a flow that
540 * meets 'mf''s prerequisites. */
542 mf_is_all_wild(const struct mf_field *mf, const struct flow_wildcards *wc)
558 assert(mf->fww_bit != 0);
559 return (wc->wildcards & mf->fww_bit) != 0;
562 return !wc->tun_id_mask;
591 return !wc->reg_masks[mf->id - MFF_REG0];
594 return ((wc->wildcards & (FWW_ETH_MCAST | FWW_DL_DST))
595 == (FWW_ETH_MCAST | FWW_DL_DST));
598 return !wc->vlan_tci_mask;
600 return !(wc->vlan_tci_mask & htons(VLAN_VID_MASK));
602 return !(wc->vlan_tci_mask & htons(VLAN_PCP_MASK));
605 return !wc->nw_src_mask;
607 return !wc->nw_dst_mask;
610 return ipv6_mask_is_any(&wc->ipv6_src_mask);
612 return ipv6_mask_is_any(&wc->ipv6_dst_mask);
615 return ipv6_mask_is_any(&wc->nd_target_mask);
618 return !(wc->nw_frag_mask & FLOW_NW_FRAG_MASK);
621 return !wc->nw_src_mask;
623 return !wc->nw_dst_mask;
627 case MFF_ICMPV4_TYPE:
628 case MFF_ICMPV6_TYPE:
629 return !wc->tp_src_mask;
632 case MFF_ICMPV4_CODE:
633 case MFF_ICMPV6_CODE:
634 return !wc->tp_dst_mask;
642 /* Initializes 'mask' with the wildcard bit pattern for field 'mf' within 'wc'.
643 * Each bit in 'mask' will be set to 1 if the bit is significant for matching
644 * purposes, or to 0 if it is wildcarded.
646 * The caller is responsible for ensuring that 'wc' corresponds to a flow that
647 * meets 'mf''s prerequisites. */
649 mf_get_mask(const struct mf_field *mf, const struct flow_wildcards *wc,
650 union mf_value *mask)
666 assert(mf->fww_bit != 0);
667 memset(mask, wc->wildcards & mf->fww_bit ? 0x00 : 0xff, mf->n_bytes);
671 mask->be64 = wc->tun_id_mask;
701 mask->be32 = htonl(wc->reg_masks[mf->id - MFF_REG0]);
705 memcpy(mask->mac, flow_wildcards_to_dl_dst_mask(wc->wildcards),
710 mask->be16 = wc->vlan_tci_mask;
713 mask->be16 = wc->vlan_tci_mask & htons(VLAN_VID_MASK);
716 mask->u8 = vlan_tci_to_pcp(wc->vlan_tci_mask);
720 mask->be32 = wc->nw_src_mask;
723 mask->be32 = wc->nw_dst_mask;
727 mask->ipv6 = wc->ipv6_src_mask;
730 mask->ipv6 = wc->ipv6_dst_mask;
734 mask->ipv6 = wc->nd_target_mask;
738 mask->u8 = wc->nw_frag_mask & FLOW_NW_FRAG_MASK;
742 mask->be32 = wc->nw_src_mask;
745 mask->be32 = wc->nw_dst_mask;
750 mask->be16 = wc->tp_src_mask;
754 mask->be16 = wc->tp_dst_mask;
757 case MFF_ICMPV4_TYPE:
758 case MFF_ICMPV6_TYPE:
759 mask->u8 = ntohs(wc->tp_src_mask);
761 case MFF_ICMPV4_CODE:
762 case MFF_ICMPV6_CODE:
763 mask->u8 = ntohs(wc->tp_dst_mask);
772 /* Tests whether 'mask' is a valid wildcard bit pattern for 'mf'. Returns true
773 * if the mask is valid, false otherwise. */
775 mf_is_mask_valid(const struct mf_field *mf, const union mf_value *mask)
777 switch (mf->maskable) {
779 return (is_all_zeros((const uint8_t *) mask, mf->n_bytes) ||
780 is_all_ones((const uint8_t *) mask, mf->n_bytes));
786 return (mf->n_bytes == 4
787 ? ip_is_cidr(mask->be32)
788 : ipv6_is_cidr(&mask->ipv6));
791 return flow_wildcards_is_dl_dst_mask_valid(mask->mac);
798 is_ip_any(const struct flow *flow)
800 return (flow->dl_type == htons(ETH_TYPE_IP) ||
801 flow->dl_type == htons(ETH_TYPE_IPV6));
805 is_icmpv4(const struct flow *flow)
807 return (flow->dl_type == htons(ETH_TYPE_IP)
808 && flow->nw_proto == IPPROTO_ICMP);
812 is_icmpv6(const struct flow *flow)
814 return (flow->dl_type == htons(ETH_TYPE_IPV6)
815 && flow->nw_proto == IPPROTO_ICMPV6);
818 /* Returns true if 'flow' meets the prerequisites for 'mf', false otherwise. */
820 mf_are_prereqs_ok(const struct mf_field *mf, const struct flow *flow)
822 switch (mf->prereqs) {
827 return flow->dl_type == htons(ETH_TYPE_ARP);
829 return flow->dl_type == htons(ETH_TYPE_IP);
831 return flow->dl_type == htons(ETH_TYPE_IPV6);
833 return is_ip_any(flow);
836 return is_ip_any(flow) && flow->nw_proto == IPPROTO_TCP;
838 return is_ip_any(flow) && flow->nw_proto == IPPROTO_UDP;
840 return is_icmpv4(flow);
842 return is_icmpv6(flow);
845 return (is_icmpv6(flow)
846 && flow->tp_dst == htons(0)
847 && (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT) ||
848 flow->tp_src == htons(ND_NEIGHBOR_ADVERT)));
850 return (is_icmpv6(flow)
851 && flow->tp_dst == htons(0)
852 && (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT)));
854 return (is_icmpv6(flow)
855 && flow->tp_dst == htons(0)
856 && (flow->tp_src == htons(ND_NEIGHBOR_ADVERT)));
862 /* Returns true if 'value' may be a valid value *as part of a masked match*,
865 * A value is not rejected just because it is not valid for the field in
866 * question, but only if it doesn't make sense to test the bits in question at
867 * all. For example, the MFF_VLAN_TCI field will never have a nonzero value
868 * without the VLAN_CFI bit being set, but we can't reject those values because
869 * it is still legitimate to test just for those bits (see the documentation
870 * for NXM_OF_VLAN_TCI in nicira-ext.h). On the other hand, there is never a
871 * reason to set the low bit of MFF_IP_DSCP to 1, so we reject that. */
873 mf_is_value_valid(const struct mf_field *mf, const union mf_value *value)
923 case MFF_ICMPV4_TYPE:
924 case MFF_ICMPV4_CODE:
925 case MFF_ICMPV6_TYPE:
926 case MFF_ICMPV6_CODE:
933 return !(value->u8 & ~IP_DSCP_MASK);
935 return !(value->u8 & ~IP_ECN_MASK);
937 return !(value->u8 & ~FLOW_NW_FRAG_MASK);
940 return !(value->be16 & htons(0xff00));
943 return !(value->be16 & htons(VLAN_CFI | VLAN_PCP_MASK));
946 return !(value->u8 & ~7);
949 return !(value->be32 & ~htonl(IPV6_LABEL_MASK));
957 /* Copies the value of field 'mf' from 'flow' into 'value'. The caller is
958 * responsible for ensuring that 'flow' meets 'mf''s prerequisites. */
960 mf_get_value(const struct mf_field *mf, const struct flow *flow,
961 union mf_value *value)
965 value->be64 = flow->tun_id;
969 value->be16 = htons(flow->in_port);
999 value->be32 = htonl(flow->regs[mf->id - MFF_REG0]);
1003 memcpy(value->mac, flow->dl_src, ETH_ADDR_LEN);
1007 memcpy(value->mac, flow->dl_dst, ETH_ADDR_LEN);
1011 value->be16 = flow->dl_type;
1015 value->be16 = flow->vlan_tci;
1019 value->be16 = flow->vlan_tci & htons(VLAN_VID_MASK);
1023 value->u8 = vlan_tci_to_pcp(flow->vlan_tci);
1027 value->be32 = flow->nw_src;
1031 value->be32 = flow->nw_dst;
1035 value->ipv6 = flow->ipv6_src;
1039 value->ipv6 = flow->ipv6_dst;
1042 case MFF_IPV6_LABEL:
1043 value->be32 = flow->ipv6_label;
1047 value->u8 = flow->nw_proto;
1051 value->u8 = flow->nw_tos & IP_DSCP_MASK;
1055 value->u8 = flow->nw_tos & IP_ECN_MASK;
1059 value->u8 = flow->nw_ttl;
1063 value->u8 = flow->nw_frag;
1067 value->be16 = htons(flow->nw_proto);
1071 value->be32 = flow->nw_src;
1075 value->be32 = flow->nw_dst;
1080 memcpy(value->mac, flow->arp_sha, ETH_ADDR_LEN);
1085 memcpy(value->mac, flow->arp_tha, ETH_ADDR_LEN);
1089 value->be16 = flow->tp_src;
1093 value->be16 = flow->tp_dst;
1097 value->be16 = flow->tp_src;
1101 value->be16 = flow->tp_dst;
1104 case MFF_ICMPV4_TYPE:
1105 case MFF_ICMPV6_TYPE:
1106 value->u8 = ntohs(flow->tp_src);
1109 case MFF_ICMPV4_CODE:
1110 case MFF_ICMPV6_CODE:
1111 value->u8 = ntohs(flow->tp_dst);
1115 value->ipv6 = flow->nd_target;
1124 /* Makes 'rule' match field 'mf' exactly, with the value matched taken from
1125 * 'value'. The caller is responsible for ensuring that 'rule' meets 'mf''s
1128 mf_set_value(const struct mf_field *mf,
1129 const union mf_value *value, struct cls_rule *rule)
1133 cls_rule_set_tun_id(rule, value->be64);
1137 cls_rule_set_in_port(rule, ntohs(value->be16));
1168 cls_rule_set_reg(rule, mf->id - MFF_REG0, ntohl(value->be32));
1173 cls_rule_set_dl_src(rule, value->mac);
1177 cls_rule_set_dl_dst(rule, value->mac);
1181 cls_rule_set_dl_type(rule, value->be16);
1185 cls_rule_set_dl_tci(rule, value->be16);
1189 cls_rule_set_dl_vlan(rule, value->be16);
1193 cls_rule_set_dl_vlan_pcp(rule, value->u8);
1197 cls_rule_set_nw_src(rule, value->be32);
1201 cls_rule_set_nw_dst(rule, value->be32);
1205 cls_rule_set_ipv6_src(rule, &value->ipv6);
1209 cls_rule_set_ipv6_dst(rule, &value->ipv6);
1212 case MFF_IPV6_LABEL:
1213 cls_rule_set_ipv6_label(rule, value->be32);
1217 cls_rule_set_nw_proto(rule, value->u8);
1221 cls_rule_set_nw_dscp(rule, value->u8);
1225 cls_rule_set_nw_ecn(rule, value->u8);
1229 cls_rule_set_nw_ttl(rule, value->u8);
1233 cls_rule_set_nw_frag(rule, value->u8);
1237 cls_rule_set_nw_proto(rule, ntohs(value->be16));
1241 cls_rule_set_nw_src(rule, value->be32);
1245 cls_rule_set_nw_dst(rule, value->be32);
1250 cls_rule_set_arp_sha(rule, value->mac);
1255 cls_rule_set_arp_tha(rule, value->mac);
1259 cls_rule_set_tp_src(rule, value->be16);
1263 cls_rule_set_tp_dst(rule, value->be16);
1267 cls_rule_set_tp_src(rule, value->be16);
1271 cls_rule_set_tp_dst(rule, value->be16);
1274 case MFF_ICMPV4_TYPE:
1275 case MFF_ICMPV6_TYPE:
1276 cls_rule_set_icmp_type(rule, value->u8);
1279 case MFF_ICMPV4_CODE:
1280 case MFF_ICMPV6_CODE:
1281 cls_rule_set_icmp_code(rule, value->u8);
1285 cls_rule_set_nd_target(rule, &value->ipv6);
1294 /* Makes 'rule' match field 'mf' exactly, with the value matched taken from
1295 * 'value'. The caller is responsible for ensuring that 'rule' meets 'mf''s
1298 mf_set_flow_value(const struct mf_field *mf,
1299 const union mf_value *value, struct flow *flow)
1303 flow->tun_id = value->be64;
1307 flow->in_port = ntohs(value->be16);
1338 flow->regs[mf->id - MFF_REG0] = ntohl(value->be32);
1343 memcpy(flow->dl_src, value->mac, ETH_ADDR_LEN);
1347 memcpy(flow->dl_dst, value->mac, ETH_ADDR_LEN);
1351 flow->dl_type = value->be16;
1355 flow->vlan_tci = value->be16;
1359 flow_set_vlan_vid(flow, value->be16);
1363 flow_set_vlan_pcp(flow, value->u8);
1367 flow->nw_src = value->be32;
1371 flow->nw_dst = value->be32;
1375 flow->ipv6_src = value->ipv6;
1379 flow->ipv6_dst = value->ipv6;
1382 case MFF_IPV6_LABEL:
1383 flow->ipv6_label = value->be32 & ~htonl(IPV6_LABEL_MASK);
1387 flow->nw_proto = value->u8;
1391 flow->nw_tos &= ~IP_DSCP_MASK;
1392 flow->nw_tos |= value->u8 & IP_DSCP_MASK;
1396 flow->nw_tos &= ~IP_ECN_MASK;
1397 flow->nw_tos |= value->u8 & IP_ECN_MASK;
1401 flow->nw_ttl = value->u8;
1405 flow->nw_frag &= value->u8;
1409 flow->nw_proto = ntohs(value->be16);
1413 flow->nw_src = value->be32;
1417 flow->nw_dst = value->be32;
1422 memcpy(flow->arp_sha, value->mac, ETH_ADDR_LEN);
1427 memcpy(flow->arp_tha, value->mac, ETH_ADDR_LEN);
1432 flow->tp_src = value->be16;
1437 flow->tp_dst = value->be16;
1440 case MFF_ICMPV4_TYPE:
1441 case MFF_ICMPV6_TYPE:
1442 flow->tp_src = htons(value->u8);
1445 case MFF_ICMPV4_CODE:
1446 case MFF_ICMPV6_CODE:
1447 flow->tp_dst = htons(value->u8);
1451 flow->nd_target = value->ipv6;
1460 /* Makes 'rule' wildcard field 'mf'.
1462 * The caller is responsible for ensuring that 'rule' meets 'mf''s
1465 mf_set_wild(const struct mf_field *mf, struct cls_rule *rule)
1469 cls_rule_set_tun_id_masked(rule, htonll(0), htonll(0));
1473 rule->wc.wildcards |= FWW_IN_PORT;
1474 rule->flow.in_port = 0;
1479 cls_rule_set_reg_masked(rule, 0, 0, 0);
1484 cls_rule_set_reg_masked(rule, 1, 0, 0);
1489 cls_rule_set_reg_masked(rule, 2, 0, 0);
1494 cls_rule_set_reg_masked(rule, 3, 0, 0);
1499 cls_rule_set_reg_masked(rule, 4, 0, 0);
1504 cls_rule_set_reg_masked(rule, 5, 0, 0);
1509 cls_rule_set_reg_masked(rule, 6, 0, 0);
1514 cls_rule_set_reg_masked(rule, 7, 0, 0);
1522 rule->wc.wildcards |= FWW_DL_SRC;
1523 memset(rule->flow.dl_src, 0, sizeof rule->flow.dl_src);
1527 rule->wc.wildcards |= FWW_DL_DST | FWW_ETH_MCAST;
1528 memset(rule->flow.dl_dst, 0, sizeof rule->flow.dl_dst);
1532 rule->wc.wildcards |= FWW_DL_TYPE;
1533 rule->flow.dl_type = htons(0);
1537 cls_rule_set_dl_tci_masked(rule, htons(0), htons(0));
1541 cls_rule_set_any_vid(rule);
1545 cls_rule_set_any_pcp(rule);
1550 cls_rule_set_nw_src_masked(rule, htonl(0), htonl(0));
1555 cls_rule_set_nw_dst_masked(rule, htonl(0), htonl(0));
1559 memset(&rule->wc.ipv6_src_mask, 0, sizeof rule->wc.ipv6_src_mask);
1560 memset(&rule->flow.ipv6_src, 0, sizeof rule->flow.ipv6_src);
1564 memset(&rule->wc.ipv6_dst_mask, 0, sizeof rule->wc.ipv6_dst_mask);
1565 memset(&rule->flow.ipv6_dst, 0, sizeof rule->flow.ipv6_dst);
1568 case MFF_IPV6_LABEL:
1569 rule->wc.wildcards |= FWW_IPV6_LABEL;
1570 rule->flow.ipv6_label = 0;
1574 rule->wc.wildcards |= FWW_NW_PROTO;
1575 rule->flow.nw_proto = 0;
1579 rule->wc.wildcards |= FWW_NW_DSCP;
1580 rule->flow.nw_tos &= ~IP_DSCP_MASK;
1584 rule->wc.wildcards |= FWW_NW_ECN;
1585 rule->flow.nw_tos &= ~IP_ECN_MASK;
1589 rule->wc.wildcards |= FWW_NW_TTL;
1590 rule->flow.nw_ttl = 0;
1594 rule->wc.nw_frag_mask |= FLOW_NW_FRAG_MASK;
1595 rule->flow.nw_frag &= ~FLOW_NW_FRAG_MASK;
1599 rule->wc.wildcards |= FWW_NW_PROTO;
1600 rule->flow.nw_proto = 0;
1605 rule->wc.wildcards |= FWW_ARP_SHA;
1606 memset(rule->flow.arp_sha, 0, sizeof rule->flow.arp_sha);
1611 rule->wc.wildcards |= FWW_ARP_THA;
1612 memset(rule->flow.arp_tha, 0, sizeof rule->flow.arp_tha);
1617 case MFF_ICMPV4_TYPE:
1618 case MFF_ICMPV6_TYPE:
1619 rule->wc.tp_src_mask = htons(0);
1620 rule->flow.tp_src = htons(0);
1625 case MFF_ICMPV4_CODE:
1626 case MFF_ICMPV6_CODE:
1627 rule->wc.tp_dst_mask = htons(0);
1628 rule->flow.tp_dst = htons(0);
1632 memset(&rule->wc.nd_target_mask, 0, sizeof rule->wc.nd_target_mask);
1633 memset(&rule->flow.nd_target, 0, sizeof rule->flow.nd_target);
1642 /* Makes 'rule' match field 'mf' with the specified 'value' and 'mask'.
1643 * 'value' specifies a value to match and 'mask' specifies a wildcard pattern,
1644 * with a 1-bit indicating that the corresponding value bit must match and a
1645 * 0-bit indicating a don't-care.
1647 * If 'mask' is NULL or points to all-1-bits, then this call is equivalent to
1648 * mf_set_value(mf, value, rule). If 'mask' points to all-0-bits, then this
1649 * call is equivalent to mf_set_wild(mf, rule).
1651 * 'mask' must be a valid mask for 'mf' (see mf_is_mask_valid()). The caller
1652 * is responsible for ensuring that 'rule' meets 'mf''s prerequisites. */
1654 mf_set(const struct mf_field *mf,
1655 const union mf_value *value, const union mf_value *mask,
1656 struct cls_rule *rule)
1658 if (!mask || is_all_ones((const uint8_t *) mask, mf->n_bytes)) {
1659 mf_set_value(mf, value, rule);
1661 } else if (is_all_zeros((const uint8_t *) mask, mf->n_bytes)) {
1662 mf_set_wild(mf, rule);
1672 case MFF_IPV6_LABEL:
1680 case MFF_ICMPV4_TYPE:
1681 case MFF_ICMPV4_CODE:
1682 case MFF_ICMPV6_TYPE:
1683 case MFF_ICMPV6_CODE:
1689 cls_rule_set_tun_id_masked(rule, value->be64, mask->be64);
1719 cls_rule_set_reg_masked(rule, mf->id - MFF_REG0,
1720 ntohl(value->be32), ntohl(mask->be32));
1724 if (flow_wildcards_is_dl_dst_mask_valid(mask->mac)) {
1725 cls_rule_set_dl_dst_masked(rule, value->mac, mask->mac);
1730 cls_rule_set_dl_tci_masked(rule, value->be16, mask->be16);
1734 cls_rule_set_nw_src_masked(rule, value->be32, mask->be32);
1738 cls_rule_set_nw_dst_masked(rule, value->be32, mask->be32);
1742 cls_rule_set_ipv6_src_masked(rule, &value->ipv6, &mask->ipv6);
1746 cls_rule_set_ipv6_dst_masked(rule, &value->ipv6, &mask->ipv6);
1750 cls_rule_set_nd_target_masked(rule, &value->ipv6, &mask->ipv6);
1754 cls_rule_set_nw_frag_masked(rule, value->u8, mask->u8);
1758 cls_rule_set_nw_src_masked(rule, value->be32, mask->be32);
1762 cls_rule_set_nw_dst_masked(rule, value->be32, mask->be32);
1767 cls_rule_set_tp_src_masked(rule, value->be16, mask->be16);
1772 cls_rule_set_tp_dst_masked(rule, value->be16, mask->be16);
1782 mf_check__(const struct mf_subfield *sf, const struct flow *flow,
1786 VLOG_WARN_RL(&rl, "unknown %s field", type);
1787 } else if (!sf->n_bits) {
1788 VLOG_WARN_RL(&rl, "zero bit %s field %s", type, sf->field->name);
1789 } else if (sf->ofs >= sf->field->n_bits) {
1790 VLOG_WARN_RL(&rl, "bit offset %d exceeds %d-bit width of %s field %s",
1791 sf->ofs, sf->field->n_bits, type, sf->field->name);
1792 } else if (sf->ofs + sf->n_bits > sf->field->n_bits) {
1793 VLOG_WARN_RL(&rl, "bit offset %d and width %d exceeds %d-bit width "
1794 "of %s field %s", sf->ofs, sf->n_bits,
1795 sf->field->n_bits, type, sf->field->name);
1796 } else if (flow && !mf_are_prereqs_ok(sf->field, flow)) {
1797 VLOG_WARN_RL(&rl, "%s field %s lacks correct prerequisites",
1798 type, sf->field->name);
1803 return OFPERR_OFPBAC_BAD_ARGUMENT;
1806 /* Checks whether 'sf' is valid for reading a subfield out of 'flow'. Returns
1807 * 0 if so, otherwise an OpenFlow error code (e.g. as returned by
1810 mf_check_src(const struct mf_subfield *sf, const struct flow *flow)
1812 return mf_check__(sf, flow, "source");
1815 /* Checks whether 'sf' is valid for writing a subfield into 'flow'. Returns 0
1816 * if so, otherwise an OpenFlow error code (e.g. as returned by
1819 mf_check_dst(const struct mf_subfield *sf, const struct flow *flow)
1821 int error = mf_check__(sf, flow, "destination");
1822 if (!error && !sf->field->writable) {
1823 VLOG_WARN_RL(&rl, "destination field %s is not writable",
1825 return OFPERR_OFPBAC_BAD_ARGUMENT;
1830 /* Copies the value and wildcard bit pattern for 'mf' from 'rule' into the
1831 * 'value' and 'mask', respectively. */
1833 mf_get(const struct mf_field *mf, const struct cls_rule *rule,
1834 union mf_value *value, union mf_value *mask)
1836 mf_get_value(mf, &rule->flow, value);
1837 mf_get_mask(mf, &rule->wc, mask);
1840 /* Assigns a random value for field 'mf' to 'value'. */
1842 mf_random_value(const struct mf_field *mf, union mf_value *value)
1844 random_bytes(value, mf->n_bytes);
1894 case MFF_ICMPV4_TYPE:
1895 case MFF_ICMPV4_CODE:
1896 case MFF_ICMPV6_TYPE:
1897 case MFF_ICMPV6_CODE:
1903 case MFF_IPV6_LABEL:
1904 value->be32 &= ~htonl(IPV6_LABEL_MASK);
1908 value->u8 &= IP_DSCP_MASK;
1912 value->u8 &= IP_ECN_MASK;
1916 value->u8 &= FLOW_NW_FRAG_MASK;
1920 value->be16 &= htons(0xff);
1924 value->be16 &= htons(VLAN_VID_MASK);
1938 mf_from_integer_string(const struct mf_field *mf, const char *s,
1939 uint8_t *valuep, uint8_t *maskp)
1941 unsigned long long int integer, mask;
1946 integer = strtoull(s, &tail, 0);
1947 if (errno || (*tail != '\0' && *tail != '/')) {
1952 mask = strtoull(tail + 1, &tail, 0);
1953 if (errno || *tail != '\0') {
1960 for (i = mf->n_bytes - 1; i >= 0; i--) {
1961 valuep[i] = integer;
1967 return xasprintf("%s: value too large for %u-byte field %s",
1968 s, mf->n_bytes, mf->name);
1973 return xasprintf("%s: bad syntax for %s", s, mf->name);
1977 mf_from_ethernet_string(const struct mf_field *mf, const char *s,
1978 uint8_t mac[ETH_ADDR_LEN],
1979 uint8_t mask[ETH_ADDR_LEN])
1981 assert(mf->n_bytes == ETH_ADDR_LEN);
1983 switch (sscanf(s, ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT,
1984 ETH_ADDR_SCAN_ARGS(mac), ETH_ADDR_SCAN_ARGS(mask))){
1985 case ETH_ADDR_SCAN_COUNT * 2:
1988 case ETH_ADDR_SCAN_COUNT:
1989 memset(mask, 0xff, ETH_ADDR_LEN);
1993 return xasprintf("%s: invalid Ethernet address", s);
1998 mf_from_ipv4_string(const struct mf_field *mf, const char *s,
1999 ovs_be32 *ip, ovs_be32 *mask)
2003 assert(mf->n_bytes == sizeof *ip);
2005 if (sscanf(s, IP_SCAN_FMT"/"IP_SCAN_FMT,
2006 IP_SCAN_ARGS(ip), IP_SCAN_ARGS(mask)) == IP_SCAN_COUNT * 2) {
2008 } else if (sscanf(s, IP_SCAN_FMT"/%d",
2009 IP_SCAN_ARGS(ip), &prefix) == IP_SCAN_COUNT + 1) {
2010 if (prefix <= 0 || prefix > 32) {
2011 return xasprintf("%s: network prefix bits not between 1 and "
2013 } else if (prefix == 32) {
2014 *mask = htonl(UINT32_MAX);
2016 *mask = htonl(((1u << prefix) - 1) << (32 - prefix));
2018 } else if (sscanf(s, IP_SCAN_FMT, IP_SCAN_ARGS(ip)) == IP_SCAN_COUNT) {
2019 *mask = htonl(UINT32_MAX);
2021 return xasprintf("%s: invalid IP address", s);
2027 mf_from_ipv6_string(const struct mf_field *mf, const char *s,
2028 struct in6_addr *value, struct in6_addr *mask)
2030 char *str = xstrdup(s);
2031 char *save_ptr = NULL;
2032 const char *name, *netmask;
2035 assert(mf->n_bytes == sizeof *value);
2037 name = strtok_r(str, "/", &save_ptr);
2038 retval = name ? lookup_ipv6(name, value) : EINVAL;
2042 err = xasprintf("%s: could not convert to IPv6 address", str);
2048 netmask = strtok_r(NULL, "/", &save_ptr);
2050 int prefix = atoi(netmask);
2051 if (prefix <= 0 || prefix > 128) {
2053 return xasprintf("%s: prefix bits not between 1 and 128", s);
2055 *mask = ipv6_create_mask(prefix);
2058 *mask = in6addr_exact;
2066 mf_from_ofp_port_string(const struct mf_field *mf, const char *s,
2067 ovs_be16 *valuep, ovs_be16 *maskp)
2071 assert(mf->n_bytes == sizeof(ovs_be16));
2072 if (ofputil_port_from_string(s, &port)) {
2073 *valuep = htons(port);
2074 *maskp = htons(UINT16_MAX);
2077 return mf_from_integer_string(mf, s,
2078 (uint8_t *) valuep, (uint8_t *) maskp);
2082 struct frag_handling {
2088 static const struct frag_handling all_frags[] = {
2089 #define A FLOW_NW_FRAG_ANY
2090 #define L FLOW_NW_FRAG_LATER
2091 /* name mask value */
2094 { "first", A|L, A },
2095 { "later", A|L, A|L },
2100 { "not_later", L, 0 },
2107 mf_from_frag_string(const char *s, uint8_t *valuep, uint8_t *maskp)
2109 const struct frag_handling *h;
2111 for (h = all_frags; h < &all_frags[ARRAY_SIZE(all_frags)]; h++) {
2112 if (!strcasecmp(s, h->name)) {
2113 /* We force the upper bits of the mask on to make mf_parse_value()
2114 * happy (otherwise it will never think it's an exact match.) */
2115 *maskp = h->mask | ~FLOW_NW_FRAG_MASK;
2121 return xasprintf("%s: unknown fragment type (valid types are \"no\", "
2122 "\"yes\", \"first\", \"later\", \"not_first\"", s);
2125 /* Parses 's', a string value for field 'mf', into 'value' and 'mask'. Returns
2126 * NULL if successful, otherwise a malloc()'d string describing the error. */
2128 mf_parse(const struct mf_field *mf, const char *s,
2129 union mf_value *value, union mf_value *mask)
2131 if (!strcasecmp(s, "any") || !strcmp(s, "*")) {
2132 memset(value, 0, mf->n_bytes);
2133 memset(mask, 0, mf->n_bytes);
2137 switch (mf->string) {
2139 case MFS_HEXADECIMAL:
2140 return mf_from_integer_string(mf, s,
2141 (uint8_t *) value, (uint8_t *) mask);
2144 return mf_from_ethernet_string(mf, s, value->mac, mask->mac);
2147 return mf_from_ipv4_string(mf, s, &value->be32, &mask->be32);
2150 return mf_from_ipv6_string(mf, s, &value->ipv6, &mask->ipv6);
2153 return mf_from_ofp_port_string(mf, s, &value->be16, &mask->be16);
2156 return mf_from_frag_string(s, &value->u8, &mask->u8);
2161 /* Parses 's', a string value for field 'mf', into 'value'. Returns NULL if
2162 * successful, otherwise a malloc()'d string describing the error. */
2164 mf_parse_value(const struct mf_field *mf, const char *s, union mf_value *value)
2166 union mf_value mask;
2169 error = mf_parse(mf, s, value, &mask);
2174 if (!is_all_ones((const uint8_t *) &mask, mf->n_bytes)) {
2175 return xasprintf("%s: wildcards not allowed here", s);
2181 mf_format_integer_string(const struct mf_field *mf, const uint8_t *valuep,
2182 const uint8_t *maskp, struct ds *s)
2184 unsigned long long int integer;
2187 assert(mf->n_bytes <= 8);
2190 for (i = 0; i < mf->n_bytes; i++) {
2191 integer = (integer << 8) | valuep[i];
2193 if (mf->string == MFS_HEXADECIMAL) {
2194 ds_put_format(s, "%#llx", integer);
2196 ds_put_format(s, "%lld", integer);
2200 unsigned long long int mask;
2203 for (i = 0; i < mf->n_bytes; i++) {
2204 mask = (mask << 8) | maskp[i];
2207 /* I guess we could write the mask in decimal for MFS_DECIMAL but I'm
2208 * not sure that that a bit-mask written in decimal is ever easier to
2209 * understand than the same bit-mask written in hexadecimal. */
2210 ds_put_format(s, "/%#llx", mask);
2215 mf_format_frag_string(const uint8_t *valuep, const uint8_t *maskp,
2218 const struct frag_handling *h;
2219 uint8_t value = *valuep;
2220 uint8_t mask = *maskp;
2223 mask &= FLOW_NW_FRAG_MASK;
2225 for (h = all_frags; h < &all_frags[ARRAY_SIZE(all_frags)]; h++) {
2226 if (value == h->value && mask == h->mask) {
2227 ds_put_cstr(s, h->name);
2231 ds_put_cstr(s, "<error>");
2234 /* Appends to 's' a string representation of field 'mf' whose value is in
2235 * 'value' and 'mask'. 'mask' may be NULL to indicate an exact match. */
2237 mf_format(const struct mf_field *mf,
2238 const union mf_value *value, const union mf_value *mask,
2242 if (is_all_zeros((const uint8_t *) mask, mf->n_bytes)) {
2243 ds_put_cstr(s, "ANY");
2245 } else if (is_all_ones((const uint8_t *) mask, mf->n_bytes)) {
2250 switch (mf->string) {
2253 ofputil_format_port(ntohs(value->be16), s);
2258 case MFS_HEXADECIMAL:
2259 mf_format_integer_string(mf, (uint8_t *) value, (uint8_t *) mask, s);
2263 ds_put_format(s, ETH_ADDR_FMT, ETH_ADDR_ARGS(value->mac));
2265 ds_put_format(s, "/"ETH_ADDR_FMT, ETH_ADDR_ARGS(mask->mac));
2270 ip_format_masked(value->be32, mask ? mask->be32 : htonl(UINT32_MAX),
2275 print_ipv6_masked(s, &value->ipv6, mask ? &mask->ipv6 : NULL);
2279 mf_format_frag_string(&value->u8, &mask->u8, s);
2287 /* Makes subfield 'sf' within 'rule' exactly match the 'sf->n_bits'
2288 * least-significant bits in 'x'.
2290 * See mf_set_subfield() for an example.
2292 * The difference between this function and mf_set_subfield() is that the
2293 * latter function can only handle subfields up to 64 bits wide, whereas this
2294 * one handles the general case. On the other hand, mf_set_subfield() is
2295 * arguably easier to use. */
2297 mf_write_subfield(const struct mf_subfield *sf, const union mf_subvalue *x,
2298 struct cls_rule *rule)
2300 const struct mf_field *field = sf->field;
2301 union mf_value value, mask;
2303 mf_get(field, rule, &value, &mask);
2304 bitwise_copy(x, sizeof *x, 0, &value, field->n_bytes, sf->ofs, sf->n_bits);
2305 bitwise_one ( &mask, field->n_bytes, sf->ofs, sf->n_bits);
2306 mf_set(field, &value, &mask, rule);
2309 /* Makes subfield 'sf' within 'rule' exactly match the 'sf->n_bits'
2310 * least-significant bits of 'x'.
2312 * Example: suppose that 'sf->field' is originally the following 2-byte field
2315 * value == 0xe00a == 2#1110000000001010
2316 * mask == 0xfc3f == 2#1111110000111111
2318 * The call mf_set_subfield(sf, 0x55, 8, 7, rule), where sf->ofs == 8 and
2319 * sf->n_bits == 7 would have the following effect (note that 0x55 is
2322 * value == 0xd50a == 2#1101010100001010
2323 * mask == 0xff3f == 2#1111111100111111
2324 * ^^^^^^^ affected bits
2326 * The caller is responsible for ensuring that the result will be a valid
2327 * wildcard pattern for 'sf->field'. The caller is responsible for ensuring
2328 * that 'rule' meets 'sf->field''s prerequisites. */
2330 mf_set_subfield(const struct mf_subfield *sf, uint64_t x,
2331 struct cls_rule *rule)
2333 const struct mf_field *field = sf->field;
2334 unsigned int n_bits = sf->n_bits;
2335 unsigned int ofs = sf->ofs;
2337 if (ofs == 0 && field->n_bytes * 8 == n_bits) {
2338 union mf_value value;
2341 for (i = field->n_bytes - 1; i >= 0; i--) {
2342 ((uint8_t *) &value)[i] = x;
2345 mf_set_value(field, &value, rule);
2347 union mf_value value, mask;
2348 uint8_t *vp = (uint8_t *) &value;
2349 uint8_t *mp = (uint8_t *) &mask;
2351 mf_get(field, rule, &value, &mask);
2352 bitwise_put(x, vp, field->n_bytes, ofs, n_bits);
2353 bitwise_put(UINT64_MAX, mp, field->n_bytes, ofs, n_bits);
2354 mf_set(field, &value, &mask, rule);
2358 /* Similar to mf_set_subfield() but modifies only a flow, not a cls_rule. */
2360 mf_set_subfield_value(const struct mf_subfield *sf, uint64_t x,
2363 const struct mf_field *field = sf->field;
2364 unsigned int n_bits = sf->n_bits;
2365 unsigned int ofs = sf->ofs;
2366 union mf_value value;
2368 if (ofs == 0 && field->n_bytes * 8 == n_bits) {
2371 for (i = field->n_bytes - 1; i >= 0; i--) {
2372 ((uint8_t *) &value)[i] = x;
2375 mf_set_flow_value(field, &value, flow);
2377 mf_get_value(field, flow, &value);
2378 bitwise_put(x, &value, field->n_bytes, ofs, n_bits);
2379 mf_set_flow_value(field, &value, flow);
2383 /* Initializes 'x' to the value of 'sf' within 'flow'. 'sf' must be valid for
2384 * reading 'flow', e.g. as checked by mf_check_src(). */
2386 mf_read_subfield(const struct mf_subfield *sf, const struct flow *flow,
2387 union mf_subvalue *x)
2389 union mf_value value;
2391 mf_get_value(sf->field, flow, &value);
2393 memset(x, 0, sizeof *x);
2394 bitwise_copy(&value, sf->field->n_bytes, sf->ofs,
2399 /* Returns the value of 'sf' within 'flow'. 'sf' must be valid for reading
2400 * 'flow', e.g. as checked by mf_check_src() and sf->n_bits must be 64 or
2403 mf_get_subfield(const struct mf_subfield *sf, const struct flow *flow)
2405 union mf_value value;
2407 mf_get_value(sf->field, flow, &value);
2408 return bitwise_get(&value, sf->field->n_bytes, sf->ofs, sf->n_bits);
2411 /* Formats 'sf' into 's' in a format normally acceptable to
2412 * mf_parse_subfield(). (It won't be acceptable if sf->field is NULL or if
2413 * sf->field has no NXM name.) */
2415 mf_format_subfield(const struct mf_subfield *sf, struct ds *s)
2418 ds_put_cstr(s, "<unknown>");
2419 } else if (sf->field->nxm_name) {
2420 ds_put_cstr(s, sf->field->nxm_name);
2421 } else if (sf->field->nxm_header) {
2422 uint32_t header = sf->field->nxm_header;
2423 ds_put_format(s, "%d:%d", NXM_VENDOR(header), NXM_FIELD(header));
2425 ds_put_cstr(s, sf->field->name);
2428 if (sf->field && sf->ofs == 0 && sf->n_bits == sf->field->n_bits) {
2429 ds_put_cstr(s, "[]");
2430 } else if (sf->n_bits == 1) {
2431 ds_put_format(s, "[%d]", sf->ofs);
2433 ds_put_format(s, "[%d..%d]", sf->ofs, sf->ofs + sf->n_bits - 1);
2437 static const struct mf_field *
2438 mf_parse_subfield_name(const char *name, int name_len, bool *wild)
2442 *wild = name_len > 2 && !memcmp(&name[name_len - 2], "_W", 2);
2447 for (i = 0; i < MFF_N_IDS; i++) {
2448 const struct mf_field *mf = mf_from_id(i);
2451 && !strncmp(mf->nxm_name, name, name_len)
2452 && mf->nxm_name[name_len] == '\0') {
2460 /* Parses a subfield from the beginning of '*sp' into 'sf'. If successful,
2461 * returns NULL and advances '*sp' to the first byte following the parsed
2462 * string. On failure, returns a malloc()'d error message, does not modify
2463 * '*sp', and does not properly initialize 'sf'.
2465 * The syntax parsed from '*sp' takes the form "header[start..end]" where
2466 * 'header' is the name of an NXM field and 'start' and 'end' are (inclusive)
2467 * bit indexes. "..end" may be omitted to indicate a single bit. "start..end"
2468 * may both be omitted (the [] are still required) to indicate an entire
2471 mf_parse_subfield__(struct mf_subfield *sf, const char **sp)
2473 const struct mf_field *field;
2482 name_len = strcspn(s, "[");
2483 if (s[name_len] != '[') {
2484 return xasprintf("%s: missing [ looking for field name", *sp);
2487 field = mf_parse_subfield_name(name, name_len, &wild);
2489 return xasprintf("%s: unknown field `%.*s'", *sp, name_len, s);
2493 if (sscanf(s, "[%d..%d]", &start, &end) == 2) {
2494 /* Nothing to do. */
2495 } else if (sscanf(s, "[%d]", &start) == 1) {
2497 } else if (!strncmp(s, "[]", 2)) {
2499 end = field->n_bits - 1;
2501 return xasprintf("%s: syntax error expecting [] or [<bit>] or "
2502 "[<start>..<end>]", *sp);
2504 s = strchr(s, ']') + 1;
2507 return xasprintf("%s: starting bit %d is after ending bit %d",
2509 } else if (start >= field->n_bits) {
2510 return xasprintf("%s: starting bit %d is not valid because field is "
2511 "only %d bits wide", *sp, start, field->n_bits);
2512 } else if (end >= field->n_bits){
2513 return xasprintf("%s: ending bit %d is not valid because field is "
2514 "only %d bits wide", *sp, end, field->n_bits);
2519 sf->n_bits = end - start + 1;
2525 /* Parses a subfield from the beginning of 's' into 'sf'. Returns the first
2526 * byte in 's' following the parsed string.
2528 * Exits with an error message if 's' has incorrect syntax.
2530 * The syntax parsed from 's' takes the form "header[start..end]" where
2531 * 'header' is the name of an NXM field and 'start' and 'end' are (inclusive)
2532 * bit indexes. "..end" may be omitted to indicate a single bit. "start..end"
2533 * may both be omitted (the [] are still required) to indicate an entire
2536 mf_parse_subfield(struct mf_subfield *sf, const char *s)
2538 char *msg = mf_parse_subfield__(sf, &s);
2540 ovs_fatal(0, "%s", msg);