2 * Copyright (c) 2011 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"
33 #include "socket-util.h"
34 #include "unaligned.h"
36 #define MF_FIELD_SIZES(MEMBER) \
37 sizeof ((union mf_value *)0)->MEMBER, \
38 8 * sizeof ((union mf_value *)0)->MEMBER
40 static const struct mf_field mf_fields[MFF_N_IDS] = {
46 MFF_TUN_ID, "tun_id", NULL,
53 MFF_IN_PORT, "in_port", NULL,
55 MFM_NONE, FWW_IN_PORT,
61 #define REGISTER(IDX) \
63 MFF_REG##IDX, "reg" #IDX, NULL, \
64 MF_FIELD_SIZES(be32), \
91 MFF_ETH_SRC, "eth_src", "dl_src",
98 MFF_ETH_DST, "eth_dst", "dl_dst",
105 MFF_ETH_TYPE, "eth_type", "dl_type",
106 MF_FIELD_SIZES(be16),
107 MFM_NONE, FWW_DL_TYPE,
114 MFF_VLAN_TCI, "vlan_tci", NULL,
115 MF_FIELD_SIZES(be16),
121 MFF_VLAN_VID, "dl_vlan", NULL,
122 sizeof(ovs_be16), 12,
128 MFF_VLAN_PCP, "dl_vlan_pcp", NULL,
141 MFF_IPV4_SRC, "ip_src", "nw_src",
142 MF_FIELD_SIZES(be32),
148 MFF_IPV4_DST, "ip_dst", "nw_dst",
149 MF_FIELD_SIZES(be32),
157 MFF_IPV6_SRC, "ipv6_src", NULL,
158 MF_FIELD_SIZES(ipv6),
164 MFF_IPV6_DST, "ipv6_dst", NULL,
165 MF_FIELD_SIZES(ipv6),
173 MFF_IP_PROTO, "nw_proto", NULL,
175 MFM_NONE, FWW_NW_PROTO,
180 MFF_IP_TOS, "nw_tos", NULL,
182 MFM_NONE, FWW_NW_TOS,
189 MFF_ARP_OP, "arp_op", NULL,
190 MF_FIELD_SIZES(be16),
191 MFM_NONE, FWW_NW_PROTO,
196 MFF_ARP_SPA, "arp_spa", NULL,
197 MF_FIELD_SIZES(be32),
203 MFF_ARP_TPA, "arp_tpa", NULL,
204 MF_FIELD_SIZES(be32),
210 MFF_ARP_SHA, "arp_sha", NULL,
212 MFM_NONE, FWW_ARP_SHA,
217 MFF_ARP_THA, "arp_tha", NULL,
219 MFM_NONE, FWW_ARP_THA,
230 MFF_TCP_SRC, "tcp_src", "tp_src",
231 MF_FIELD_SIZES(be16),
232 MFM_NONE, FWW_TP_SRC,
237 MFF_TCP_DST, "tcp_dst", "tp_dst",
238 MF_FIELD_SIZES(be16),
239 MFM_NONE, FWW_TP_DST,
246 MFF_UDP_SRC, "udp_src", NULL,
247 MF_FIELD_SIZES(be16),
248 MFM_NONE, FWW_TP_SRC,
253 MFF_UDP_DST, "udp_dst", NULL,
254 MF_FIELD_SIZES(be16),
255 MFM_NONE, FWW_TP_DST,
262 MFF_ICMP_TYPE, "icmp_type", NULL,
264 MFM_NONE, FWW_TP_SRC,
269 MFF_ICMP_CODE, "icmp_code", NULL,
271 MFM_NONE, FWW_TP_SRC,
282 MFF_ND_TARGET, "nd_target", NULL,
283 MF_FIELD_SIZES(ipv6),
284 MFM_NONE, FWW_ND_TARGET,
289 MFF_ND_SLL, "nd_sll", NULL,
291 MFM_NONE, FWW_ARP_SHA,
296 MFF_ND_TLL, "nd_tll", NULL,
298 MFM_NONE, FWW_ARP_THA,
305 /* Returns the field with the given 'id'. */
306 const struct mf_field *
307 mf_from_id(enum mf_field_id id)
309 assert((unsigned int) id < MFF_N_IDS);
310 return &mf_fields[id];
313 /* Returns the field with the given 'name', or a null pointer if no field has
315 const struct mf_field *
316 mf_from_name(const char *name)
318 static struct shash mf_by_name = SHASH_INITIALIZER(&mf_by_name);
320 if (shash_is_empty(&mf_by_name)) {
321 const struct mf_field *mf;
323 for (mf = mf_fields; mf < &mf_fields[MFF_N_IDS]; mf++) {
324 shash_add_once(&mf_by_name, mf->name, mf);
325 if (mf->extra_name) {
326 shash_add_once(&mf_by_name, mf->extra_name, mf);
331 return shash_find_data(&mf_by_name, name);
334 /* Returns true if 'wc' wildcards all the bits in field 'mf', false if 'wc'
335 * specifies at least one bit in the field.
337 * The caller is responsible for ensuring that 'wc' corresponds to a flow that
338 * meets 'mf''s prerequisites. */
340 mf_is_all_wild(const struct mf_field *mf, const struct flow_wildcards *wc)
360 assert(mf->fww_bit != 0);
361 return (wc->wildcards & mf->fww_bit) != 0;
364 return !wc->tun_id_mask;
381 return !wc->reg_masks[mf->id - MFF_REG0];
384 return ((wc->wildcards & (FWW_ETH_MCAST | FWW_DL_DST))
385 == (FWW_ETH_MCAST | FWW_DL_DST));
388 return !wc->vlan_tci_mask;
390 return !(wc->vlan_tci_mask & htons(VLAN_VID_MASK));
392 return !(wc->vlan_tci_mask & htons(VLAN_PCP_MASK));
395 return !wc->nw_src_mask;
397 return !wc->nw_dst_mask;
400 return ipv6_mask_is_any(&wc->ipv6_src_mask);
402 return ipv6_mask_is_any(&wc->ipv6_dst_mask);
405 return !wc->nw_src_mask;
407 return !wc->nw_dst_mask;
415 /* Initializes 'mask' with the wildcard bit pattern for field 'mf' within 'wc'.
416 * Each bit in 'mask' will be set to 1 if the bit is significant for matching
417 * purposes, or to 0 if it is wildcarded.
419 * The caller is responsible for ensuring that 'wc' corresponds to a flow that
420 * meets 'mf''s prerequisites. */
422 mf_get_mask(const struct mf_field *mf, const struct flow_wildcards *wc,
423 union mf_value *mask)
443 assert(mf->fww_bit != 0);
444 memset(mask, wc->wildcards & mf->fww_bit ? 0x00 : 0xff, mf->n_bytes);
448 mask->be64 = wc->tun_id_mask;
466 mask->be32 = htonl(wc->reg_masks[mf->id - MFF_REG0]);
470 memcpy(mask->mac, flow_wildcards_to_dl_dst_mask(wc->wildcards),
475 mask->be16 = wc->vlan_tci_mask;
478 mask->be16 = wc->vlan_tci_mask & htons(VLAN_VID_MASK);
481 mask->u8 = vlan_tci_to_pcp(wc->vlan_tci_mask);
485 mask->be32 = wc->nw_src_mask;
488 mask->be32 = wc->nw_dst_mask;
492 mask->ipv6 = wc->ipv6_src_mask;
495 mask->ipv6 = wc->ipv6_dst_mask;
499 mask->be32 = wc->nw_src_mask;
502 mask->be32 = wc->nw_dst_mask;
511 /* Tests whether 'mask' is a valid wildcard bit pattern for 'mf'. Returns true
512 * if the mask is valid, false otherwise. */
514 mf_is_mask_valid(const struct mf_field *mf, const union mf_value *mask)
516 switch (mf->maskable) {
518 return (is_all_zeros((const uint8_t *) mask, mf->n_bytes) ||
519 is_all_ones((const uint8_t *) mask, mf->n_bytes));
525 return (mf->n_bytes == 4
526 ? ip_is_cidr(mask->be32)
527 : ipv6_is_cidr(&mask->ipv6));
530 return flow_wildcards_is_dl_dst_mask_valid(mask->mac);
537 is_ip_any(const struct flow *flow)
539 return (flow->dl_type == htons(ETH_TYPE_IP) ||
540 flow->dl_type == htons(ETH_TYPE_IPV6));
544 is_icmpv4(const struct flow *flow)
546 return (flow->dl_type == htons(ETH_TYPE_IP)
547 && flow->nw_proto == IPPROTO_ICMP);
551 is_icmpv6(const struct flow *flow)
553 return (flow->dl_type == htons(ETH_TYPE_IPV6)
554 && flow->nw_proto == IPPROTO_ICMPV6);
557 /* Returns true if 'flow' meets the prerequisites for 'mf', false otherwise. */
559 mf_are_prereqs_ok(const struct mf_field *mf, const struct flow *flow)
561 switch (mf->prereqs) {
566 return flow->dl_type == htons(ETH_TYPE_ARP);
568 return flow->dl_type == htons(ETH_TYPE_IP);
570 return flow->dl_type == htons(ETH_TYPE_IPV6);
572 return is_ip_any(flow);
575 return is_ip_any(flow) && flow->nw_proto == IPPROTO_TCP;
577 return is_ip_any(flow) && flow->nw_proto == IPPROTO_UDP;
579 return is_icmpv6(flow);
581 return is_icmpv4(flow) || is_icmpv6(flow);
584 return (is_icmpv6(flow)
585 && flow->icmp_code == htons(0)
586 && (flow->icmp_type == htons(ND_NEIGHBOR_SOLICIT) ||
587 flow->icmp_type == htons(ND_NEIGHBOR_ADVERT)));
589 return (is_icmpv6(flow)
590 && flow->icmp_code == htons(0)
591 && (flow->icmp_type == htons(ND_NEIGHBOR_SOLICIT)));
593 return (is_icmpv6(flow)
594 && flow->icmp_code == htons(0)
595 && (flow->icmp_type == htons(ND_NEIGHBOR_ADVERT)));
601 /* Returns true if 'value' may be a valid value *as part of a masked match*,
604 * A value is not rejected just because it is not valid for the field in
605 * question, but only if it doesn't make sense to test the bits in question at
606 * all. For example, the MFF_VLAN_TCI field will never have a nonzero value
607 * without the VLAN_CFI bit being set, but we can't reject those values because
608 * it is still legitimate to test just for those bits (see the documentation
609 * for NXM_OF_VLAN_TCI in nicira-ext.h). On the other hand, there is never a
610 * reason to set the low bit of MFF_IP_TOS to 1, so we reject that. */
612 mf_is_value_valid(const struct mf_field *mf, const union mf_value *value)
657 return !(value->u8 & 0x03);
660 return !(value->be16 & htons(0xff00));
663 return !(value->be16 & htons(VLAN_CFI | VLAN_PCP_MASK));
666 return !(value->u8 & ~7);
674 /* Copies the value of field 'mf' from 'flow' into 'value'. The caller is
675 * responsible for ensuring that 'flow' meets 'mf''s prerequisites. */
677 mf_get_value(const struct mf_field *mf, const struct flow *flow,
678 union mf_value *value)
682 value->be64 = flow->tun_id;
686 value->be16 = htons(flow->in_port);
704 value->be32 = htonl(flow->regs[0]);
708 memcpy(value->mac, flow->dl_src, ETH_ADDR_LEN);
712 memcpy(value->mac, flow->dl_dst, ETH_ADDR_LEN);
716 value->be16 = flow->dl_type;
720 value->be16 = flow->vlan_tci;
724 value->be16 = flow->vlan_tci & htons(VLAN_VID_MASK);
728 value->u8 = vlan_tci_to_pcp(flow->vlan_tci);
732 value->be32 = flow->nw_src;
736 value->be32 = flow->nw_dst;
740 value->ipv6 = flow->ipv6_src;
744 value->ipv6 = flow->ipv6_dst;
748 value->u8 = flow->nw_proto;
752 value->u8 = flow->nw_tos;
756 value->be16 = htons(flow->nw_proto);
760 value->be32 = flow->nw_src;
764 value->be32 = flow->nw_dst;
769 memcpy(value->mac, flow->arp_sha, ETH_ADDR_LEN);
774 memcpy(value->mac, flow->arp_tha, ETH_ADDR_LEN);
778 value->be16 = flow->tp_src;
782 value->be16 = flow->tp_dst;
786 value->be16 = flow->tp_src;
790 value->be16 = flow->tp_dst;
794 value->u8 = ntohs(flow->tp_src);
798 value->u8 = ntohs(flow->tp_dst);
802 value->ipv6 = flow->nd_target;
811 /* Makes 'rule' match field 'mf' exactly, with the value matched taken from
812 * 'value'. The caller is responsible for ensuring that 'rule' meets 'mf''s
815 mf_set_value(const struct mf_field *mf,
816 const union mf_value *value, struct cls_rule *rule)
820 cls_rule_set_tun_id(rule, value->be64);
824 cls_rule_set_in_port(rule, ntohs(value->be16));
843 cls_rule_set_reg(rule, mf->id - MFF_REG0, ntohl(value->be32));
848 cls_rule_set_dl_src(rule, value->mac);
852 cls_rule_set_dl_dst(rule, value->mac);
856 cls_rule_set_dl_type(rule, value->be16);
860 cls_rule_set_dl_tci(rule, value->be16);
864 cls_rule_set_dl_vlan(rule, value->be16);
868 cls_rule_set_dl_vlan_pcp(rule, value->u8);
872 cls_rule_set_nw_src(rule, value->be32);
876 cls_rule_set_nw_dst(rule, value->be32);
880 cls_rule_set_ipv6_src(rule, &value->ipv6);
884 cls_rule_set_ipv6_dst(rule, &value->ipv6);
888 cls_rule_set_nw_proto(rule, value->u8);
892 cls_rule_set_nw_tos(rule, value->u8);
896 cls_rule_set_nw_proto(rule, ntohs(value->be16));
900 cls_rule_set_nw_src(rule, value->be32);
904 cls_rule_set_nw_dst(rule, value->be32);
909 cls_rule_set_arp_sha(rule, value->mac);
914 cls_rule_set_arp_tha(rule, value->mac);
918 cls_rule_set_tp_src(rule, value->be16);
922 cls_rule_set_tp_dst(rule, value->be16);
926 cls_rule_set_tp_src(rule, value->be16);
930 cls_rule_set_tp_dst(rule, value->be16);
934 cls_rule_set_icmp_type(rule, value->u8);
938 cls_rule_set_icmp_code(rule, value->u8);
942 cls_rule_set_nd_target(rule, &value->ipv6);
951 /* Makes 'rule' wildcard field 'mf'.
953 * The caller is responsible for ensuring that 'rule' meets 'mf''s
956 mf_set_wild(const struct mf_field *mf, struct cls_rule *rule)
960 cls_rule_set_tun_id_masked(rule, htonll(0), htonll(0));
964 rule->wc.wildcards |= FWW_IN_PORT;
965 rule->flow.in_port = 0;
970 cls_rule_set_reg_masked(rule, 0, 0, 0);
975 cls_rule_set_reg_masked(rule, 1, 0, 0);
980 cls_rule_set_reg_masked(rule, 2, 0, 0);
985 cls_rule_set_reg_masked(rule, 3, 0, 0);
993 rule->wc.wildcards |= FWW_DL_SRC;
994 memset(rule->flow.dl_src, 0, sizeof rule->flow.dl_src);
998 rule->wc.wildcards |= FWW_DL_DST | FWW_ETH_MCAST;
999 memset(rule->flow.dl_dst, 0, sizeof rule->flow.dl_dst);
1003 rule->wc.wildcards |= FWW_DL_TYPE;
1004 rule->flow.dl_type = htons(0);
1008 cls_rule_set_dl_tci_masked(rule, htons(0), htons(0));
1012 cls_rule_set_any_vid(rule);
1016 cls_rule_set_any_pcp(rule);
1021 cls_rule_set_nw_src_masked(rule, htonl(0), htonl(0));
1026 cls_rule_set_nw_dst_masked(rule, htonl(0), htonl(0));
1030 memset(&rule->wc.ipv6_src_mask, 0, sizeof rule->wc.ipv6_src_mask);
1031 memset(&rule->flow.ipv6_src, 0, sizeof rule->flow.ipv6_src);
1035 memset(&rule->wc.ipv6_dst_mask, 0, sizeof rule->wc.ipv6_dst_mask);
1036 memset(&rule->flow.ipv6_dst, 0, sizeof rule->flow.ipv6_dst);
1040 rule->wc.wildcards |= FWW_NW_PROTO;
1041 rule->flow.nw_proto = 0;
1045 rule->wc.wildcards |= FWW_NW_TOS;
1046 rule->flow.nw_tos = 0;
1050 rule->wc.wildcards |= FWW_NW_PROTO;
1051 rule->flow.nw_proto = 0;
1056 rule->wc.wildcards |= FWW_ARP_SHA;
1057 memset(rule->flow.arp_sha, 0, sizeof rule->flow.arp_sha);
1062 rule->wc.wildcards |= FWW_ARP_THA;
1063 memset(rule->flow.arp_tha, 0, sizeof rule->flow.arp_tha);
1069 rule->wc.wildcards |= FWW_TP_SRC;
1070 rule->flow.tp_src = htons(0);
1076 rule->wc.wildcards |= FWW_TP_DST;
1077 rule->flow.tp_dst = htons(0);
1081 rule->wc.wildcards |= FWW_ND_TARGET;
1082 memset(&rule->flow.nd_target, 0, sizeof rule->flow.nd_target);
1091 /* Makes 'rule' match field 'mf' with the specified 'value' and 'mask'.
1092 * 'value' specifies a value to match and 'mask' specifies a wildcard pattern,
1093 * with a 1-bit indicating that the corresponding value bit must match and a
1094 * 0-bit indicating a don't-care.
1096 * If 'mask' is NULL or points to all-1-bits, then this call is equivalent to
1097 * mf_set_value(mf, value, rule). If 'mask' points to all-0-bits, then this
1098 * call is equivalent to mf_set_wild(mf, rule).
1100 * 'mask' must be a valid mask for 'mf' (see mf_is_mask_valid()). The caller
1101 * is responsible for ensuring that 'rule' meets 'mf''s prerequisites. */
1103 mf_set(const struct mf_field *mf,
1104 const union mf_value *value, const union mf_value *mask,
1105 struct cls_rule *rule)
1107 if (!mask || is_all_ones((const uint8_t *) mask, mf->n_bytes)) {
1108 mf_set_value(mf, value, rule);
1110 } else if (is_all_zeros((const uint8_t *) mask, mf->n_bytes)) {
1111 mf_set_wild(mf, rule);
1138 cls_rule_set_tun_id_masked(rule, value->be64, mask->be64);
1156 cls_rule_set_reg_masked(rule, mf->id - MFF_REG0,
1157 ntohl(value->be32), ntohl(mask->be32));
1161 if (flow_wildcards_is_dl_dst_mask_valid(mask->mac)) {
1162 cls_rule_set_dl_dst_masked(rule, value->mac, mask->mac);
1167 cls_rule_set_dl_tci_masked(rule, value->be16, mask->be16);
1171 cls_rule_set_nw_src_masked(rule, value->be32, mask->be32);
1175 cls_rule_set_nw_dst_masked(rule, value->be32, mask->be32);
1179 cls_rule_set_ipv6_src_masked(rule, &value->ipv6, &mask->ipv6);
1183 cls_rule_set_ipv6_dst_masked(rule, &value->ipv6, &mask->ipv6);
1187 cls_rule_set_nw_src_masked(rule, value->be32, mask->be32);
1191 cls_rule_set_nw_dst_masked(rule, value->be32, mask->be32);
1200 /* Makes a subfield starting at bit offset 'ofs' and continuing for 'n_bits' in
1201 * 'rule''s field 'mf' exactly match the 'n_bits' least-significant bits of
1204 * Example: suppose that 'mf' is originally the following 2-byte field in
1207 * value == 0xe00a == 2#1110000000001010
1208 * mask == 0xfc3f == 2#1111110000111111
1210 * The call mf_set_subfield(mf, 0x55, 8, 7, rule) would have the following
1211 * effect (note that 0x55 is 2#1010101):
1213 * value == 0xd50a == 2#1101010100001010
1214 * mask == 0xff3f == 2#1111111100111111
1216 * The caller is responsible for ensuring that the result will be a valid
1217 * wildcard pattern for 'mf'. The caller is responsible for ensuring that
1218 * 'rule' meets 'mf''s prerequisites. */
1220 mf_set_subfield(const struct mf_field *mf, uint64_t x, unsigned int ofs,
1221 unsigned int n_bits, struct cls_rule *rule)
1223 if (ofs == 0 && mf->n_bytes * 8 == n_bits) {
1224 union mf_value value;
1227 for (i = mf->n_bytes - 1; i >= 0; i--) {
1228 ((uint8_t *) &value)[i] = x;
1231 mf_set_value(mf, &value, rule);
1233 union mf_value value, mask;
1235 unsigned int byte_ofs;
1237 mf_get(mf, rule, &value, &mask);
1239 byte_ofs = mf->n_bytes - ofs / 8;
1240 vp = &((uint8_t *) &value)[byte_ofs];
1241 mp = &((uint8_t *) &mask)[byte_ofs];
1243 unsigned int chunk = MIN(8 - ofs % 8, n_bits);
1244 uint8_t chunk_mask = ((1 << chunk) - 1) << (ofs % 8);
1246 *--vp &= ~chunk_mask;
1247 *vp |= chunk_mask & (x << (ofs % 8));
1248 *--mp |= chunk_mask;
1254 while (n_bits >= 8) {
1262 uint8_t chunk_mask = (1 << n_bits) - 1;
1264 *--vp &= ~chunk_mask;
1265 *vp |= chunk_mask & x;
1266 *--mp |= chunk_mask;
1269 mf_set(mf, &value, &mask, rule);
1273 /* Copies the value and wildcard bit pattern for 'mf' from 'rule' into the
1274 * 'value' and 'mask', respectively. */
1276 mf_get(const struct mf_field *mf, const struct cls_rule *rule,
1277 union mf_value *value, union mf_value *mask)
1279 mf_get_value(mf, &rule->flow, value);
1280 mf_get_mask(mf, &rule->wc, mask);
1283 /* Assigns a random value for field 'mf' to 'value'. */
1285 mf_random_value(const struct mf_field *mf, union mf_value *value)
1287 random_bytes(value, mf->n_bytes);
1336 value->be16 &= htons(0xff);
1340 value->be16 &= htons(VLAN_VID_MASK);
1354 mf_from_integer_string(const struct mf_field *mf, const char *s,
1355 uint8_t *valuep, uint8_t *maskp)
1357 unsigned long long int integer, mask;
1362 integer = strtoull(s, &tail, 0);
1363 if (errno || (*tail != '\0' && *tail != '/')) {
1368 mask = strtoull(tail + 1, &tail, 0);
1369 if (errno || *tail != '\0') {
1376 for (i = mf->n_bytes - 1; i >= 0; i--) {
1377 valuep[i] = integer;
1383 return xasprintf("%s: value too large for %u-byte field %s",
1384 s, mf->n_bytes, mf->name);
1389 return xasprintf("%s: bad syntax for %s", s, mf->name);
1393 mf_from_ethernet_string(const struct mf_field *mf, const char *s,
1394 uint8_t mac[ETH_ADDR_LEN],
1395 uint8_t mask[ETH_ADDR_LEN])
1397 assert(mf->n_bytes == ETH_ADDR_LEN);
1399 switch (sscanf(s, ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT,
1400 ETH_ADDR_SCAN_ARGS(mac), ETH_ADDR_SCAN_ARGS(mask))){
1401 case ETH_ADDR_SCAN_COUNT * 2:
1404 case ETH_ADDR_SCAN_COUNT:
1405 memset(mask, 0xff, ETH_ADDR_LEN);
1409 return xasprintf("%s: invalid Ethernet address", s);
1414 mf_from_ipv4_string(const struct mf_field *mf, const char *s,
1415 ovs_be32 *ip, ovs_be32 *mask)
1419 assert(mf->n_bytes == sizeof *ip);
1421 if (sscanf(s, IP_SCAN_FMT"/"IP_SCAN_FMT,
1422 IP_SCAN_ARGS(ip), IP_SCAN_ARGS(mask)) == IP_SCAN_COUNT * 2) {
1424 } else if (sscanf(s, IP_SCAN_FMT"/%d",
1425 IP_SCAN_ARGS(ip), &prefix) == IP_SCAN_COUNT + 1) {
1426 if (prefix <= 0 || prefix > 32) {
1427 return xasprintf("%s: network prefix bits not between 1 and "
1429 } else if (prefix == 32) {
1430 *mask = htonl(UINT32_MAX);
1432 *mask = htonl(((1u << prefix) - 1) << (32 - prefix));
1434 } else if (sscanf(s, IP_SCAN_FMT, IP_SCAN_ARGS(ip)) == IP_SCAN_COUNT) {
1435 *mask = htonl(UINT32_MAX);
1437 return xasprintf("%s: invalid IP address", s);
1443 mf_from_ipv6_string(const struct mf_field *mf, const char *s,
1444 struct in6_addr *value, struct in6_addr *mask)
1446 char *str = xstrdup(s);
1447 char *save_ptr = NULL;
1448 const char *name, *netmask;
1451 assert(mf->n_bytes == sizeof *value);
1453 name = strtok_r(str, "/", &save_ptr);
1454 retval = name ? lookup_ipv6(name, value) : EINVAL;
1458 err = xasprintf("%s: could not convert to IPv6 address", str);
1464 netmask = strtok_r(NULL, "/", &save_ptr);
1466 int prefix = atoi(netmask);
1467 if (prefix <= 0 || prefix > 128) {
1469 return xasprintf("%s: prefix bits not between 1 and 128", s);
1471 *mask = ipv6_create_mask(prefix);
1474 *mask = in6addr_exact;
1482 mf_from_ofp_port_string(const struct mf_field *mf, const char *s,
1483 ovs_be16 *valuep, ovs_be16 *maskp)
1487 assert(mf->n_bytes == sizeof(ovs_be16));
1488 if (ofputil_port_from_string(s, &port)) {
1489 *valuep = htons(port);
1490 *maskp = htons(UINT16_MAX);
1493 return mf_from_integer_string(mf, s,
1494 (uint8_t *) valuep, (uint8_t *) maskp);
1498 /* Parses 's', a string value for field 'mf', into 'value' and 'mask'. Returns
1499 * NULL if successful, otherwise a malloc()'d string describing the error. */
1501 mf_parse(const struct mf_field *mf, const char *s,
1502 union mf_value *value, union mf_value *mask)
1504 if (!strcasecmp(s, "any") || !strcmp(s, "*")) {
1505 memset(value, 0, mf->n_bytes);
1506 memset(mask, 0, mf->n_bytes);
1510 switch (mf->string) {
1512 case MFS_HEXADECIMAL:
1513 return mf_from_integer_string(mf, s,
1514 (uint8_t *) value, (uint8_t *) mask);
1517 return mf_from_ethernet_string(mf, s, value->mac, mask->mac);
1520 return mf_from_ipv4_string(mf, s, &value->be32, &mask->be32);
1523 return mf_from_ipv6_string(mf, s, &value->ipv6, &mask->ipv6);
1526 return mf_from_ofp_port_string(mf, s, &value->be16, &mask->be16);
1531 /* Parses 's', a string value for field 'mf', into 'value'. Returns NULL if
1532 * successful, otherwise a malloc()'d string describing the error. */
1534 mf_parse_value(const struct mf_field *mf, const char *s, union mf_value *value)
1536 union mf_value mask;
1539 error = mf_parse(mf, s, value, &mask);
1544 if (!is_all_ones((const uint8_t *) &mask, mf->n_bytes)) {
1545 return xasprintf("%s: wildcards not allowed here", s);
1551 mf_format_integer_string(const struct mf_field *mf, const uint8_t *valuep,
1552 const uint8_t *maskp, struct ds *s)
1554 unsigned long long int integer;
1557 assert(mf->n_bytes <= 8);
1560 for (i = 0; i < mf->n_bytes; i++) {
1561 integer = (integer << 8) | valuep[i];
1563 if (mf->string == MFS_HEXADECIMAL) {
1564 ds_put_format(s, "%#llx", integer);
1566 ds_put_format(s, "%lld", integer);
1570 unsigned long long int mask;
1573 for (i = 0; i < mf->n_bytes; i++) {
1574 mask = (mask << 8) | maskp[i];
1577 /* I guess we could write the mask in decimal for MFS_DECIMAL but I'm
1578 * not sure that that a bit-mask written in decimal is ever easier to
1579 * understand than the same bit-mask written in hexadecimal. */
1580 ds_put_format(s, "/%#llx", mask);
1584 /* Appends to 's' a string representation of field 'mf' whose value is in
1585 * 'value' and 'mask'. 'mask' may be NULL to indicate an exact match. */
1587 mf_format(const struct mf_field *mf,
1588 const union mf_value *value, const union mf_value *mask,
1592 if (is_all_zeros((const uint8_t *) mask, mf->n_bytes)) {
1593 ds_put_cstr(s, "ANY");
1595 } else if (is_all_ones((const uint8_t *) mask, mf->n_bytes)) {
1600 switch (mf->string) {
1603 ofputil_format_port(ntohs(value->be16), s);
1608 case MFS_HEXADECIMAL:
1609 mf_format_integer_string(mf, (uint8_t *) value, (uint8_t *) mask, s);
1613 ds_put_format(s, ETH_ADDR_FMT, ETH_ADDR_ARGS(value->mac));
1615 ds_put_format(s, "/"ETH_ADDR_FMT, ETH_ADDR_ARGS(mask->mac));
1620 ip_format_masked(value->be32, mask ? mask->be32 : htonl(UINT32_MAX),
1625 print_ipv6_masked(s, &value->ipv6, mask ? &mask->ipv6 : NULL);