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),
293 MFM_NONE, FWW_TP_SRC,
297 NXM_OF_TCP_SRC, "NXM_OF_TCP_SRC",
299 MFF_TCP_DST, "tcp_dst", "tp_dst",
300 MF_FIELD_SIZES(be16),
301 MFM_NONE, FWW_TP_DST,
305 NXM_OF_TCP_DST, "NXM_OF_TCP_DST",
309 MFF_UDP_SRC, "udp_src", NULL,
310 MF_FIELD_SIZES(be16),
311 MFM_NONE, FWW_TP_SRC,
315 NXM_OF_UDP_SRC, "NXM_OF_UDP_SRC",
317 MFF_UDP_DST, "udp_dst", NULL,
318 MF_FIELD_SIZES(be16),
319 MFM_NONE, FWW_TP_DST,
323 NXM_OF_UDP_DST, "NXM_OF_UDP_DST",
327 MFF_ICMPV4_TYPE, "icmp_type", NULL,
329 MFM_NONE, FWW_TP_SRC,
333 NXM_OF_ICMP_TYPE, "NXM_OF_ICMP_TYPE",
335 MFF_ICMPV4_CODE, "icmp_code", NULL,
337 MFM_NONE, FWW_TP_DST,
341 NXM_OF_ICMP_CODE, "NXM_OF_ICMP_CODE",
345 MFF_ICMPV6_TYPE, "icmpv6_type", NULL,
347 MFM_NONE, FWW_TP_SRC,
351 NXM_NX_ICMPV6_TYPE, "NXM_NX_ICMPV6_TYPE",
353 MFF_ICMPV6_CODE, "icmpv6_code", NULL,
355 MFM_NONE, FWW_TP_DST,
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)
516 case MFF_ICMPV4_TYPE:
517 case MFF_ICMPV4_CODE:
518 case MFF_ICMPV6_TYPE:
519 case MFF_ICMPV6_CODE:
523 assert(mf->fww_bit != 0);
524 return (wc->wildcards & mf->fww_bit) != 0;
527 return !wc->tun_id_mask;
547 return !wc->reg_masks[mf->id - MFF_REG0];
550 return ((wc->wildcards & (FWW_ETH_MCAST | FWW_DL_DST))
551 == (FWW_ETH_MCAST | FWW_DL_DST));
554 return !wc->vlan_tci_mask;
556 return !(wc->vlan_tci_mask & htons(VLAN_VID_MASK));
558 return !(wc->vlan_tci_mask & htons(VLAN_PCP_MASK));
561 return !wc->nw_src_mask;
563 return !wc->nw_dst_mask;
566 return ipv6_mask_is_any(&wc->ipv6_src_mask);
568 return ipv6_mask_is_any(&wc->ipv6_dst_mask);
571 return !(wc->nw_frag_mask & FLOW_NW_FRAG_MASK);
574 return !wc->nw_src_mask;
576 return !wc->nw_dst_mask;
584 /* Initializes 'mask' with the wildcard bit pattern for field 'mf' within 'wc'.
585 * Each bit in 'mask' will be set to 1 if the bit is significant for matching
586 * purposes, or to 0 if it is wildcarded.
588 * The caller is responsible for ensuring that 'wc' corresponds to a flow that
589 * meets 'mf''s prerequisites. */
591 mf_get_mask(const struct mf_field *mf, const struct flow_wildcards *wc,
592 union mf_value *mask)
610 case MFF_ICMPV4_TYPE:
611 case MFF_ICMPV4_CODE:
612 case MFF_ICMPV6_TYPE:
613 case MFF_ICMPV6_CODE:
617 assert(mf->fww_bit != 0);
618 memset(mask, wc->wildcards & mf->fww_bit ? 0x00 : 0xff, mf->n_bytes);
622 mask->be64 = wc->tun_id_mask;
643 mask->be32 = htonl(wc->reg_masks[mf->id - MFF_REG0]);
647 memcpy(mask->mac, flow_wildcards_to_dl_dst_mask(wc->wildcards),
652 mask->be16 = wc->vlan_tci_mask;
655 mask->be16 = wc->vlan_tci_mask & htons(VLAN_VID_MASK);
658 mask->u8 = vlan_tci_to_pcp(wc->vlan_tci_mask);
662 mask->be32 = wc->nw_src_mask;
665 mask->be32 = wc->nw_dst_mask;
669 mask->ipv6 = wc->ipv6_src_mask;
672 mask->ipv6 = wc->ipv6_dst_mask;
676 mask->u8 = wc->nw_frag_mask & FLOW_NW_FRAG_MASK;
680 mask->be32 = wc->nw_src_mask;
683 mask->be32 = wc->nw_dst_mask;
692 /* Tests whether 'mask' is a valid wildcard bit pattern for 'mf'. Returns true
693 * if the mask is valid, false otherwise. */
695 mf_is_mask_valid(const struct mf_field *mf, const union mf_value *mask)
697 switch (mf->maskable) {
699 return (is_all_zeros((const uint8_t *) mask, mf->n_bytes) ||
700 is_all_ones((const uint8_t *) mask, mf->n_bytes));
706 return (mf->n_bytes == 4
707 ? ip_is_cidr(mask->be32)
708 : ipv6_is_cidr(&mask->ipv6));
711 return flow_wildcards_is_dl_dst_mask_valid(mask->mac);
718 is_ip_any(const struct flow *flow)
720 return (flow->dl_type == htons(ETH_TYPE_IP) ||
721 flow->dl_type == htons(ETH_TYPE_IPV6));
725 is_icmpv4(const struct flow *flow)
727 return (flow->dl_type == htons(ETH_TYPE_IP)
728 && flow->nw_proto == IPPROTO_ICMP);
732 is_icmpv6(const struct flow *flow)
734 return (flow->dl_type == htons(ETH_TYPE_IPV6)
735 && flow->nw_proto == IPPROTO_ICMPV6);
738 /* Returns true if 'flow' meets the prerequisites for 'mf', false otherwise. */
740 mf_are_prereqs_ok(const struct mf_field *mf, const struct flow *flow)
742 switch (mf->prereqs) {
747 return flow->dl_type == htons(ETH_TYPE_ARP);
749 return flow->dl_type == htons(ETH_TYPE_IP);
751 return flow->dl_type == htons(ETH_TYPE_IPV6);
753 return is_ip_any(flow);
756 return is_ip_any(flow) && flow->nw_proto == IPPROTO_TCP;
758 return is_ip_any(flow) && flow->nw_proto == IPPROTO_UDP;
760 return is_icmpv4(flow);
762 return is_icmpv6(flow);
765 return (is_icmpv6(flow)
766 && flow->tp_dst == htons(0)
767 && (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT) ||
768 flow->tp_src == htons(ND_NEIGHBOR_ADVERT)));
770 return (is_icmpv6(flow)
771 && flow->tp_dst == htons(0)
772 && (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT)));
774 return (is_icmpv6(flow)
775 && flow->tp_dst == htons(0)
776 && (flow->tp_src == htons(ND_NEIGHBOR_ADVERT)));
782 /* Returns true if 'value' may be a valid value *as part of a masked match*,
785 * A value is not rejected just because it is not valid for the field in
786 * question, but only if it doesn't make sense to test the bits in question at
787 * all. For example, the MFF_VLAN_TCI field will never have a nonzero value
788 * without the VLAN_CFI bit being set, but we can't reject those values because
789 * it is still legitimate to test just for those bits (see the documentation
790 * for NXM_OF_VLAN_TCI in nicira-ext.h). On the other hand, there is never a
791 * reason to set the low bit of MFF_IP_DSCP to 1, so we reject that. */
793 mf_is_value_valid(const struct mf_field *mf, const union mf_value *value)
834 case MFF_ICMPV4_TYPE:
835 case MFF_ICMPV4_CODE:
836 case MFF_ICMPV6_TYPE:
837 case MFF_ICMPV6_CODE:
844 return !(value->u8 & ~IP_DSCP_MASK);
846 return !(value->u8 & ~IP_ECN_MASK);
848 return !(value->u8 & ~FLOW_NW_FRAG_MASK);
851 return !(value->be16 & htons(0xff00));
854 return !(value->be16 & htons(VLAN_CFI | VLAN_PCP_MASK));
857 return !(value->u8 & ~7);
860 return !(value->be32 & ~htonl(IPV6_LABEL_MASK));
868 /* Copies the value of field 'mf' from 'flow' into 'value'. The caller is
869 * responsible for ensuring that 'flow' meets 'mf''s prerequisites. */
871 mf_get_value(const struct mf_field *mf, const struct flow *flow,
872 union mf_value *value)
876 value->be64 = flow->tun_id;
880 value->be16 = htons(flow->in_port);
901 value->be32 = htonl(flow->regs[mf->id - MFF_REG0]);
905 memcpy(value->mac, flow->dl_src, ETH_ADDR_LEN);
909 memcpy(value->mac, flow->dl_dst, ETH_ADDR_LEN);
913 value->be16 = flow->dl_type;
917 value->be16 = flow->vlan_tci;
921 value->be16 = flow->vlan_tci & htons(VLAN_VID_MASK);
925 value->u8 = vlan_tci_to_pcp(flow->vlan_tci);
929 value->be32 = flow->nw_src;
933 value->be32 = flow->nw_dst;
937 value->ipv6 = flow->ipv6_src;
941 value->ipv6 = flow->ipv6_dst;
945 value->be32 = flow->ipv6_label;
949 value->u8 = flow->nw_proto;
953 value->u8 = flow->nw_tos & IP_DSCP_MASK;
957 value->u8 = flow->nw_tos & IP_ECN_MASK;
961 value->u8 = flow->nw_ttl;
965 value->u8 = flow->nw_frag;
969 value->be16 = htons(flow->nw_proto);
973 value->be32 = flow->nw_src;
977 value->be32 = flow->nw_dst;
982 memcpy(value->mac, flow->arp_sha, ETH_ADDR_LEN);
987 memcpy(value->mac, flow->arp_tha, ETH_ADDR_LEN);
991 value->be16 = flow->tp_src;
995 value->be16 = flow->tp_dst;
999 value->be16 = flow->tp_src;
1003 value->be16 = flow->tp_dst;
1006 case MFF_ICMPV4_TYPE:
1007 case MFF_ICMPV6_TYPE:
1008 value->u8 = ntohs(flow->tp_src);
1011 case MFF_ICMPV4_CODE:
1012 case MFF_ICMPV6_CODE:
1013 value->u8 = ntohs(flow->tp_dst);
1017 value->ipv6 = flow->nd_target;
1026 /* Makes 'rule' match field 'mf' exactly, with the value matched taken from
1027 * 'value'. The caller is responsible for ensuring that 'rule' meets 'mf''s
1030 mf_set_value(const struct mf_field *mf,
1031 const union mf_value *value, struct cls_rule *rule)
1035 cls_rule_set_tun_id(rule, value->be64);
1039 cls_rule_set_in_port(rule, ntohs(value->be16));
1061 cls_rule_set_reg(rule, mf->id - MFF_REG0, ntohl(value->be32));
1066 cls_rule_set_dl_src(rule, value->mac);
1070 cls_rule_set_dl_dst(rule, value->mac);
1074 cls_rule_set_dl_type(rule, value->be16);
1078 cls_rule_set_dl_tci(rule, value->be16);
1082 cls_rule_set_dl_vlan(rule, value->be16);
1086 cls_rule_set_dl_vlan_pcp(rule, value->u8);
1090 cls_rule_set_nw_src(rule, value->be32);
1094 cls_rule_set_nw_dst(rule, value->be32);
1098 cls_rule_set_ipv6_src(rule, &value->ipv6);
1102 cls_rule_set_ipv6_dst(rule, &value->ipv6);
1105 case MFF_IPV6_LABEL:
1106 cls_rule_set_ipv6_label(rule, value->be32);
1110 cls_rule_set_nw_proto(rule, value->u8);
1114 cls_rule_set_nw_dscp(rule, value->u8);
1118 cls_rule_set_nw_ecn(rule, value->u8);
1122 cls_rule_set_nw_ttl(rule, value->u8);
1126 cls_rule_set_nw_frag(rule, value->u8);
1130 cls_rule_set_nw_proto(rule, ntohs(value->be16));
1134 cls_rule_set_nw_src(rule, value->be32);
1138 cls_rule_set_nw_dst(rule, value->be32);
1143 cls_rule_set_arp_sha(rule, value->mac);
1148 cls_rule_set_arp_tha(rule, value->mac);
1152 cls_rule_set_tp_src(rule, value->be16);
1156 cls_rule_set_tp_dst(rule, value->be16);
1160 cls_rule_set_tp_src(rule, value->be16);
1164 cls_rule_set_tp_dst(rule, value->be16);
1167 case MFF_ICMPV4_TYPE:
1168 case MFF_ICMPV6_TYPE:
1169 cls_rule_set_icmp_type(rule, value->u8);
1172 case MFF_ICMPV4_CODE:
1173 case MFF_ICMPV6_CODE:
1174 cls_rule_set_icmp_code(rule, value->u8);
1178 cls_rule_set_nd_target(rule, &value->ipv6);
1187 /* Makes 'rule' match field 'mf' exactly, with the value matched taken from
1188 * 'value'. The caller is responsible for ensuring that 'rule' meets 'mf''s
1191 mf_set_flow_value(const struct mf_field *mf,
1192 const union mf_value *value, struct flow *flow)
1196 flow->tun_id = value->be64;
1200 flow->in_port = ntohs(value->be16);
1222 flow->regs[mf->id - MFF_REG0] = ntohl(value->be32);
1227 memcpy(flow->dl_src, value->mac, ETH_ADDR_LEN);
1231 memcpy(flow->dl_src, value->mac, ETH_ADDR_LEN);
1235 flow->dl_type = value->be16;
1239 flow->vlan_tci = value->be16;
1243 flow_set_vlan_vid(flow, value->be16);
1247 flow_set_vlan_pcp(flow, value->u8);
1251 flow->nw_src = value->be32;
1255 flow->nw_dst = value->be32;
1259 flow->ipv6_src = value->ipv6;
1263 flow->ipv6_dst = value->ipv6;
1266 case MFF_IPV6_LABEL:
1267 flow->ipv6_label = value->be32 & ~htonl(IPV6_LABEL_MASK);
1271 flow->nw_proto = value->u8;
1275 flow->nw_tos &= ~IP_DSCP_MASK;
1276 flow->nw_tos |= value->u8 & IP_DSCP_MASK;
1280 flow->nw_tos &= ~IP_ECN_MASK;
1281 flow->nw_tos |= value->u8 & IP_ECN_MASK;
1285 flow->nw_ttl = value->u8;
1289 flow->nw_frag &= value->u8;
1293 flow->nw_proto = ntohs(value->be16);
1297 flow->nw_src = value->be32;
1301 flow->nw_dst = value->be32;
1306 memcpy(flow->arp_sha, value->mac, ETH_ADDR_LEN);
1311 memcpy(flow->arp_tha, value->mac, ETH_ADDR_LEN);
1316 flow->tp_src = value->be16;
1321 flow->tp_dst = value->be16;
1324 case MFF_ICMPV4_TYPE:
1325 case MFF_ICMPV6_TYPE:
1326 flow->tp_src = htons(value->u8);
1329 case MFF_ICMPV4_CODE:
1330 case MFF_ICMPV6_CODE:
1331 flow->tp_dst = htons(value->u8);
1335 flow->nd_target = value->ipv6;
1344 /* Makes 'rule' wildcard field 'mf'.
1346 * The caller is responsible for ensuring that 'rule' meets 'mf''s
1349 mf_set_wild(const struct mf_field *mf, struct cls_rule *rule)
1353 cls_rule_set_tun_id_masked(rule, htonll(0), htonll(0));
1357 rule->wc.wildcards |= FWW_IN_PORT;
1358 rule->flow.in_port = 0;
1363 cls_rule_set_reg_masked(rule, 0, 0, 0);
1368 cls_rule_set_reg_masked(rule, 1, 0, 0);
1373 cls_rule_set_reg_masked(rule, 2, 0, 0);
1378 cls_rule_set_reg_masked(rule, 3, 0, 0);
1383 cls_rule_set_reg_masked(rule, 4, 0, 0);
1391 rule->wc.wildcards |= FWW_DL_SRC;
1392 memset(rule->flow.dl_src, 0, sizeof rule->flow.dl_src);
1396 rule->wc.wildcards |= FWW_DL_DST | FWW_ETH_MCAST;
1397 memset(rule->flow.dl_dst, 0, sizeof rule->flow.dl_dst);
1401 rule->wc.wildcards |= FWW_DL_TYPE;
1402 rule->flow.dl_type = htons(0);
1406 cls_rule_set_dl_tci_masked(rule, htons(0), htons(0));
1410 cls_rule_set_any_vid(rule);
1414 cls_rule_set_any_pcp(rule);
1419 cls_rule_set_nw_src_masked(rule, htonl(0), htonl(0));
1424 cls_rule_set_nw_dst_masked(rule, htonl(0), htonl(0));
1428 memset(&rule->wc.ipv6_src_mask, 0, sizeof rule->wc.ipv6_src_mask);
1429 memset(&rule->flow.ipv6_src, 0, sizeof rule->flow.ipv6_src);
1433 memset(&rule->wc.ipv6_dst_mask, 0, sizeof rule->wc.ipv6_dst_mask);
1434 memset(&rule->flow.ipv6_dst, 0, sizeof rule->flow.ipv6_dst);
1437 case MFF_IPV6_LABEL:
1438 rule->wc.wildcards |= FWW_IPV6_LABEL;
1439 rule->flow.ipv6_label = 0;
1443 rule->wc.wildcards |= FWW_NW_PROTO;
1444 rule->flow.nw_proto = 0;
1448 rule->wc.wildcards |= FWW_NW_DSCP;
1449 rule->flow.nw_tos &= ~IP_DSCP_MASK;
1453 rule->wc.wildcards |= FWW_NW_ECN;
1454 rule->flow.nw_tos &= ~IP_ECN_MASK;
1458 rule->wc.wildcards |= FWW_NW_TTL;
1459 rule->flow.nw_ttl = 0;
1463 rule->wc.nw_frag_mask |= FLOW_NW_FRAG_MASK;
1464 rule->flow.nw_frag &= ~FLOW_NW_FRAG_MASK;
1468 rule->wc.wildcards |= FWW_NW_PROTO;
1469 rule->flow.nw_proto = 0;
1474 rule->wc.wildcards |= FWW_ARP_SHA;
1475 memset(rule->flow.arp_sha, 0, sizeof rule->flow.arp_sha);
1480 rule->wc.wildcards |= FWW_ARP_THA;
1481 memset(rule->flow.arp_tha, 0, sizeof rule->flow.arp_tha);
1486 case MFF_ICMPV4_TYPE:
1487 case MFF_ICMPV6_TYPE:
1488 rule->wc.wildcards |= FWW_TP_SRC;
1489 rule->flow.tp_src = htons(0);
1494 case MFF_ICMPV4_CODE:
1495 case MFF_ICMPV6_CODE:
1496 rule->wc.wildcards |= FWW_TP_DST;
1497 rule->flow.tp_dst = htons(0);
1501 rule->wc.wildcards |= FWW_ND_TARGET;
1502 memset(&rule->flow.nd_target, 0, sizeof rule->flow.nd_target);
1511 /* Makes 'rule' match field 'mf' with the specified 'value' and 'mask'.
1512 * 'value' specifies a value to match and 'mask' specifies a wildcard pattern,
1513 * with a 1-bit indicating that the corresponding value bit must match and a
1514 * 0-bit indicating a don't-care.
1516 * If 'mask' is NULL or points to all-1-bits, then this call is equivalent to
1517 * mf_set_value(mf, value, rule). If 'mask' points to all-0-bits, then this
1518 * call is equivalent to mf_set_wild(mf, rule).
1520 * 'mask' must be a valid mask for 'mf' (see mf_is_mask_valid()). The caller
1521 * is responsible for ensuring that 'rule' meets 'mf''s prerequisites. */
1523 mf_set(const struct mf_field *mf,
1524 const union mf_value *value, const union mf_value *mask,
1525 struct cls_rule *rule)
1527 if (!mask || is_all_ones((const uint8_t *) mask, mf->n_bytes)) {
1528 mf_set_value(mf, value, rule);
1530 } else if (is_all_zeros((const uint8_t *) mask, mf->n_bytes)) {
1531 mf_set_wild(mf, rule);
1541 case MFF_IPV6_LABEL:
1553 case MFF_ICMPV4_TYPE:
1554 case MFF_ICMPV4_CODE:
1555 case MFF_ICMPV6_TYPE:
1556 case MFF_ICMPV6_CODE:
1563 cls_rule_set_tun_id_masked(rule, value->be64, mask->be64);
1584 cls_rule_set_reg_masked(rule, mf->id - MFF_REG0,
1585 ntohl(value->be32), ntohl(mask->be32));
1589 if (flow_wildcards_is_dl_dst_mask_valid(mask->mac)) {
1590 cls_rule_set_dl_dst_masked(rule, value->mac, mask->mac);
1595 cls_rule_set_dl_tci_masked(rule, value->be16, mask->be16);
1599 cls_rule_set_nw_src_masked(rule, value->be32, mask->be32);
1603 cls_rule_set_nw_dst_masked(rule, value->be32, mask->be32);
1607 cls_rule_set_ipv6_src_masked(rule, &value->ipv6, &mask->ipv6);
1611 cls_rule_set_ipv6_dst_masked(rule, &value->ipv6, &mask->ipv6);
1615 cls_rule_set_nw_frag_masked(rule, value->u8, mask->u8);
1619 cls_rule_set_nw_src_masked(rule, value->be32, mask->be32);
1623 cls_rule_set_nw_dst_masked(rule, value->be32, mask->be32);
1633 mf_check__(const struct mf_subfield *sf, const struct flow *flow,
1637 VLOG_WARN_RL(&rl, "unknown %s field", type);
1638 } else if (!sf->n_bits) {
1639 VLOG_WARN_RL(&rl, "zero bit %s field %s", type, sf->field->name);
1640 } else if (sf->ofs >= sf->field->n_bits) {
1641 VLOG_WARN_RL(&rl, "bit offset %d exceeds %d-bit width of %s field %s",
1642 sf->ofs, sf->field->n_bits, type, sf->field->name);
1643 } else if (sf->ofs + sf->n_bits > sf->field->n_bits) {
1644 VLOG_WARN_RL(&rl, "bit offset %d and width %d exceeds %d-bit width "
1645 "of %s field %s", sf->ofs, sf->n_bits,
1646 sf->field->n_bits, type, sf->field->name);
1647 } else if (flow && !mf_are_prereqs_ok(sf->field, flow)) {
1648 VLOG_WARN_RL(&rl, "%s field %s lacks correct prerequisites",
1649 type, sf->field->name);
1654 return OFPERR_OFPBAC_BAD_ARGUMENT;
1657 /* Checks whether 'sf' is valid for reading a subfield out of 'flow'. Returns
1658 * 0 if so, otherwise an OpenFlow error code (e.g. as returned by
1661 mf_check_src(const struct mf_subfield *sf, const struct flow *flow)
1663 return mf_check__(sf, flow, "source");
1666 /* Checks whether 'sf' is valid for writing a subfield into 'flow'. Returns 0
1667 * if so, otherwise an OpenFlow error code (e.g. as returned by
1670 mf_check_dst(const struct mf_subfield *sf, const struct flow *flow)
1672 int error = mf_check__(sf, flow, "destination");
1673 if (!error && !sf->field->writable) {
1674 VLOG_WARN_RL(&rl, "destination field %s is not writable",
1676 return OFPERR_OFPBAC_BAD_ARGUMENT;
1681 /* Copies the value and wildcard bit pattern for 'mf' from 'rule' into the
1682 * 'value' and 'mask', respectively. */
1684 mf_get(const struct mf_field *mf, const struct cls_rule *rule,
1685 union mf_value *value, union mf_value *mask)
1687 mf_get_value(mf, &rule->flow, value);
1688 mf_get_mask(mf, &rule->wc, mask);
1691 /* Assigns a random value for field 'mf' to 'value'. */
1693 mf_random_value(const struct mf_field *mf, union mf_value *value)
1695 random_bytes(value, mf->n_bytes);
1736 case MFF_ICMPV4_TYPE:
1737 case MFF_ICMPV4_CODE:
1738 case MFF_ICMPV6_TYPE:
1739 case MFF_ICMPV6_CODE:
1745 case MFF_IPV6_LABEL:
1746 value->be32 &= ~htonl(IPV6_LABEL_MASK);
1750 value->u8 &= IP_DSCP_MASK;
1754 value->u8 &= IP_ECN_MASK;
1758 value->u8 &= FLOW_NW_FRAG_MASK;
1762 value->be16 &= htons(0xff);
1766 value->be16 &= htons(VLAN_VID_MASK);
1780 mf_from_integer_string(const struct mf_field *mf, const char *s,
1781 uint8_t *valuep, uint8_t *maskp)
1783 unsigned long long int integer, mask;
1788 integer = strtoull(s, &tail, 0);
1789 if (errno || (*tail != '\0' && *tail != '/')) {
1794 mask = strtoull(tail + 1, &tail, 0);
1795 if (errno || *tail != '\0') {
1802 for (i = mf->n_bytes - 1; i >= 0; i--) {
1803 valuep[i] = integer;
1809 return xasprintf("%s: value too large for %u-byte field %s",
1810 s, mf->n_bytes, mf->name);
1815 return xasprintf("%s: bad syntax for %s", s, mf->name);
1819 mf_from_ethernet_string(const struct mf_field *mf, const char *s,
1820 uint8_t mac[ETH_ADDR_LEN],
1821 uint8_t mask[ETH_ADDR_LEN])
1823 assert(mf->n_bytes == ETH_ADDR_LEN);
1825 switch (sscanf(s, ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT,
1826 ETH_ADDR_SCAN_ARGS(mac), ETH_ADDR_SCAN_ARGS(mask))){
1827 case ETH_ADDR_SCAN_COUNT * 2:
1830 case ETH_ADDR_SCAN_COUNT:
1831 memset(mask, 0xff, ETH_ADDR_LEN);
1835 return xasprintf("%s: invalid Ethernet address", s);
1840 mf_from_ipv4_string(const struct mf_field *mf, const char *s,
1841 ovs_be32 *ip, ovs_be32 *mask)
1845 assert(mf->n_bytes == sizeof *ip);
1847 if (sscanf(s, IP_SCAN_FMT"/"IP_SCAN_FMT,
1848 IP_SCAN_ARGS(ip), IP_SCAN_ARGS(mask)) == IP_SCAN_COUNT * 2) {
1850 } else if (sscanf(s, IP_SCAN_FMT"/%d",
1851 IP_SCAN_ARGS(ip), &prefix) == IP_SCAN_COUNT + 1) {
1852 if (prefix <= 0 || prefix > 32) {
1853 return xasprintf("%s: network prefix bits not between 1 and "
1855 } else if (prefix == 32) {
1856 *mask = htonl(UINT32_MAX);
1858 *mask = htonl(((1u << prefix) - 1) << (32 - prefix));
1860 } else if (sscanf(s, IP_SCAN_FMT, IP_SCAN_ARGS(ip)) == IP_SCAN_COUNT) {
1861 *mask = htonl(UINT32_MAX);
1863 return xasprintf("%s: invalid IP address", s);
1869 mf_from_ipv6_string(const struct mf_field *mf, const char *s,
1870 struct in6_addr *value, struct in6_addr *mask)
1872 char *str = xstrdup(s);
1873 char *save_ptr = NULL;
1874 const char *name, *netmask;
1877 assert(mf->n_bytes == sizeof *value);
1879 name = strtok_r(str, "/", &save_ptr);
1880 retval = name ? lookup_ipv6(name, value) : EINVAL;
1884 err = xasprintf("%s: could not convert to IPv6 address", str);
1890 netmask = strtok_r(NULL, "/", &save_ptr);
1892 int prefix = atoi(netmask);
1893 if (prefix <= 0 || prefix > 128) {
1895 return xasprintf("%s: prefix bits not between 1 and 128", s);
1897 *mask = ipv6_create_mask(prefix);
1900 *mask = in6addr_exact;
1908 mf_from_ofp_port_string(const struct mf_field *mf, const char *s,
1909 ovs_be16 *valuep, ovs_be16 *maskp)
1913 assert(mf->n_bytes == sizeof(ovs_be16));
1914 if (ofputil_port_from_string(s, &port)) {
1915 *valuep = htons(port);
1916 *maskp = htons(UINT16_MAX);
1919 return mf_from_integer_string(mf, s,
1920 (uint8_t *) valuep, (uint8_t *) maskp);
1924 struct frag_handling {
1930 static const struct frag_handling all_frags[] = {
1931 #define A FLOW_NW_FRAG_ANY
1932 #define L FLOW_NW_FRAG_LATER
1933 /* name mask value */
1936 { "first", A|L, A },
1937 { "later", A|L, A|L },
1942 { "not_later", L, 0 },
1949 mf_from_frag_string(const char *s, uint8_t *valuep, uint8_t *maskp)
1951 const struct frag_handling *h;
1953 for (h = all_frags; h < &all_frags[ARRAY_SIZE(all_frags)]; h++) {
1954 if (!strcasecmp(s, h->name)) {
1955 /* We force the upper bits of the mask on to make mf_parse_value()
1956 * happy (otherwise it will never think it's an exact match.) */
1957 *maskp = h->mask | ~FLOW_NW_FRAG_MASK;
1963 return xasprintf("%s: unknown fragment type (valid types are \"no\", "
1964 "\"yes\", \"first\", \"later\", \"not_first\"", s);
1967 /* Parses 's', a string value for field 'mf', into 'value' and 'mask'. Returns
1968 * NULL if successful, otherwise a malloc()'d string describing the error. */
1970 mf_parse(const struct mf_field *mf, const char *s,
1971 union mf_value *value, union mf_value *mask)
1973 if (!strcasecmp(s, "any") || !strcmp(s, "*")) {
1974 memset(value, 0, mf->n_bytes);
1975 memset(mask, 0, mf->n_bytes);
1979 switch (mf->string) {
1981 case MFS_HEXADECIMAL:
1982 return mf_from_integer_string(mf, s,
1983 (uint8_t *) value, (uint8_t *) mask);
1986 return mf_from_ethernet_string(mf, s, value->mac, mask->mac);
1989 return mf_from_ipv4_string(mf, s, &value->be32, &mask->be32);
1992 return mf_from_ipv6_string(mf, s, &value->ipv6, &mask->ipv6);
1995 return mf_from_ofp_port_string(mf, s, &value->be16, &mask->be16);
1998 return mf_from_frag_string(s, &value->u8, &mask->u8);
2003 /* Parses 's', a string value for field 'mf', into 'value'. Returns NULL if
2004 * successful, otherwise a malloc()'d string describing the error. */
2006 mf_parse_value(const struct mf_field *mf, const char *s, union mf_value *value)
2008 union mf_value mask;
2011 error = mf_parse(mf, s, value, &mask);
2016 if (!is_all_ones((const uint8_t *) &mask, mf->n_bytes)) {
2017 return xasprintf("%s: wildcards not allowed here", s);
2023 mf_format_integer_string(const struct mf_field *mf, const uint8_t *valuep,
2024 const uint8_t *maskp, struct ds *s)
2026 unsigned long long int integer;
2029 assert(mf->n_bytes <= 8);
2032 for (i = 0; i < mf->n_bytes; i++) {
2033 integer = (integer << 8) | valuep[i];
2035 if (mf->string == MFS_HEXADECIMAL) {
2036 ds_put_format(s, "%#llx", integer);
2038 ds_put_format(s, "%lld", integer);
2042 unsigned long long int mask;
2045 for (i = 0; i < mf->n_bytes; i++) {
2046 mask = (mask << 8) | maskp[i];
2049 /* I guess we could write the mask in decimal for MFS_DECIMAL but I'm
2050 * not sure that that a bit-mask written in decimal is ever easier to
2051 * understand than the same bit-mask written in hexadecimal. */
2052 ds_put_format(s, "/%#llx", mask);
2057 mf_format_frag_string(const uint8_t *valuep, const uint8_t *maskp,
2060 const struct frag_handling *h;
2061 uint8_t value = *valuep;
2062 uint8_t mask = *maskp;
2065 mask &= FLOW_NW_FRAG_MASK;
2067 for (h = all_frags; h < &all_frags[ARRAY_SIZE(all_frags)]; h++) {
2068 if (value == h->value && mask == h->mask) {
2069 ds_put_cstr(s, h->name);
2073 ds_put_cstr(s, "<error>");
2076 /* Appends to 's' a string representation of field 'mf' whose value is in
2077 * 'value' and 'mask'. 'mask' may be NULL to indicate an exact match. */
2079 mf_format(const struct mf_field *mf,
2080 const union mf_value *value, const union mf_value *mask,
2084 if (is_all_zeros((const uint8_t *) mask, mf->n_bytes)) {
2085 ds_put_cstr(s, "ANY");
2087 } else if (is_all_ones((const uint8_t *) mask, mf->n_bytes)) {
2092 switch (mf->string) {
2095 ofputil_format_port(ntohs(value->be16), s);
2100 case MFS_HEXADECIMAL:
2101 mf_format_integer_string(mf, (uint8_t *) value, (uint8_t *) mask, s);
2105 ds_put_format(s, ETH_ADDR_FMT, ETH_ADDR_ARGS(value->mac));
2107 ds_put_format(s, "/"ETH_ADDR_FMT, ETH_ADDR_ARGS(mask->mac));
2112 ip_format_masked(value->be32, mask ? mask->be32 : htonl(UINT32_MAX),
2117 print_ipv6_masked(s, &value->ipv6, mask ? &mask->ipv6 : NULL);
2121 mf_format_frag_string(&value->u8, &mask->u8, s);
2129 /* Makes a subfield starting at bit offset 'ofs' and continuing for 'n_bits' in
2130 * 'rule''s field 'mf' exactly match the 'n_bits' least-significant bits of
2133 * Example: suppose that 'mf' is originally the following 2-byte field in
2136 * value == 0xe00a == 2#1110000000001010
2137 * mask == 0xfc3f == 2#1111110000111111
2139 * The call mf_set_subfield(mf, 0x55, 8, 7, rule) would have the following
2140 * effect (note that 0x55 is 2#1010101):
2142 * value == 0xd50a == 2#1101010100001010
2143 * mask == 0xff3f == 2#1111111100111111
2145 * The caller is responsible for ensuring that the result will be a valid
2146 * wildcard pattern for 'mf'. The caller is responsible for ensuring that
2147 * 'rule' meets 'mf''s prerequisites. */
2149 mf_set_subfield(const struct mf_subfield *sf, uint64_t x,
2150 struct cls_rule *rule)
2152 const struct mf_field *field = sf->field;
2153 unsigned int n_bits = sf->n_bits;
2154 unsigned int ofs = sf->ofs;
2156 if (ofs == 0 && field->n_bytes * 8 == n_bits) {
2157 union mf_value value;
2160 for (i = field->n_bytes - 1; i >= 0; i--) {
2161 ((uint8_t *) &value)[i] = x;
2164 mf_set_value(field, &value, rule);
2166 union mf_value value, mask;
2167 uint8_t *vp = (uint8_t *) &value;
2168 uint8_t *mp = (uint8_t *) &mask;
2170 mf_get(field, rule, &value, &mask);
2171 bitwise_put(x, vp, field->n_bytes, ofs, n_bits);
2172 bitwise_put(UINT64_MAX, mp, field->n_bytes, ofs, n_bits);
2173 mf_set(field, &value, &mask, rule);
2177 /* Similar to mf_set_subfield() but modifies only a flow, not a cls_rule. */
2179 mf_set_subfield_value(const struct mf_subfield *sf, uint64_t x,
2182 const struct mf_field *field = sf->field;
2183 unsigned int n_bits = sf->n_bits;
2184 unsigned int ofs = sf->ofs;
2185 union mf_value value;
2187 if (ofs == 0 && field->n_bytes * 8 == n_bits) {
2190 for (i = field->n_bytes - 1; i >= 0; i--) {
2191 ((uint8_t *) &value)[i] = x;
2194 mf_set_flow_value(field, &value, flow);
2196 mf_get_value(field, flow, &value);
2197 bitwise_put(x, &value, field->n_bytes, ofs, n_bits);
2198 mf_set_flow_value(field, &value, flow);
2202 /* Returns the value of 'sf' within 'flow'. 'sf' must be valid for reading
2203 * 'flow', e.g. as checked by mf_check_src() and sf->n_bits must be 64 or
2206 mf_get_subfield(const struct mf_subfield *sf, const struct flow *flow)
2208 union mf_value value;
2210 mf_get_value(sf->field, flow, &value);
2211 return bitwise_get(&value, sf->field->n_bytes, sf->ofs, sf->n_bits);
2214 /* Formats 'sf' into 's' in a format normally acceptable to
2215 * mf_parse_subfield(). (It won't be acceptable if sf->field is NULL or if
2216 * sf->field has no NXM name.) */
2218 mf_format_subfield(const struct mf_subfield *sf, struct ds *s)
2221 ds_put_cstr(s, "<unknown>");
2222 } else if (sf->field->nxm_name) {
2223 ds_put_cstr(s, sf->field->nxm_name);
2224 } else if (sf->field->nxm_header) {
2225 uint32_t header = sf->field->nxm_header;
2226 ds_put_format(s, "%d:%d", NXM_VENDOR(header), NXM_FIELD(header));
2228 ds_put_cstr(s, sf->field->name);
2231 if (sf->ofs == 0 && sf->n_bits == sf->field->n_bits) {
2232 ds_put_cstr(s, "[]");
2233 } else if (sf->n_bits == 1) {
2234 ds_put_format(s, "[%d]", sf->ofs);
2236 ds_put_format(s, "[%d..%d]", sf->ofs, sf->ofs + sf->n_bits - 1);
2240 static const struct mf_field *
2241 mf_parse_subfield_name(const char *name, int name_len, bool *wild)
2245 *wild = name_len > 2 && !memcmp(&name[name_len - 2], "_W", 2);
2250 for (i = 0; i < MFF_N_IDS; i++) {
2251 const struct mf_field *mf = mf_from_id(i);
2254 && !strncmp(mf->nxm_name, name, name_len)
2255 && mf->nxm_name[name_len] == '\0') {
2263 /* Parses a subfield from the beginning of '*sp' into 'sf'. If successful,
2264 * returns NULL and advances '*sp' to the first byte following the parsed
2265 * string. On failure, returns a malloc()'d error message, does not modify
2266 * '*sp', and does not properly initialize 'sf'.
2268 * The syntax parsed from '*sp' takes the form "header[start..end]" where
2269 * 'header' is the name of an NXM field and 'start' and 'end' are (inclusive)
2270 * bit indexes. "..end" may be omitted to indicate a single bit. "start..end"
2271 * may both be omitted (the [] are still required) to indicate an entire
2274 mf_parse_subfield__(struct mf_subfield *sf, const char **sp)
2276 const struct mf_field *field;
2285 name_len = strcspn(s, "[");
2286 if (s[name_len] != '[') {
2287 return xasprintf("%s: missing [ looking for field name", *sp);
2290 field = mf_parse_subfield_name(name, name_len, &wild);
2292 return xasprintf("%s: unknown field `%.*s'", *sp, name_len, s);
2296 if (sscanf(s, "[%d..%d]", &start, &end) == 2) {
2297 /* Nothing to do. */
2298 } else if (sscanf(s, "[%d]", &start) == 1) {
2300 } else if (!strncmp(s, "[]", 2)) {
2302 end = field->n_bits - 1;
2304 return xasprintf("%s: syntax error expecting [] or [<bit>] or "
2305 "[<start>..<end>]", *sp);
2307 s = strchr(s, ']') + 1;
2310 return xasprintf("%s: starting bit %d is after ending bit %d",
2312 } else if (start >= field->n_bits) {
2313 return xasprintf("%s: starting bit %d is not valid because field is "
2314 "only %d bits wide", *sp, start, field->n_bits);
2315 } else if (end >= field->n_bits){
2316 return xasprintf("%s: ending bit %d is not valid because field is "
2317 "only %d bits wide", *sp, end, field->n_bits);
2322 sf->n_bits = end - start + 1;
2328 /* Parses a subfield from the beginning of 's' into 'sf'. Returns the first
2329 * byte in 's' following the parsed string.
2331 * Exits with an error message if 's' has incorrect syntax.
2333 * The syntax parsed from 's' takes the form "header[start..end]" where
2334 * 'header' is the name of an NXM field and 'start' and 'end' are (inclusive)
2335 * bit indexes. "..end" may be omitted to indicate a single bit. "start..end"
2336 * may both be omitted (the [] are still required) to indicate an entire
2339 mf_parse_subfield(struct mf_subfield *sf, const char *s)
2341 char *msg = mf_parse_subfield__(sf, &s);
2343 ovs_fatal(0, "%s", msg);