2 * Copyright (c) 2011, 2012 Nicira Networks.
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",
58 MFF_IN_PORT, "in_port", NULL,
60 MFM_NONE, FWW_IN_PORT,
64 NXM_OF_IN_PORT, "NXM_OF_IN_PORT",
67 #define REGISTER(IDX) \
69 MFF_REG##IDX, "reg" #IDX, NULL, \
70 MF_FIELD_SIZES(be32), \
102 MFF_ETH_SRC, "eth_src", "dl_src",
104 MFM_NONE, FWW_DL_SRC,
108 NXM_OF_ETH_SRC, "NXM_OF_ETH_SRC",
110 MFF_ETH_DST, "eth_dst", "dl_dst",
116 NXM_OF_ETH_DST, "NXM_OF_ETH_DST",
118 MFF_ETH_TYPE, "eth_type", "dl_type",
119 MF_FIELD_SIZES(be16),
120 MFM_NONE, FWW_DL_TYPE,
124 NXM_OF_ETH_TYPE, "NXM_OF_ETH_TYPE",
128 MFF_VLAN_TCI, "vlan_tci", NULL,
129 MF_FIELD_SIZES(be16),
134 NXM_OF_VLAN_TCI, "NXM_OF_VLAN_TCI",
136 MFF_VLAN_VID, "dl_vlan", NULL,
137 sizeof(ovs_be16), 12,
144 MFF_VLAN_PCP, "dl_vlan_pcp", NULL,
158 MFF_IPV4_SRC, "ip_src", "nw_src",
159 MF_FIELD_SIZES(be32),
164 NXM_OF_IP_SRC, "NXM_OF_IP_SRC",
166 MFF_IPV4_DST, "ip_dst", "nw_dst",
167 MF_FIELD_SIZES(be32),
172 NXM_OF_IP_DST, "NXM_OF_IP_DST",
176 MFF_IPV6_SRC, "ipv6_src", NULL,
177 MF_FIELD_SIZES(ipv6),
182 NXM_NX_IPV6_SRC, "NXM_NX_IPV6_SRC",
184 MFF_IPV6_DST, "ipv6_dst", NULL,
185 MF_FIELD_SIZES(ipv6),
190 NXM_NX_IPV6_DST, "NXM_NX_IPV6_DST",
193 MFF_IPV6_LABEL, "ipv6_label", NULL,
195 MFM_NONE, FWW_IPV6_LABEL,
199 NXM_NX_IPV6_LABEL, "NXM_NX_IPV6_LABEL",
203 MFF_IP_PROTO, "nw_proto", NULL,
205 MFM_NONE, FWW_NW_PROTO,
209 NXM_OF_IP_PROTO, "NXM_OF_IP_PROTO",
211 MFF_IP_DSCP, "nw_tos", NULL,
213 MFM_NONE, FWW_NW_DSCP,
217 NXM_OF_IP_TOS, "NXM_OF_IP_TOS"
219 MFF_IP_ECN, "nw_ecn", NULL,
221 MFM_NONE, FWW_NW_ECN,
225 NXM_NX_IP_ECN, "NXM_NX_IP_ECN",
227 MFF_IP_TTL, "nw_ttl", NULL,
229 MFM_NONE, FWW_NW_TTL,
233 NXM_NX_IP_TTL, "NXM_NX_IP_TTL"
235 MFF_IP_FRAG, "ip_frag", NULL,
241 NXM_NX_IP_FRAG, "NXM_NX_IP_FRAG"
245 MFF_ARP_OP, "arp_op", NULL,
246 MF_FIELD_SIZES(be16),
247 MFM_NONE, FWW_NW_PROTO,
251 NXM_OF_ARP_OP, "NXM_OF_ARP_OP",
253 MFF_ARP_SPA, "arp_spa", NULL,
254 MF_FIELD_SIZES(be32),
259 NXM_OF_ARP_SPA, "NXM_OF_ARP_SPA",
261 MFF_ARP_TPA, "arp_tpa", NULL,
262 MF_FIELD_SIZES(be32),
267 NXM_OF_ARP_TPA, "NXM_OF_ARP_TPA",
269 MFF_ARP_SHA, "arp_sha", NULL,
271 MFM_NONE, FWW_ARP_SHA,
275 NXM_NX_ARP_SHA, "NXM_NX_ARP_SHA",
277 MFF_ARP_THA, "arp_tha", NULL,
279 MFM_NONE, FWW_ARP_THA,
283 NXM_NX_ARP_THA, "NXM_NX_ARP_THA",
291 MFF_TCP_SRC, "tcp_src", "tp_src",
292 MF_FIELD_SIZES(be16),
297 NXM_OF_TCP_SRC, "NXM_OF_TCP_SRC",
299 MFF_TCP_DST, "tcp_dst", "tp_dst",
300 MF_FIELD_SIZES(be16),
305 NXM_OF_TCP_DST, "NXM_OF_TCP_DST",
309 MFF_UDP_SRC, "udp_src", NULL,
310 MF_FIELD_SIZES(be16),
315 NXM_OF_UDP_SRC, "NXM_OF_UDP_SRC",
317 MFF_UDP_DST, "udp_dst", NULL,
318 MF_FIELD_SIZES(be16),
323 NXM_OF_UDP_DST, "NXM_OF_UDP_DST",
327 MFF_ICMPV4_TYPE, "icmp_type", NULL,
333 NXM_OF_ICMP_TYPE, "NXM_OF_ICMP_TYPE",
335 MFF_ICMPV4_CODE, "icmp_code", NULL,
341 NXM_OF_ICMP_CODE, "NXM_OF_ICMP_CODE",
345 MFF_ICMPV6_TYPE, "icmpv6_type", NULL,
351 NXM_NX_ICMPV6_TYPE, "NXM_NX_ICMPV6_TYPE",
353 MFF_ICMPV6_CODE, "icmpv6_code", NULL,
359 NXM_NX_ICMPV6_CODE, "NXM_NX_ICMPV6_CODE",
367 MFF_ND_TARGET, "nd_target", NULL,
368 MF_FIELD_SIZES(ipv6),
369 MFM_NONE, FWW_ND_TARGET,
373 NXM_NX_ND_TARGET, "NXM_NX_ND_TARGET",
375 MFF_ND_SLL, "nd_sll", NULL,
377 MFM_NONE, FWW_ARP_SHA,
381 NXM_NX_ND_SLL, "NXM_NX_ND_SLL",
383 MFF_ND_TLL, "nd_tll", NULL,
385 MFM_NONE, FWW_ARP_THA,
389 NXM_NX_ND_TLL, "NXM_NX_ND_TLL",
394 struct hmap_node hmap_node;
396 const struct mf_field *mf;
399 static struct hmap all_nxm_fields = HMAP_INITIALIZER(&all_nxm_fields);
401 /* Rate limit for parse errors. These always indicate a bug in an OpenFlow
402 * controller and so there's not much point in showing a lot of them. */
403 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
405 /* Returns the field with the given 'id'. */
406 const struct mf_field *
407 mf_from_id(enum mf_field_id id)
409 assert((unsigned int) id < MFF_N_IDS);
410 return &mf_fields[id];
413 /* Returns the field with the given 'name', or a null pointer if no field has
415 const struct mf_field *
416 mf_from_name(const char *name)
418 static struct shash mf_by_name = SHASH_INITIALIZER(&mf_by_name);
420 if (shash_is_empty(&mf_by_name)) {
421 const struct mf_field *mf;
423 for (mf = mf_fields; mf < &mf_fields[MFF_N_IDS]; mf++) {
424 shash_add_once(&mf_by_name, mf->name, mf);
425 if (mf->extra_name) {
426 shash_add_once(&mf_by_name, mf->extra_name, mf);
431 return shash_find_data(&mf_by_name, name);
435 add_nxm_field(uint32_t nxm_header, const struct mf_field *mf)
439 f = xmalloc(sizeof *f);
440 hmap_insert(&all_nxm_fields, &f->hmap_node, hash_int(nxm_header, 0));
441 f->nxm_header = nxm_header;
448 const struct mf_field *mf;
450 for (mf = mf_fields; mf < &mf_fields[MFF_N_IDS]; mf++) {
451 if (mf->nxm_header) {
452 add_nxm_field(mf->nxm_header, mf);
453 if (mf->maskable != MFM_NONE) {
454 add_nxm_field(NXM_MAKE_WILD_HEADER(mf->nxm_header), mf);
460 /* Verify that the header values are unique. */
461 for (mf = mf_fields; mf < &mf_fields[MFF_N_IDS]; mf++) {
462 if (mf->nxm_header) {
463 assert(mf_from_nxm_header(mf->nxm_header) == mf);
464 if (mf->maskable != MFM_NONE) {
465 assert(mf_from_nxm_header(NXM_MAKE_WILD_HEADER(mf->nxm_header))
473 const struct mf_field *
474 mf_from_nxm_header(uint32_t header)
476 const struct nxm_field *f;
478 if (hmap_is_empty(&all_nxm_fields)) {
482 HMAP_FOR_EACH_IN_BUCKET (f, hmap_node, hash_int(header, 0),
484 if (f->nxm_header == header) {
492 /* Returns true if 'wc' wildcards all the bits in field 'mf', false if 'wc'
493 * specifies at least one bit in the field.
495 * The caller is responsible for ensuring that 'wc' corresponds to a flow that
496 * meets 'mf''s prerequisites. */
498 mf_is_all_wild(const struct mf_field *mf, const struct flow_wildcards *wc)
515 assert(mf->fww_bit != 0);
516 return (wc->wildcards & mf->fww_bit) != 0;
519 return !wc->tun_id_mask;
539 return !wc->reg_masks[mf->id - MFF_REG0];
542 return ((wc->wildcards & (FWW_ETH_MCAST | FWW_DL_DST))
543 == (FWW_ETH_MCAST | FWW_DL_DST));
546 return !wc->vlan_tci_mask;
548 return !(wc->vlan_tci_mask & htons(VLAN_VID_MASK));
550 return !(wc->vlan_tci_mask & htons(VLAN_PCP_MASK));
553 return !wc->nw_src_mask;
555 return !wc->nw_dst_mask;
558 return ipv6_mask_is_any(&wc->ipv6_src_mask);
560 return ipv6_mask_is_any(&wc->ipv6_dst_mask);
563 return !(wc->nw_frag_mask & FLOW_NW_FRAG_MASK);
566 return !wc->nw_src_mask;
568 return !wc->nw_dst_mask;
572 case MFF_ICMPV4_TYPE:
573 case MFF_ICMPV6_TYPE:
574 return !wc->tp_src_mask;
577 case MFF_ICMPV4_CODE:
578 case MFF_ICMPV6_CODE:
579 return !wc->tp_dst_mask;
587 /* Initializes 'mask' with the wildcard bit pattern for field 'mf' within 'wc'.
588 * Each bit in 'mask' will be set to 1 if the bit is significant for matching
589 * purposes, or to 0 if it is wildcarded.
591 * The caller is responsible for ensuring that 'wc' corresponds to a flow that
592 * meets 'mf''s prerequisites. */
594 mf_get_mask(const struct mf_field *mf, const struct flow_wildcards *wc,
595 union mf_value *mask)
612 assert(mf->fww_bit != 0);
613 memset(mask, wc->wildcards & mf->fww_bit ? 0x00 : 0xff, mf->n_bytes);
617 mask->be64 = wc->tun_id_mask;
638 mask->be32 = htonl(wc->reg_masks[mf->id - MFF_REG0]);
642 memcpy(mask->mac, flow_wildcards_to_dl_dst_mask(wc->wildcards),
647 mask->be16 = wc->vlan_tci_mask;
650 mask->be16 = wc->vlan_tci_mask & htons(VLAN_VID_MASK);
653 mask->u8 = vlan_tci_to_pcp(wc->vlan_tci_mask);
657 mask->be32 = wc->nw_src_mask;
660 mask->be32 = wc->nw_dst_mask;
664 mask->ipv6 = wc->ipv6_src_mask;
667 mask->ipv6 = wc->ipv6_dst_mask;
671 mask->u8 = wc->nw_frag_mask & FLOW_NW_FRAG_MASK;
675 mask->be32 = wc->nw_src_mask;
678 mask->be32 = wc->nw_dst_mask;
683 mask->be16 = wc->tp_src_mask;
687 mask->be16 = wc->tp_dst_mask;
690 case MFF_ICMPV4_TYPE:
691 case MFF_ICMPV6_TYPE:
692 mask->u8 = ntohs(wc->tp_src_mask);
694 case MFF_ICMPV4_CODE:
695 case MFF_ICMPV6_CODE:
696 mask->u8 = ntohs(wc->tp_dst_mask);
705 /* Tests whether 'mask' is a valid wildcard bit pattern for 'mf'. Returns true
706 * if the mask is valid, false otherwise. */
708 mf_is_mask_valid(const struct mf_field *mf, const union mf_value *mask)
710 switch (mf->maskable) {
712 return (is_all_zeros((const uint8_t *) mask, mf->n_bytes) ||
713 is_all_ones((const uint8_t *) mask, mf->n_bytes));
719 return (mf->n_bytes == 4
720 ? ip_is_cidr(mask->be32)
721 : ipv6_is_cidr(&mask->ipv6));
724 return flow_wildcards_is_dl_dst_mask_valid(mask->mac);
731 is_ip_any(const struct flow *flow)
733 return (flow->dl_type == htons(ETH_TYPE_IP) ||
734 flow->dl_type == htons(ETH_TYPE_IPV6));
738 is_icmpv4(const struct flow *flow)
740 return (flow->dl_type == htons(ETH_TYPE_IP)
741 && flow->nw_proto == IPPROTO_ICMP);
745 is_icmpv6(const struct flow *flow)
747 return (flow->dl_type == htons(ETH_TYPE_IPV6)
748 && flow->nw_proto == IPPROTO_ICMPV6);
751 /* Returns true if 'flow' meets the prerequisites for 'mf', false otherwise. */
753 mf_are_prereqs_ok(const struct mf_field *mf, const struct flow *flow)
755 switch (mf->prereqs) {
760 return flow->dl_type == htons(ETH_TYPE_ARP);
762 return flow->dl_type == htons(ETH_TYPE_IP);
764 return flow->dl_type == htons(ETH_TYPE_IPV6);
766 return is_ip_any(flow);
769 return is_ip_any(flow) && flow->nw_proto == IPPROTO_TCP;
771 return is_ip_any(flow) && flow->nw_proto == IPPROTO_UDP;
773 return is_icmpv4(flow);
775 return is_icmpv6(flow);
778 return (is_icmpv6(flow)
779 && flow->tp_dst == htons(0)
780 && (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT) ||
781 flow->tp_src == htons(ND_NEIGHBOR_ADVERT)));
783 return (is_icmpv6(flow)
784 && flow->tp_dst == htons(0)
785 && (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT)));
787 return (is_icmpv6(flow)
788 && flow->tp_dst == htons(0)
789 && (flow->tp_src == htons(ND_NEIGHBOR_ADVERT)));
795 /* Returns true if 'value' may be a valid value *as part of a masked match*,
798 * A value is not rejected just because it is not valid for the field in
799 * question, but only if it doesn't make sense to test the bits in question at
800 * all. For example, the MFF_VLAN_TCI field will never have a nonzero value
801 * without the VLAN_CFI bit being set, but we can't reject those values because
802 * it is still legitimate to test just for those bits (see the documentation
803 * for NXM_OF_VLAN_TCI in nicira-ext.h). On the other hand, there is never a
804 * reason to set the low bit of MFF_IP_DSCP to 1, so we reject that. */
806 mf_is_value_valid(const struct mf_field *mf, const union mf_value *value)
847 case MFF_ICMPV4_TYPE:
848 case MFF_ICMPV4_CODE:
849 case MFF_ICMPV6_TYPE:
850 case MFF_ICMPV6_CODE:
857 return !(value->u8 & ~IP_DSCP_MASK);
859 return !(value->u8 & ~IP_ECN_MASK);
861 return !(value->u8 & ~FLOW_NW_FRAG_MASK);
864 return !(value->be16 & htons(0xff00));
867 return !(value->be16 & htons(VLAN_CFI | VLAN_PCP_MASK));
870 return !(value->u8 & ~7);
873 return !(value->be32 & ~htonl(IPV6_LABEL_MASK));
881 /* Copies the value of field 'mf' from 'flow' into 'value'. The caller is
882 * responsible for ensuring that 'flow' meets 'mf''s prerequisites. */
884 mf_get_value(const struct mf_field *mf, const struct flow *flow,
885 union mf_value *value)
889 value->be64 = flow->tun_id;
893 value->be16 = htons(flow->in_port);
914 value->be32 = htonl(flow->regs[mf->id - MFF_REG0]);
918 memcpy(value->mac, flow->dl_src, ETH_ADDR_LEN);
922 memcpy(value->mac, flow->dl_dst, ETH_ADDR_LEN);
926 value->be16 = flow->dl_type;
930 value->be16 = flow->vlan_tci;
934 value->be16 = flow->vlan_tci & htons(VLAN_VID_MASK);
938 value->u8 = vlan_tci_to_pcp(flow->vlan_tci);
942 value->be32 = flow->nw_src;
946 value->be32 = flow->nw_dst;
950 value->ipv6 = flow->ipv6_src;
954 value->ipv6 = flow->ipv6_dst;
958 value->be32 = flow->ipv6_label;
962 value->u8 = flow->nw_proto;
966 value->u8 = flow->nw_tos & IP_DSCP_MASK;
970 value->u8 = flow->nw_tos & IP_ECN_MASK;
974 value->u8 = flow->nw_ttl;
978 value->u8 = flow->nw_frag;
982 value->be16 = htons(flow->nw_proto);
986 value->be32 = flow->nw_src;
990 value->be32 = flow->nw_dst;
995 memcpy(value->mac, flow->arp_sha, ETH_ADDR_LEN);
1000 memcpy(value->mac, flow->arp_tha, ETH_ADDR_LEN);
1004 value->be16 = flow->tp_src;
1008 value->be16 = flow->tp_dst;
1012 value->be16 = flow->tp_src;
1016 value->be16 = flow->tp_dst;
1019 case MFF_ICMPV4_TYPE:
1020 case MFF_ICMPV6_TYPE:
1021 value->u8 = ntohs(flow->tp_src);
1024 case MFF_ICMPV4_CODE:
1025 case MFF_ICMPV6_CODE:
1026 value->u8 = ntohs(flow->tp_dst);
1030 value->ipv6 = flow->nd_target;
1039 /* Makes 'rule' match field 'mf' exactly, with the value matched taken from
1040 * 'value'. The caller is responsible for ensuring that 'rule' meets 'mf''s
1043 mf_set_value(const struct mf_field *mf,
1044 const union mf_value *value, struct cls_rule *rule)
1048 cls_rule_set_tun_id(rule, value->be64);
1052 cls_rule_set_in_port(rule, ntohs(value->be16));
1074 cls_rule_set_reg(rule, mf->id - MFF_REG0, ntohl(value->be32));
1079 cls_rule_set_dl_src(rule, value->mac);
1083 cls_rule_set_dl_dst(rule, value->mac);
1087 cls_rule_set_dl_type(rule, value->be16);
1091 cls_rule_set_dl_tci(rule, value->be16);
1095 cls_rule_set_dl_vlan(rule, value->be16);
1099 cls_rule_set_dl_vlan_pcp(rule, value->u8);
1103 cls_rule_set_nw_src(rule, value->be32);
1107 cls_rule_set_nw_dst(rule, value->be32);
1111 cls_rule_set_ipv6_src(rule, &value->ipv6);
1115 cls_rule_set_ipv6_dst(rule, &value->ipv6);
1118 case MFF_IPV6_LABEL:
1119 cls_rule_set_ipv6_label(rule, value->be32);
1123 cls_rule_set_nw_proto(rule, value->u8);
1127 cls_rule_set_nw_dscp(rule, value->u8);
1131 cls_rule_set_nw_ecn(rule, value->u8);
1135 cls_rule_set_nw_ttl(rule, value->u8);
1139 cls_rule_set_nw_frag(rule, value->u8);
1143 cls_rule_set_nw_proto(rule, ntohs(value->be16));
1147 cls_rule_set_nw_src(rule, value->be32);
1151 cls_rule_set_nw_dst(rule, value->be32);
1156 cls_rule_set_arp_sha(rule, value->mac);
1161 cls_rule_set_arp_tha(rule, value->mac);
1165 cls_rule_set_tp_src(rule, value->be16);
1169 cls_rule_set_tp_dst(rule, value->be16);
1173 cls_rule_set_tp_src(rule, value->be16);
1177 cls_rule_set_tp_dst(rule, value->be16);
1180 case MFF_ICMPV4_TYPE:
1181 case MFF_ICMPV6_TYPE:
1182 cls_rule_set_icmp_type(rule, value->u8);
1185 case MFF_ICMPV4_CODE:
1186 case MFF_ICMPV6_CODE:
1187 cls_rule_set_icmp_code(rule, value->u8);
1191 cls_rule_set_nd_target(rule, &value->ipv6);
1200 /* Makes 'rule' match field 'mf' exactly, with the value matched taken from
1201 * 'value'. The caller is responsible for ensuring that 'rule' meets 'mf''s
1204 mf_set_flow_value(const struct mf_field *mf,
1205 const union mf_value *value, struct flow *flow)
1209 flow->tun_id = value->be64;
1213 flow->in_port = ntohs(value->be16);
1235 flow->regs[mf->id - MFF_REG0] = ntohl(value->be32);
1240 memcpy(flow->dl_src, value->mac, ETH_ADDR_LEN);
1244 memcpy(flow->dl_src, value->mac, ETH_ADDR_LEN);
1248 flow->dl_type = value->be16;
1252 flow->vlan_tci = value->be16;
1256 flow_set_vlan_vid(flow, value->be16);
1260 flow_set_vlan_pcp(flow, value->u8);
1264 flow->nw_src = value->be32;
1268 flow->nw_dst = value->be32;
1272 flow->ipv6_src = value->ipv6;
1276 flow->ipv6_dst = value->ipv6;
1279 case MFF_IPV6_LABEL:
1280 flow->ipv6_label = value->be32 & ~htonl(IPV6_LABEL_MASK);
1284 flow->nw_proto = value->u8;
1288 flow->nw_tos &= ~IP_DSCP_MASK;
1289 flow->nw_tos |= value->u8 & IP_DSCP_MASK;
1293 flow->nw_tos &= ~IP_ECN_MASK;
1294 flow->nw_tos |= value->u8 & IP_ECN_MASK;
1298 flow->nw_ttl = value->u8;
1302 flow->nw_frag &= value->u8;
1306 flow->nw_proto = ntohs(value->be16);
1310 flow->nw_src = value->be32;
1314 flow->nw_dst = value->be32;
1319 memcpy(flow->arp_sha, value->mac, ETH_ADDR_LEN);
1324 memcpy(flow->arp_tha, value->mac, ETH_ADDR_LEN);
1329 flow->tp_src = value->be16;
1334 flow->tp_dst = value->be16;
1337 case MFF_ICMPV4_TYPE:
1338 case MFF_ICMPV6_TYPE:
1339 flow->tp_src = htons(value->u8);
1342 case MFF_ICMPV4_CODE:
1343 case MFF_ICMPV6_CODE:
1344 flow->tp_dst = htons(value->u8);
1348 flow->nd_target = value->ipv6;
1357 /* Makes 'rule' wildcard field 'mf'.
1359 * The caller is responsible for ensuring that 'rule' meets 'mf''s
1362 mf_set_wild(const struct mf_field *mf, struct cls_rule *rule)
1366 cls_rule_set_tun_id_masked(rule, htonll(0), htonll(0));
1370 rule->wc.wildcards |= FWW_IN_PORT;
1371 rule->flow.in_port = 0;
1376 cls_rule_set_reg_masked(rule, 0, 0, 0);
1381 cls_rule_set_reg_masked(rule, 1, 0, 0);
1386 cls_rule_set_reg_masked(rule, 2, 0, 0);
1391 cls_rule_set_reg_masked(rule, 3, 0, 0);
1396 cls_rule_set_reg_masked(rule, 4, 0, 0);
1404 rule->wc.wildcards |= FWW_DL_SRC;
1405 memset(rule->flow.dl_src, 0, sizeof rule->flow.dl_src);
1409 rule->wc.wildcards |= FWW_DL_DST | FWW_ETH_MCAST;
1410 memset(rule->flow.dl_dst, 0, sizeof rule->flow.dl_dst);
1414 rule->wc.wildcards |= FWW_DL_TYPE;
1415 rule->flow.dl_type = htons(0);
1419 cls_rule_set_dl_tci_masked(rule, htons(0), htons(0));
1423 cls_rule_set_any_vid(rule);
1427 cls_rule_set_any_pcp(rule);
1432 cls_rule_set_nw_src_masked(rule, htonl(0), htonl(0));
1437 cls_rule_set_nw_dst_masked(rule, htonl(0), htonl(0));
1441 memset(&rule->wc.ipv6_src_mask, 0, sizeof rule->wc.ipv6_src_mask);
1442 memset(&rule->flow.ipv6_src, 0, sizeof rule->flow.ipv6_src);
1446 memset(&rule->wc.ipv6_dst_mask, 0, sizeof rule->wc.ipv6_dst_mask);
1447 memset(&rule->flow.ipv6_dst, 0, sizeof rule->flow.ipv6_dst);
1450 case MFF_IPV6_LABEL:
1451 rule->wc.wildcards |= FWW_IPV6_LABEL;
1452 rule->flow.ipv6_label = 0;
1456 rule->wc.wildcards |= FWW_NW_PROTO;
1457 rule->flow.nw_proto = 0;
1461 rule->wc.wildcards |= FWW_NW_DSCP;
1462 rule->flow.nw_tos &= ~IP_DSCP_MASK;
1466 rule->wc.wildcards |= FWW_NW_ECN;
1467 rule->flow.nw_tos &= ~IP_ECN_MASK;
1471 rule->wc.wildcards |= FWW_NW_TTL;
1472 rule->flow.nw_ttl = 0;
1476 rule->wc.nw_frag_mask |= FLOW_NW_FRAG_MASK;
1477 rule->flow.nw_frag &= ~FLOW_NW_FRAG_MASK;
1481 rule->wc.wildcards |= FWW_NW_PROTO;
1482 rule->flow.nw_proto = 0;
1487 rule->wc.wildcards |= FWW_ARP_SHA;
1488 memset(rule->flow.arp_sha, 0, sizeof rule->flow.arp_sha);
1493 rule->wc.wildcards |= FWW_ARP_THA;
1494 memset(rule->flow.arp_tha, 0, sizeof rule->flow.arp_tha);
1499 case MFF_ICMPV4_TYPE:
1500 case MFF_ICMPV6_TYPE:
1501 rule->wc.tp_src_mask = htons(0);
1502 rule->flow.tp_src = htons(0);
1507 case MFF_ICMPV4_CODE:
1508 case MFF_ICMPV6_CODE:
1509 rule->wc.tp_dst_mask = htons(0);
1510 rule->flow.tp_dst = htons(0);
1514 rule->wc.wildcards |= FWW_ND_TARGET;
1515 memset(&rule->flow.nd_target, 0, sizeof rule->flow.nd_target);
1524 /* Makes 'rule' match field 'mf' with the specified 'value' and 'mask'.
1525 * 'value' specifies a value to match and 'mask' specifies a wildcard pattern,
1526 * with a 1-bit indicating that the corresponding value bit must match and a
1527 * 0-bit indicating a don't-care.
1529 * If 'mask' is NULL or points to all-1-bits, then this call is equivalent to
1530 * mf_set_value(mf, value, rule). If 'mask' points to all-0-bits, then this
1531 * call is equivalent to mf_set_wild(mf, rule).
1533 * 'mask' must be a valid mask for 'mf' (see mf_is_mask_valid()). The caller
1534 * is responsible for ensuring that 'rule' meets 'mf''s prerequisites. */
1536 mf_set(const struct mf_field *mf,
1537 const union mf_value *value, const union mf_value *mask,
1538 struct cls_rule *rule)
1540 if (!mask || is_all_ones((const uint8_t *) mask, mf->n_bytes)) {
1541 mf_set_value(mf, value, rule);
1543 } else if (is_all_zeros((const uint8_t *) mask, mf->n_bytes)) {
1544 mf_set_wild(mf, rule);
1554 case MFF_IPV6_LABEL:
1562 case MFF_ICMPV4_TYPE:
1563 case MFF_ICMPV4_CODE:
1564 case MFF_ICMPV6_TYPE:
1565 case MFF_ICMPV6_CODE:
1572 cls_rule_set_tun_id_masked(rule, value->be64, mask->be64);
1593 cls_rule_set_reg_masked(rule, mf->id - MFF_REG0,
1594 ntohl(value->be32), ntohl(mask->be32));
1598 if (flow_wildcards_is_dl_dst_mask_valid(mask->mac)) {
1599 cls_rule_set_dl_dst_masked(rule, value->mac, mask->mac);
1604 cls_rule_set_dl_tci_masked(rule, value->be16, mask->be16);
1608 cls_rule_set_nw_src_masked(rule, value->be32, mask->be32);
1612 cls_rule_set_nw_dst_masked(rule, value->be32, mask->be32);
1616 cls_rule_set_ipv6_src_masked(rule, &value->ipv6, &mask->ipv6);
1620 cls_rule_set_ipv6_dst_masked(rule, &value->ipv6, &mask->ipv6);
1624 cls_rule_set_nw_frag_masked(rule, value->u8, mask->u8);
1628 cls_rule_set_nw_src_masked(rule, value->be32, mask->be32);
1632 cls_rule_set_nw_dst_masked(rule, value->be32, mask->be32);
1637 cls_rule_set_tp_src_masked(rule, value->be16, mask->be16);
1642 cls_rule_set_tp_dst_masked(rule, value->be16, mask->be16);
1652 mf_check__(const struct mf_subfield *sf, const struct flow *flow,
1656 VLOG_WARN_RL(&rl, "unknown %s field", type);
1657 } else if (!sf->n_bits) {
1658 VLOG_WARN_RL(&rl, "zero bit %s field %s", type, sf->field->name);
1659 } else if (sf->ofs >= sf->field->n_bits) {
1660 VLOG_WARN_RL(&rl, "bit offset %d exceeds %d-bit width of %s field %s",
1661 sf->ofs, sf->field->n_bits, type, sf->field->name);
1662 } else if (sf->ofs + sf->n_bits > sf->field->n_bits) {
1663 VLOG_WARN_RL(&rl, "bit offset %d and width %d exceeds %d-bit width "
1664 "of %s field %s", sf->ofs, sf->n_bits,
1665 sf->field->n_bits, type, sf->field->name);
1666 } else if (flow && !mf_are_prereqs_ok(sf->field, flow)) {
1667 VLOG_WARN_RL(&rl, "%s field %s lacks correct prerequisites",
1668 type, sf->field->name);
1673 return OFPERR_OFPBAC_BAD_ARGUMENT;
1676 /* Checks whether 'sf' is valid for reading a subfield out of 'flow'. Returns
1677 * 0 if so, otherwise an OpenFlow error code (e.g. as returned by
1680 mf_check_src(const struct mf_subfield *sf, const struct flow *flow)
1682 return mf_check__(sf, flow, "source");
1685 /* Checks whether 'sf' is valid for writing a subfield into 'flow'. Returns 0
1686 * if so, otherwise an OpenFlow error code (e.g. as returned by
1689 mf_check_dst(const struct mf_subfield *sf, const struct flow *flow)
1691 int error = mf_check__(sf, flow, "destination");
1692 if (!error && !sf->field->writable) {
1693 VLOG_WARN_RL(&rl, "destination field %s is not writable",
1695 return OFPERR_OFPBAC_BAD_ARGUMENT;
1700 /* Copies the value and wildcard bit pattern for 'mf' from 'rule' into the
1701 * 'value' and 'mask', respectively. */
1703 mf_get(const struct mf_field *mf, const struct cls_rule *rule,
1704 union mf_value *value, union mf_value *mask)
1706 mf_get_value(mf, &rule->flow, value);
1707 mf_get_mask(mf, &rule->wc, mask);
1710 /* Assigns a random value for field 'mf' to 'value'. */
1712 mf_random_value(const struct mf_field *mf, union mf_value *value)
1714 random_bytes(value, mf->n_bytes);
1755 case MFF_ICMPV4_TYPE:
1756 case MFF_ICMPV4_CODE:
1757 case MFF_ICMPV6_TYPE:
1758 case MFF_ICMPV6_CODE:
1764 case MFF_IPV6_LABEL:
1765 value->be32 &= ~htonl(IPV6_LABEL_MASK);
1769 value->u8 &= IP_DSCP_MASK;
1773 value->u8 &= IP_ECN_MASK;
1777 value->u8 &= FLOW_NW_FRAG_MASK;
1781 value->be16 &= htons(0xff);
1785 value->be16 &= htons(VLAN_VID_MASK);
1799 mf_from_integer_string(const struct mf_field *mf, const char *s,
1800 uint8_t *valuep, uint8_t *maskp)
1802 unsigned long long int integer, mask;
1807 integer = strtoull(s, &tail, 0);
1808 if (errno || (*tail != '\0' && *tail != '/')) {
1813 mask = strtoull(tail + 1, &tail, 0);
1814 if (errno || *tail != '\0') {
1821 for (i = mf->n_bytes - 1; i >= 0; i--) {
1822 valuep[i] = integer;
1828 return xasprintf("%s: value too large for %u-byte field %s",
1829 s, mf->n_bytes, mf->name);
1834 return xasprintf("%s: bad syntax for %s", s, mf->name);
1838 mf_from_ethernet_string(const struct mf_field *mf, const char *s,
1839 uint8_t mac[ETH_ADDR_LEN],
1840 uint8_t mask[ETH_ADDR_LEN])
1842 assert(mf->n_bytes == ETH_ADDR_LEN);
1844 switch (sscanf(s, ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT,
1845 ETH_ADDR_SCAN_ARGS(mac), ETH_ADDR_SCAN_ARGS(mask))){
1846 case ETH_ADDR_SCAN_COUNT * 2:
1849 case ETH_ADDR_SCAN_COUNT:
1850 memset(mask, 0xff, ETH_ADDR_LEN);
1854 return xasprintf("%s: invalid Ethernet address", s);
1859 mf_from_ipv4_string(const struct mf_field *mf, const char *s,
1860 ovs_be32 *ip, ovs_be32 *mask)
1864 assert(mf->n_bytes == sizeof *ip);
1866 if (sscanf(s, IP_SCAN_FMT"/"IP_SCAN_FMT,
1867 IP_SCAN_ARGS(ip), IP_SCAN_ARGS(mask)) == IP_SCAN_COUNT * 2) {
1869 } else if (sscanf(s, IP_SCAN_FMT"/%d",
1870 IP_SCAN_ARGS(ip), &prefix) == IP_SCAN_COUNT + 1) {
1871 if (prefix <= 0 || prefix > 32) {
1872 return xasprintf("%s: network prefix bits not between 1 and "
1874 } else if (prefix == 32) {
1875 *mask = htonl(UINT32_MAX);
1877 *mask = htonl(((1u << prefix) - 1) << (32 - prefix));
1879 } else if (sscanf(s, IP_SCAN_FMT, IP_SCAN_ARGS(ip)) == IP_SCAN_COUNT) {
1880 *mask = htonl(UINT32_MAX);
1882 return xasprintf("%s: invalid IP address", s);
1888 mf_from_ipv6_string(const struct mf_field *mf, const char *s,
1889 struct in6_addr *value, struct in6_addr *mask)
1891 char *str = xstrdup(s);
1892 char *save_ptr = NULL;
1893 const char *name, *netmask;
1896 assert(mf->n_bytes == sizeof *value);
1898 name = strtok_r(str, "/", &save_ptr);
1899 retval = name ? lookup_ipv6(name, value) : EINVAL;
1903 err = xasprintf("%s: could not convert to IPv6 address", str);
1909 netmask = strtok_r(NULL, "/", &save_ptr);
1911 int prefix = atoi(netmask);
1912 if (prefix <= 0 || prefix > 128) {
1914 return xasprintf("%s: prefix bits not between 1 and 128", s);
1916 *mask = ipv6_create_mask(prefix);
1919 *mask = in6addr_exact;
1927 mf_from_ofp_port_string(const struct mf_field *mf, const char *s,
1928 ovs_be16 *valuep, ovs_be16 *maskp)
1932 assert(mf->n_bytes == sizeof(ovs_be16));
1933 if (ofputil_port_from_string(s, &port)) {
1934 *valuep = htons(port);
1935 *maskp = htons(UINT16_MAX);
1938 return mf_from_integer_string(mf, s,
1939 (uint8_t *) valuep, (uint8_t *) maskp);
1943 struct frag_handling {
1949 static const struct frag_handling all_frags[] = {
1950 #define A FLOW_NW_FRAG_ANY
1951 #define L FLOW_NW_FRAG_LATER
1952 /* name mask value */
1955 { "first", A|L, A },
1956 { "later", A|L, A|L },
1961 { "not_later", L, 0 },
1968 mf_from_frag_string(const char *s, uint8_t *valuep, uint8_t *maskp)
1970 const struct frag_handling *h;
1972 for (h = all_frags; h < &all_frags[ARRAY_SIZE(all_frags)]; h++) {
1973 if (!strcasecmp(s, h->name)) {
1974 /* We force the upper bits of the mask on to make mf_parse_value()
1975 * happy (otherwise it will never think it's an exact match.) */
1976 *maskp = h->mask | ~FLOW_NW_FRAG_MASK;
1982 return xasprintf("%s: unknown fragment type (valid types are \"no\", "
1983 "\"yes\", \"first\", \"later\", \"not_first\"", s);
1986 /* Parses 's', a string value for field 'mf', into 'value' and 'mask'. Returns
1987 * NULL if successful, otherwise a malloc()'d string describing the error. */
1989 mf_parse(const struct mf_field *mf, const char *s,
1990 union mf_value *value, union mf_value *mask)
1992 if (!strcasecmp(s, "any") || !strcmp(s, "*")) {
1993 memset(value, 0, mf->n_bytes);
1994 memset(mask, 0, mf->n_bytes);
1998 switch (mf->string) {
2000 case MFS_HEXADECIMAL:
2001 return mf_from_integer_string(mf, s,
2002 (uint8_t *) value, (uint8_t *) mask);
2005 return mf_from_ethernet_string(mf, s, value->mac, mask->mac);
2008 return mf_from_ipv4_string(mf, s, &value->be32, &mask->be32);
2011 return mf_from_ipv6_string(mf, s, &value->ipv6, &mask->ipv6);
2014 return mf_from_ofp_port_string(mf, s, &value->be16, &mask->be16);
2017 return mf_from_frag_string(s, &value->u8, &mask->u8);
2022 /* Parses 's', a string value for field 'mf', into 'value'. Returns NULL if
2023 * successful, otherwise a malloc()'d string describing the error. */
2025 mf_parse_value(const struct mf_field *mf, const char *s, union mf_value *value)
2027 union mf_value mask;
2030 error = mf_parse(mf, s, value, &mask);
2035 if (!is_all_ones((const uint8_t *) &mask, mf->n_bytes)) {
2036 return xasprintf("%s: wildcards not allowed here", s);
2042 mf_format_integer_string(const struct mf_field *mf, const uint8_t *valuep,
2043 const uint8_t *maskp, struct ds *s)
2045 unsigned long long int integer;
2048 assert(mf->n_bytes <= 8);
2051 for (i = 0; i < mf->n_bytes; i++) {
2052 integer = (integer << 8) | valuep[i];
2054 if (mf->string == MFS_HEXADECIMAL) {
2055 ds_put_format(s, "%#llx", integer);
2057 ds_put_format(s, "%lld", integer);
2061 unsigned long long int mask;
2064 for (i = 0; i < mf->n_bytes; i++) {
2065 mask = (mask << 8) | maskp[i];
2068 /* I guess we could write the mask in decimal for MFS_DECIMAL but I'm
2069 * not sure that that a bit-mask written in decimal is ever easier to
2070 * understand than the same bit-mask written in hexadecimal. */
2071 ds_put_format(s, "/%#llx", mask);
2076 mf_format_frag_string(const uint8_t *valuep, const uint8_t *maskp,
2079 const struct frag_handling *h;
2080 uint8_t value = *valuep;
2081 uint8_t mask = *maskp;
2084 mask &= FLOW_NW_FRAG_MASK;
2086 for (h = all_frags; h < &all_frags[ARRAY_SIZE(all_frags)]; h++) {
2087 if (value == h->value && mask == h->mask) {
2088 ds_put_cstr(s, h->name);
2092 ds_put_cstr(s, "<error>");
2095 /* Appends to 's' a string representation of field 'mf' whose value is in
2096 * 'value' and 'mask'. 'mask' may be NULL to indicate an exact match. */
2098 mf_format(const struct mf_field *mf,
2099 const union mf_value *value, const union mf_value *mask,
2103 if (is_all_zeros((const uint8_t *) mask, mf->n_bytes)) {
2104 ds_put_cstr(s, "ANY");
2106 } else if (is_all_ones((const uint8_t *) mask, mf->n_bytes)) {
2111 switch (mf->string) {
2114 ofputil_format_port(ntohs(value->be16), s);
2119 case MFS_HEXADECIMAL:
2120 mf_format_integer_string(mf, (uint8_t *) value, (uint8_t *) mask, s);
2124 ds_put_format(s, ETH_ADDR_FMT, ETH_ADDR_ARGS(value->mac));
2126 ds_put_format(s, "/"ETH_ADDR_FMT, ETH_ADDR_ARGS(mask->mac));
2131 ip_format_masked(value->be32, mask ? mask->be32 : htonl(UINT32_MAX),
2136 print_ipv6_masked(s, &value->ipv6, mask ? &mask->ipv6 : NULL);
2140 mf_format_frag_string(&value->u8, &mask->u8, s);
2148 /* Makes a subfield starting at bit offset 'ofs' and continuing for 'n_bits' in
2149 * 'rule''s field 'mf' exactly match the 'n_bits' least-significant bits of
2152 * Example: suppose that 'mf' is originally the following 2-byte field in
2155 * value == 0xe00a == 2#1110000000001010
2156 * mask == 0xfc3f == 2#1111110000111111
2158 * The call mf_set_subfield(mf, 0x55, 8, 7, rule) would have the following
2159 * effect (note that 0x55 is 2#1010101):
2161 * value == 0xd50a == 2#1101010100001010
2162 * mask == 0xff3f == 2#1111111100111111
2164 * The caller is responsible for ensuring that the result will be a valid
2165 * wildcard pattern for 'mf'. The caller is responsible for ensuring that
2166 * 'rule' meets 'mf''s prerequisites. */
2168 mf_set_subfield(const struct mf_subfield *sf, uint64_t x,
2169 struct cls_rule *rule)
2171 const struct mf_field *field = sf->field;
2172 unsigned int n_bits = sf->n_bits;
2173 unsigned int ofs = sf->ofs;
2175 if (ofs == 0 && field->n_bytes * 8 == n_bits) {
2176 union mf_value value;
2179 for (i = field->n_bytes - 1; i >= 0; i--) {
2180 ((uint8_t *) &value)[i] = x;
2183 mf_set_value(field, &value, rule);
2185 union mf_value value, mask;
2186 uint8_t *vp = (uint8_t *) &value;
2187 uint8_t *mp = (uint8_t *) &mask;
2189 mf_get(field, rule, &value, &mask);
2190 bitwise_put(x, vp, field->n_bytes, ofs, n_bits);
2191 bitwise_put(UINT64_MAX, mp, field->n_bytes, ofs, n_bits);
2192 mf_set(field, &value, &mask, rule);
2196 /* Similar to mf_set_subfield() but modifies only a flow, not a cls_rule. */
2198 mf_set_subfield_value(const struct mf_subfield *sf, uint64_t x,
2201 const struct mf_field *field = sf->field;
2202 unsigned int n_bits = sf->n_bits;
2203 unsigned int ofs = sf->ofs;
2204 union mf_value value;
2206 if (ofs == 0 && field->n_bytes * 8 == n_bits) {
2209 for (i = field->n_bytes - 1; i >= 0; i--) {
2210 ((uint8_t *) &value)[i] = x;
2213 mf_set_flow_value(field, &value, flow);
2215 mf_get_value(field, flow, &value);
2216 bitwise_put(x, &value, field->n_bytes, ofs, n_bits);
2217 mf_set_flow_value(field, &value, flow);
2221 /* Returns the value of 'sf' within 'flow'. 'sf' must be valid for reading
2222 * 'flow', e.g. as checked by mf_check_src() and sf->n_bits must be 64 or
2225 mf_get_subfield(const struct mf_subfield *sf, const struct flow *flow)
2227 union mf_value value;
2229 mf_get_value(sf->field, flow, &value);
2230 return bitwise_get(&value, sf->field->n_bytes, sf->ofs, sf->n_bits);
2233 /* Formats 'sf' into 's' in a format normally acceptable to
2234 * mf_parse_subfield(). (It won't be acceptable if sf->field is NULL or if
2235 * sf->field has no NXM name.) */
2237 mf_format_subfield(const struct mf_subfield *sf, struct ds *s)
2240 ds_put_cstr(s, "<unknown>");
2241 } else if (sf->field->nxm_name) {
2242 ds_put_cstr(s, sf->field->nxm_name);
2243 } else if (sf->field->nxm_header) {
2244 uint32_t header = sf->field->nxm_header;
2245 ds_put_format(s, "%d:%d", NXM_VENDOR(header), NXM_FIELD(header));
2247 ds_put_cstr(s, sf->field->name);
2250 if (sf->ofs == 0 && sf->n_bits == sf->field->n_bits) {
2251 ds_put_cstr(s, "[]");
2252 } else if (sf->n_bits == 1) {
2253 ds_put_format(s, "[%d]", sf->ofs);
2255 ds_put_format(s, "[%d..%d]", sf->ofs, sf->ofs + sf->n_bits - 1);
2259 static const struct mf_field *
2260 mf_parse_subfield_name(const char *name, int name_len, bool *wild)
2264 *wild = name_len > 2 && !memcmp(&name[name_len - 2], "_W", 2);
2269 for (i = 0; i < MFF_N_IDS; i++) {
2270 const struct mf_field *mf = mf_from_id(i);
2273 && !strncmp(mf->nxm_name, name, name_len)
2274 && mf->nxm_name[name_len] == '\0') {
2282 /* Parses a subfield from the beginning of '*sp' into 'sf'. If successful,
2283 * returns NULL and advances '*sp' to the first byte following the parsed
2284 * string. On failure, returns a malloc()'d error message, does not modify
2285 * '*sp', and does not properly initialize 'sf'.
2287 * The syntax parsed from '*sp' takes the form "header[start..end]" where
2288 * 'header' is the name of an NXM field and 'start' and 'end' are (inclusive)
2289 * bit indexes. "..end" may be omitted to indicate a single bit. "start..end"
2290 * may both be omitted (the [] are still required) to indicate an entire
2293 mf_parse_subfield__(struct mf_subfield *sf, const char **sp)
2295 const struct mf_field *field;
2304 name_len = strcspn(s, "[");
2305 if (s[name_len] != '[') {
2306 return xasprintf("%s: missing [ looking for field name", *sp);
2309 field = mf_parse_subfield_name(name, name_len, &wild);
2311 return xasprintf("%s: unknown field `%.*s'", *sp, name_len, s);
2315 if (sscanf(s, "[%d..%d]", &start, &end) == 2) {
2316 /* Nothing to do. */
2317 } else if (sscanf(s, "[%d]", &start) == 1) {
2319 } else if (!strncmp(s, "[]", 2)) {
2321 end = field->n_bits - 1;
2323 return xasprintf("%s: syntax error expecting [] or [<bit>] or "
2324 "[<start>..<end>]", *sp);
2326 s = strchr(s, ']') + 1;
2329 return xasprintf("%s: starting bit %d is after ending bit %d",
2331 } else if (start >= field->n_bits) {
2332 return xasprintf("%s: starting bit %d is not valid because field is "
2333 "only %d bits wide", *sp, start, field->n_bits);
2334 } else if (end >= field->n_bits){
2335 return xasprintf("%s: ending bit %d is not valid because field is "
2336 "only %d bits wide", *sp, end, field->n_bits);
2341 sf->n_bits = end - start + 1;
2347 /* Parses a subfield from the beginning of 's' into 'sf'. Returns the first
2348 * byte in 's' following the parsed string.
2350 * Exits with an error message if 's' has incorrect syntax.
2352 * The syntax parsed from 's' takes the form "header[start..end]" where
2353 * 'header' is the name of an NXM field and 'start' and 'end' are (inclusive)
2354 * bit indexes. "..end" may be omitted to indicate a single bit. "start..end"
2355 * may both be omitted (the [] are still required) to indicate an entire
2358 mf_parse_subfield(struct mf_subfield *sf, const char *s)
2360 char *msg = mf_parse_subfield__(sf, &s);
2362 ovs_fatal(0, "%s", msg);