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), \
94 MFF_ETH_SRC, "eth_src", "dl_src",
101 MFF_ETH_DST, "eth_dst", "dl_dst",
108 MFF_ETH_TYPE, "eth_type", "dl_type",
109 MF_FIELD_SIZES(be16),
110 MFM_NONE, FWW_DL_TYPE,
117 MFF_VLAN_TCI, "vlan_tci", NULL,
118 MF_FIELD_SIZES(be16),
124 MFF_VLAN_VID, "dl_vlan", NULL,
125 sizeof(ovs_be16), 12,
131 MFF_VLAN_PCP, "dl_vlan_pcp", NULL,
144 MFF_IPV4_SRC, "ip_src", "nw_src",
145 MF_FIELD_SIZES(be32),
151 MFF_IPV4_DST, "ip_dst", "nw_dst",
152 MF_FIELD_SIZES(be32),
160 MFF_IPV6_SRC, "ipv6_src", NULL,
161 MF_FIELD_SIZES(ipv6),
167 MFF_IPV6_DST, "ipv6_dst", NULL,
168 MF_FIELD_SIZES(ipv6),
175 MFF_IPV6_LABEL, "ipv6_label", NULL,
177 MFM_NONE, FWW_IPV6_LABEL,
184 MFF_IP_PROTO, "nw_proto", NULL,
186 MFM_NONE, FWW_NW_PROTO,
191 MFF_IP_TOS, "nw_tos", NULL,
198 MFF_IP_FRAG, "ip_frag", NULL,
207 MFF_ARP_OP, "arp_op", NULL,
208 MF_FIELD_SIZES(be16),
209 MFM_NONE, FWW_NW_PROTO,
214 MFF_ARP_SPA, "arp_spa", NULL,
215 MF_FIELD_SIZES(be32),
221 MFF_ARP_TPA, "arp_tpa", NULL,
222 MF_FIELD_SIZES(be32),
228 MFF_ARP_SHA, "arp_sha", NULL,
230 MFM_NONE, FWW_ARP_SHA,
235 MFF_ARP_THA, "arp_tha", NULL,
237 MFM_NONE, FWW_ARP_THA,
248 MFF_TCP_SRC, "tcp_src", "tp_src",
249 MF_FIELD_SIZES(be16),
250 MFM_NONE, FWW_TP_SRC,
255 MFF_TCP_DST, "tcp_dst", "tp_dst",
256 MF_FIELD_SIZES(be16),
257 MFM_NONE, FWW_TP_DST,
264 MFF_UDP_SRC, "udp_src", NULL,
265 MF_FIELD_SIZES(be16),
266 MFM_NONE, FWW_TP_SRC,
271 MFF_UDP_DST, "udp_dst", NULL,
272 MF_FIELD_SIZES(be16),
273 MFM_NONE, FWW_TP_DST,
280 MFF_ICMP_TYPE, "icmp_type", NULL,
282 MFM_NONE, FWW_TP_SRC,
287 MFF_ICMP_CODE, "icmp_code", NULL,
289 MFM_NONE, FWW_TP_DST,
300 MFF_ND_TARGET, "nd_target", NULL,
301 MF_FIELD_SIZES(ipv6),
302 MFM_NONE, FWW_ND_TARGET,
307 MFF_ND_SLL, "nd_sll", NULL,
309 MFM_NONE, FWW_ARP_SHA,
314 MFF_ND_TLL, "nd_tll", NULL,
316 MFM_NONE, FWW_ARP_THA,
323 /* Returns the field with the given 'id'. */
324 const struct mf_field *
325 mf_from_id(enum mf_field_id id)
327 assert((unsigned int) id < MFF_N_IDS);
328 return &mf_fields[id];
331 /* Returns the field with the given 'name', or a null pointer if no field has
333 const struct mf_field *
334 mf_from_name(const char *name)
336 static struct shash mf_by_name = SHASH_INITIALIZER(&mf_by_name);
338 if (shash_is_empty(&mf_by_name)) {
339 const struct mf_field *mf;
341 for (mf = mf_fields; mf < &mf_fields[MFF_N_IDS]; mf++) {
342 shash_add_once(&mf_by_name, mf->name, mf);
343 if (mf->extra_name) {
344 shash_add_once(&mf_by_name, mf->extra_name, mf);
349 return shash_find_data(&mf_by_name, name);
352 /* Returns true if 'wc' wildcards all the bits in field 'mf', false if 'wc'
353 * specifies at least one bit in the field.
355 * The caller is responsible for ensuring that 'wc' corresponds to a flow that
356 * meets 'mf''s prerequisites. */
358 mf_is_all_wild(const struct mf_field *mf, const struct flow_wildcards *wc)
378 assert(mf->fww_bit != 0);
379 return (wc->wildcards & mf->fww_bit) != 0;
382 return !wc->tun_id_mask;
402 return !wc->reg_masks[mf->id - MFF_REG0];
405 return ((wc->wildcards & (FWW_ETH_MCAST | FWW_DL_DST))
406 == (FWW_ETH_MCAST | FWW_DL_DST));
409 return !wc->vlan_tci_mask;
411 return !(wc->vlan_tci_mask & htons(VLAN_VID_MASK));
413 return !(wc->vlan_tci_mask & htons(VLAN_PCP_MASK));
416 return !wc->nw_src_mask;
418 return !wc->nw_dst_mask;
421 return ipv6_mask_is_any(&wc->ipv6_src_mask);
423 return ipv6_mask_is_any(&wc->ipv6_dst_mask);
426 return !(wc->tos_frag_mask & IP_DSCP_MASK);
428 return !(wc->tos_frag_mask & FLOW_FRAG_MASK);
431 return !wc->nw_src_mask;
433 return !wc->nw_dst_mask;
441 /* Initializes 'mask' with the wildcard bit pattern for field 'mf' within 'wc'.
442 * Each bit in 'mask' will be set to 1 if the bit is significant for matching
443 * purposes, or to 0 if it is wildcarded.
445 * The caller is responsible for ensuring that 'wc' corresponds to a flow that
446 * meets 'mf''s prerequisites. */
448 mf_get_mask(const struct mf_field *mf, const struct flow_wildcards *wc,
449 union mf_value *mask)
469 assert(mf->fww_bit != 0);
470 memset(mask, wc->wildcards & mf->fww_bit ? 0x00 : 0xff, mf->n_bytes);
474 mask->be64 = wc->tun_id_mask;
495 mask->be32 = htonl(wc->reg_masks[mf->id - MFF_REG0]);
499 memcpy(mask->mac, flow_wildcards_to_dl_dst_mask(wc->wildcards),
504 mask->be16 = wc->vlan_tci_mask;
507 mask->be16 = wc->vlan_tci_mask & htons(VLAN_VID_MASK);
510 mask->u8 = vlan_tci_to_pcp(wc->vlan_tci_mask);
514 mask->be32 = wc->nw_src_mask;
517 mask->be32 = wc->nw_dst_mask;
521 mask->ipv6 = wc->ipv6_src_mask;
524 mask->ipv6 = wc->ipv6_dst_mask;
528 mask->u8 = wc->tos_frag_mask & IP_DSCP_MASK;
531 mask->u8 = wc->tos_frag_mask & FLOW_FRAG_MASK;
535 mask->be32 = wc->nw_src_mask;
538 mask->be32 = wc->nw_dst_mask;
547 /* Tests whether 'mask' is a valid wildcard bit pattern for 'mf'. Returns true
548 * if the mask is valid, false otherwise. */
550 mf_is_mask_valid(const struct mf_field *mf, const union mf_value *mask)
552 switch (mf->maskable) {
554 return (is_all_zeros((const uint8_t *) mask, mf->n_bytes) ||
555 is_all_ones((const uint8_t *) mask, mf->n_bytes));
561 return (mf->n_bytes == 4
562 ? ip_is_cidr(mask->be32)
563 : ipv6_is_cidr(&mask->ipv6));
566 return flow_wildcards_is_dl_dst_mask_valid(mask->mac);
573 is_ip_any(const struct flow *flow)
575 return (flow->dl_type == htons(ETH_TYPE_IP) ||
576 flow->dl_type == htons(ETH_TYPE_IPV6));
580 is_icmpv4(const struct flow *flow)
582 return (flow->dl_type == htons(ETH_TYPE_IP)
583 && flow->nw_proto == IPPROTO_ICMP);
587 is_icmpv6(const struct flow *flow)
589 return (flow->dl_type == htons(ETH_TYPE_IPV6)
590 && flow->nw_proto == IPPROTO_ICMPV6);
593 /* Returns true if 'flow' meets the prerequisites for 'mf', false otherwise. */
595 mf_are_prereqs_ok(const struct mf_field *mf, const struct flow *flow)
597 switch (mf->prereqs) {
602 return flow->dl_type == htons(ETH_TYPE_ARP);
604 return flow->dl_type == htons(ETH_TYPE_IP);
606 return flow->dl_type == htons(ETH_TYPE_IPV6);
608 return is_ip_any(flow);
611 return is_ip_any(flow) && flow->nw_proto == IPPROTO_TCP;
613 return is_ip_any(flow) && flow->nw_proto == IPPROTO_UDP;
615 return is_icmpv6(flow);
617 return is_icmpv4(flow) || is_icmpv6(flow);
620 return (is_icmpv6(flow)
621 && flow->tp_dst == htons(0)
622 && (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT) ||
623 flow->tp_src == htons(ND_NEIGHBOR_ADVERT)));
625 return (is_icmpv6(flow)
626 && flow->tp_dst == htons(0)
627 && (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT)));
629 return (is_icmpv6(flow)
630 && flow->tp_dst == htons(0)
631 && (flow->tp_src == htons(ND_NEIGHBOR_ADVERT)));
637 /* Returns true if 'value' may be a valid value *as part of a masked match*,
640 * A value is not rejected just because it is not valid for the field in
641 * question, but only if it doesn't make sense to test the bits in question at
642 * all. For example, the MFF_VLAN_TCI field will never have a nonzero value
643 * without the VLAN_CFI bit being set, but we can't reject those values because
644 * it is still legitimate to test just for those bits (see the documentation
645 * for NXM_OF_VLAN_TCI in nicira-ext.h). On the other hand, there is never a
646 * reason to set the low bit of MFF_IP_TOS to 1, so we reject that. */
648 mf_is_value_valid(const struct mf_field *mf, const union mf_value *value)
696 return !(value->u8 & ~IP_DSCP_MASK);
698 return !(value->u8 & ~FLOW_FRAG_MASK);
701 return !(value->be16 & htons(0xff00));
704 return !(value->be16 & htons(VLAN_CFI | VLAN_PCP_MASK));
707 return !(value->u8 & ~7);
710 return !(value->be32 & ~htonl(IPV6_LABEL_MASK));
718 /* Copies the value of field 'mf' from 'flow' into 'value'. The caller is
719 * responsible for ensuring that 'flow' meets 'mf''s prerequisites. */
721 mf_get_value(const struct mf_field *mf, const struct flow *flow,
722 union mf_value *value)
726 value->be64 = flow->tun_id;
730 value->be16 = htons(flow->in_port);
751 value->be32 = htonl(flow->regs[mf->id - MFF_REG0]);
755 memcpy(value->mac, flow->dl_src, ETH_ADDR_LEN);
759 memcpy(value->mac, flow->dl_dst, ETH_ADDR_LEN);
763 value->be16 = flow->dl_type;
767 value->be16 = flow->vlan_tci;
771 value->be16 = flow->vlan_tci & htons(VLAN_VID_MASK);
775 value->u8 = vlan_tci_to_pcp(flow->vlan_tci);
779 value->be32 = flow->nw_src;
783 value->be32 = flow->nw_dst;
787 value->ipv6 = flow->ipv6_src;
791 value->ipv6 = flow->ipv6_dst;
795 value->be32 = flow->ipv6_label;
799 value->u8 = flow->nw_proto;
803 value->u8 = flow->tos_frag & IP_DSCP_MASK;
807 value->u8 = flow->tos_frag & FLOW_FRAG_MASK;
811 value->be16 = htons(flow->nw_proto);
815 value->be32 = flow->nw_src;
819 value->be32 = flow->nw_dst;
824 memcpy(value->mac, flow->arp_sha, ETH_ADDR_LEN);
829 memcpy(value->mac, flow->arp_tha, ETH_ADDR_LEN);
833 value->be16 = flow->tp_src;
837 value->be16 = flow->tp_dst;
841 value->be16 = flow->tp_src;
845 value->be16 = flow->tp_dst;
849 value->u8 = ntohs(flow->tp_src);
853 value->u8 = ntohs(flow->tp_dst);
857 value->ipv6 = flow->nd_target;
866 /* Makes 'rule' match field 'mf' exactly, with the value matched taken from
867 * 'value'. The caller is responsible for ensuring that 'rule' meets 'mf''s
870 mf_set_value(const struct mf_field *mf,
871 const union mf_value *value, struct cls_rule *rule)
875 cls_rule_set_tun_id(rule, value->be64);
879 cls_rule_set_in_port(rule, ntohs(value->be16));
901 cls_rule_set_reg(rule, mf->id - MFF_REG0, ntohl(value->be32));
906 cls_rule_set_dl_src(rule, value->mac);
910 cls_rule_set_dl_dst(rule, value->mac);
914 cls_rule_set_dl_type(rule, value->be16);
918 cls_rule_set_dl_tci(rule, value->be16);
922 cls_rule_set_dl_vlan(rule, value->be16);
926 cls_rule_set_dl_vlan_pcp(rule, value->u8);
930 cls_rule_set_nw_src(rule, value->be32);
934 cls_rule_set_nw_dst(rule, value->be32);
938 cls_rule_set_ipv6_src(rule, &value->ipv6);
942 cls_rule_set_ipv6_dst(rule, &value->ipv6);
946 cls_rule_set_ipv6_label(rule, value->be32);
950 cls_rule_set_nw_proto(rule, value->u8);
954 cls_rule_set_nw_tos(rule, value->u8);
958 cls_rule_set_frag(rule, value->u8);
962 cls_rule_set_nw_proto(rule, ntohs(value->be16));
966 cls_rule_set_nw_src(rule, value->be32);
970 cls_rule_set_nw_dst(rule, value->be32);
975 cls_rule_set_arp_sha(rule, value->mac);
980 cls_rule_set_arp_tha(rule, value->mac);
984 cls_rule_set_tp_src(rule, value->be16);
988 cls_rule_set_tp_dst(rule, value->be16);
992 cls_rule_set_tp_src(rule, value->be16);
996 cls_rule_set_tp_dst(rule, value->be16);
1000 cls_rule_set_icmp_type(rule, value->u8);
1004 cls_rule_set_icmp_code(rule, value->u8);
1008 cls_rule_set_nd_target(rule, &value->ipv6);
1017 /* Makes 'rule' wildcard field 'mf'.
1019 * The caller is responsible for ensuring that 'rule' meets 'mf''s
1022 mf_set_wild(const struct mf_field *mf, struct cls_rule *rule)
1026 cls_rule_set_tun_id_masked(rule, htonll(0), htonll(0));
1030 rule->wc.wildcards |= FWW_IN_PORT;
1031 rule->flow.in_port = 0;
1036 cls_rule_set_reg_masked(rule, 0, 0, 0);
1041 cls_rule_set_reg_masked(rule, 1, 0, 0);
1046 cls_rule_set_reg_masked(rule, 2, 0, 0);
1051 cls_rule_set_reg_masked(rule, 3, 0, 0);
1056 cls_rule_set_reg_masked(rule, 4, 0, 0);
1064 rule->wc.wildcards |= FWW_DL_SRC;
1065 memset(rule->flow.dl_src, 0, sizeof rule->flow.dl_src);
1069 rule->wc.wildcards |= FWW_DL_DST | FWW_ETH_MCAST;
1070 memset(rule->flow.dl_dst, 0, sizeof rule->flow.dl_dst);
1074 rule->wc.wildcards |= FWW_DL_TYPE;
1075 rule->flow.dl_type = htons(0);
1079 cls_rule_set_dl_tci_masked(rule, htons(0), htons(0));
1083 cls_rule_set_any_vid(rule);
1087 cls_rule_set_any_pcp(rule);
1092 cls_rule_set_nw_src_masked(rule, htonl(0), htonl(0));
1097 cls_rule_set_nw_dst_masked(rule, htonl(0), htonl(0));
1101 memset(&rule->wc.ipv6_src_mask, 0, sizeof rule->wc.ipv6_src_mask);
1102 memset(&rule->flow.ipv6_src, 0, sizeof rule->flow.ipv6_src);
1106 memset(&rule->wc.ipv6_dst_mask, 0, sizeof rule->wc.ipv6_dst_mask);
1107 memset(&rule->flow.ipv6_dst, 0, sizeof rule->flow.ipv6_dst);
1110 case MFF_IPV6_LABEL:
1111 rule->wc.wildcards |= FWW_IPV6_LABEL;
1112 rule->flow.ipv6_label = 0;
1116 rule->wc.wildcards |= FWW_NW_PROTO;
1117 rule->flow.nw_proto = 0;
1121 rule->wc.tos_frag_mask |= IP_DSCP_MASK;
1122 rule->flow.tos_frag &= ~IP_DSCP_MASK;
1126 rule->wc.tos_frag_mask |= FLOW_FRAG_MASK;
1127 rule->flow.tos_frag &= ~FLOW_FRAG_MASK;
1131 rule->wc.wildcards |= FWW_NW_PROTO;
1132 rule->flow.nw_proto = 0;
1137 rule->wc.wildcards |= FWW_ARP_SHA;
1138 memset(rule->flow.arp_sha, 0, sizeof rule->flow.arp_sha);
1143 rule->wc.wildcards |= FWW_ARP_THA;
1144 memset(rule->flow.arp_tha, 0, sizeof rule->flow.arp_tha);
1150 rule->wc.wildcards |= FWW_TP_SRC;
1151 rule->flow.tp_src = htons(0);
1157 rule->wc.wildcards |= FWW_TP_DST;
1158 rule->flow.tp_dst = htons(0);
1162 rule->wc.wildcards |= FWW_ND_TARGET;
1163 memset(&rule->flow.nd_target, 0, sizeof rule->flow.nd_target);
1172 /* Makes 'rule' match field 'mf' with the specified 'value' and 'mask'.
1173 * 'value' specifies a value to match and 'mask' specifies a wildcard pattern,
1174 * with a 1-bit indicating that the corresponding value bit must match and a
1175 * 0-bit indicating a don't-care.
1177 * If 'mask' is NULL or points to all-1-bits, then this call is equivalent to
1178 * mf_set_value(mf, value, rule). If 'mask' points to all-0-bits, then this
1179 * call is equivalent to mf_set_wild(mf, rule).
1181 * 'mask' must be a valid mask for 'mf' (see mf_is_mask_valid()). The caller
1182 * is responsible for ensuring that 'rule' meets 'mf''s prerequisites. */
1184 mf_set(const struct mf_field *mf,
1185 const union mf_value *value, const union mf_value *mask,
1186 struct cls_rule *rule)
1188 if (!mask || is_all_ones((const uint8_t *) mask, mf->n_bytes)) {
1189 mf_set_value(mf, value, rule);
1191 } else if (is_all_zeros((const uint8_t *) mask, mf->n_bytes)) {
1192 mf_set_wild(mf, rule);
1202 case MFF_IPV6_LABEL:
1220 cls_rule_set_tun_id_masked(rule, value->be64, mask->be64);
1241 cls_rule_set_reg_masked(rule, mf->id - MFF_REG0,
1242 ntohl(value->be32), ntohl(mask->be32));
1246 if (flow_wildcards_is_dl_dst_mask_valid(mask->mac)) {
1247 cls_rule_set_dl_dst_masked(rule, value->mac, mask->mac);
1252 cls_rule_set_dl_tci_masked(rule, value->be16, mask->be16);
1256 cls_rule_set_nw_src_masked(rule, value->be32, mask->be32);
1260 cls_rule_set_nw_dst_masked(rule, value->be32, mask->be32);
1264 cls_rule_set_ipv6_src_masked(rule, &value->ipv6, &mask->ipv6);
1268 cls_rule_set_ipv6_dst_masked(rule, &value->ipv6, &mask->ipv6);
1272 cls_rule_set_frag_masked(rule, value->u8, mask->u8);
1276 cls_rule_set_nw_src_masked(rule, value->be32, mask->be32);
1280 cls_rule_set_nw_dst_masked(rule, value->be32, mask->be32);
1289 /* Makes a subfield starting at bit offset 'ofs' and continuing for 'n_bits' in
1290 * 'rule''s field 'mf' exactly match the 'n_bits' least-significant bits of
1293 * Example: suppose that 'mf' is originally the following 2-byte field in
1296 * value == 0xe00a == 2#1110000000001010
1297 * mask == 0xfc3f == 2#1111110000111111
1299 * The call mf_set_subfield(mf, 0x55, 8, 7, rule) would have the following
1300 * effect (note that 0x55 is 2#1010101):
1302 * value == 0xd50a == 2#1101010100001010
1303 * mask == 0xff3f == 2#1111111100111111
1305 * The caller is responsible for ensuring that the result will be a valid
1306 * wildcard pattern for 'mf'. The caller is responsible for ensuring that
1307 * 'rule' meets 'mf''s prerequisites. */
1309 mf_set_subfield(const struct mf_field *mf, uint64_t x, unsigned int ofs,
1310 unsigned int n_bits, struct cls_rule *rule)
1312 if (ofs == 0 && mf->n_bytes * 8 == n_bits) {
1313 union mf_value value;
1316 for (i = mf->n_bytes - 1; i >= 0; i--) {
1317 ((uint8_t *) &value)[i] = x;
1320 mf_set_value(mf, &value, rule);
1322 union mf_value value, mask;
1324 unsigned int byte_ofs;
1326 mf_get(mf, rule, &value, &mask);
1328 byte_ofs = mf->n_bytes - ofs / 8;
1329 vp = &((uint8_t *) &value)[byte_ofs];
1330 mp = &((uint8_t *) &mask)[byte_ofs];
1332 unsigned int chunk = MIN(8 - ofs % 8, n_bits);
1333 uint8_t chunk_mask = ((1 << chunk) - 1) << (ofs % 8);
1335 *--vp &= ~chunk_mask;
1336 *vp |= chunk_mask & (x << (ofs % 8));
1337 *--mp |= chunk_mask;
1343 while (n_bits >= 8) {
1351 uint8_t chunk_mask = (1 << n_bits) - 1;
1353 *--vp &= ~chunk_mask;
1354 *vp |= chunk_mask & x;
1355 *--mp |= chunk_mask;
1358 mf_set(mf, &value, &mask, rule);
1362 /* Copies the value and wildcard bit pattern for 'mf' from 'rule' into the
1363 * 'value' and 'mask', respectively. */
1365 mf_get(const struct mf_field *mf, const struct cls_rule *rule,
1366 union mf_value *value, union mf_value *mask)
1368 mf_get_value(mf, &rule->flow, value);
1369 mf_get_mask(mf, &rule->wc, mask);
1372 /* Assigns a random value for field 'mf' to 'value'. */
1374 mf_random_value(const struct mf_field *mf, union mf_value *value)
1376 random_bytes(value, mf->n_bytes);
1423 case MFF_IPV6_LABEL:
1424 value->be32 &= ~htonl(IPV6_LABEL_MASK);
1432 value->u8 &= FLOW_FRAG_MASK;
1436 value->be16 &= htons(0xff);
1440 value->be16 &= htons(VLAN_VID_MASK);
1454 mf_from_integer_string(const struct mf_field *mf, const char *s,
1455 uint8_t *valuep, uint8_t *maskp)
1457 unsigned long long int integer, mask;
1462 integer = strtoull(s, &tail, 0);
1463 if (errno || (*tail != '\0' && *tail != '/')) {
1468 mask = strtoull(tail + 1, &tail, 0);
1469 if (errno || *tail != '\0') {
1476 for (i = mf->n_bytes - 1; i >= 0; i--) {
1477 valuep[i] = integer;
1483 return xasprintf("%s: value too large for %u-byte field %s",
1484 s, mf->n_bytes, mf->name);
1489 return xasprintf("%s: bad syntax for %s", s, mf->name);
1493 mf_from_ethernet_string(const struct mf_field *mf, const char *s,
1494 uint8_t mac[ETH_ADDR_LEN],
1495 uint8_t mask[ETH_ADDR_LEN])
1497 assert(mf->n_bytes == ETH_ADDR_LEN);
1499 switch (sscanf(s, ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT,
1500 ETH_ADDR_SCAN_ARGS(mac), ETH_ADDR_SCAN_ARGS(mask))){
1501 case ETH_ADDR_SCAN_COUNT * 2:
1504 case ETH_ADDR_SCAN_COUNT:
1505 memset(mask, 0xff, ETH_ADDR_LEN);
1509 return xasprintf("%s: invalid Ethernet address", s);
1514 mf_from_ipv4_string(const struct mf_field *mf, const char *s,
1515 ovs_be32 *ip, ovs_be32 *mask)
1519 assert(mf->n_bytes == sizeof *ip);
1521 if (sscanf(s, IP_SCAN_FMT"/"IP_SCAN_FMT,
1522 IP_SCAN_ARGS(ip), IP_SCAN_ARGS(mask)) == IP_SCAN_COUNT * 2) {
1524 } else if (sscanf(s, IP_SCAN_FMT"/%d",
1525 IP_SCAN_ARGS(ip), &prefix) == IP_SCAN_COUNT + 1) {
1526 if (prefix <= 0 || prefix > 32) {
1527 return xasprintf("%s: network prefix bits not between 1 and "
1529 } else if (prefix == 32) {
1530 *mask = htonl(UINT32_MAX);
1532 *mask = htonl(((1u << prefix) - 1) << (32 - prefix));
1534 } else if (sscanf(s, IP_SCAN_FMT, IP_SCAN_ARGS(ip)) == IP_SCAN_COUNT) {
1535 *mask = htonl(UINT32_MAX);
1537 return xasprintf("%s: invalid IP address", s);
1543 mf_from_ipv6_string(const struct mf_field *mf, const char *s,
1544 struct in6_addr *value, struct in6_addr *mask)
1546 char *str = xstrdup(s);
1547 char *save_ptr = NULL;
1548 const char *name, *netmask;
1551 assert(mf->n_bytes == sizeof *value);
1553 name = strtok_r(str, "/", &save_ptr);
1554 retval = name ? lookup_ipv6(name, value) : EINVAL;
1558 err = xasprintf("%s: could not convert to IPv6 address", str);
1564 netmask = strtok_r(NULL, "/", &save_ptr);
1566 int prefix = atoi(netmask);
1567 if (prefix <= 0 || prefix > 128) {
1569 return xasprintf("%s: prefix bits not between 1 and 128", s);
1571 *mask = ipv6_create_mask(prefix);
1574 *mask = in6addr_exact;
1582 mf_from_ofp_port_string(const struct mf_field *mf, const char *s,
1583 ovs_be16 *valuep, ovs_be16 *maskp)
1587 assert(mf->n_bytes == sizeof(ovs_be16));
1588 if (ofputil_port_from_string(s, &port)) {
1589 *valuep = htons(port);
1590 *maskp = htons(UINT16_MAX);
1593 return mf_from_integer_string(mf, s,
1594 (uint8_t *) valuep, (uint8_t *) maskp);
1598 struct frag_handling {
1604 static const struct frag_handling all_frags[] = {
1605 #define A FLOW_FRAG_ANY
1606 #define L FLOW_FRAG_LATER
1607 /* name mask value */
1610 { "first", A|L, A },
1611 { "later", A|L, A|L },
1616 { "not_later", L, 0 },
1623 mf_from_frag_string(const char *s, uint8_t *valuep, uint8_t *maskp)
1625 const struct frag_handling *h;
1627 for (h = all_frags; h < &all_frags[ARRAY_SIZE(all_frags)]; h++) {
1628 if (!strcasecmp(s, h->name)) {
1629 /* We force the upper bits of the mask on to make mf_parse_value()
1630 * happy (otherwise it will never think it's an exact match.) */
1631 *maskp = h->mask | ~FLOW_FRAG_MASK;
1637 return xasprintf("%s: unknown fragment type (valid types are \"no\", "
1638 "\"yes\", \"first\", \"later\", \"not_first\"", s);
1641 /* Parses 's', a string value for field 'mf', into 'value' and 'mask'. Returns
1642 * NULL if successful, otherwise a malloc()'d string describing the error. */
1644 mf_parse(const struct mf_field *mf, const char *s,
1645 union mf_value *value, union mf_value *mask)
1647 if (!strcasecmp(s, "any") || !strcmp(s, "*")) {
1648 memset(value, 0, mf->n_bytes);
1649 memset(mask, 0, mf->n_bytes);
1653 switch (mf->string) {
1655 case MFS_HEXADECIMAL:
1656 return mf_from_integer_string(mf, s,
1657 (uint8_t *) value, (uint8_t *) mask);
1660 return mf_from_ethernet_string(mf, s, value->mac, mask->mac);
1663 return mf_from_ipv4_string(mf, s, &value->be32, &mask->be32);
1666 return mf_from_ipv6_string(mf, s, &value->ipv6, &mask->ipv6);
1669 return mf_from_ofp_port_string(mf, s, &value->be16, &mask->be16);
1672 return mf_from_frag_string(s, &value->u8, &mask->u8);
1677 /* Parses 's', a string value for field 'mf', into 'value'. Returns NULL if
1678 * successful, otherwise a malloc()'d string describing the error. */
1680 mf_parse_value(const struct mf_field *mf, const char *s, union mf_value *value)
1682 union mf_value mask;
1685 error = mf_parse(mf, s, value, &mask);
1690 if (!is_all_ones((const uint8_t *) &mask, mf->n_bytes)) {
1691 return xasprintf("%s: wildcards not allowed here", s);
1697 mf_format_integer_string(const struct mf_field *mf, const uint8_t *valuep,
1698 const uint8_t *maskp, struct ds *s)
1700 unsigned long long int integer;
1703 assert(mf->n_bytes <= 8);
1706 for (i = 0; i < mf->n_bytes; i++) {
1707 integer = (integer << 8) | valuep[i];
1709 if (mf->string == MFS_HEXADECIMAL) {
1710 ds_put_format(s, "%#llx", integer);
1712 ds_put_format(s, "%lld", integer);
1716 unsigned long long int mask;
1719 for (i = 0; i < mf->n_bytes; i++) {
1720 mask = (mask << 8) | maskp[i];
1723 /* I guess we could write the mask in decimal for MFS_DECIMAL but I'm
1724 * not sure that that a bit-mask written in decimal is ever easier to
1725 * understand than the same bit-mask written in hexadecimal. */
1726 ds_put_format(s, "/%#llx", mask);
1731 mf_format_frag_string(const uint8_t *valuep, const uint8_t *maskp,
1734 const struct frag_handling *h;
1735 uint8_t value = *valuep;
1736 uint8_t mask = *maskp;
1739 mask &= FLOW_FRAG_MASK;
1741 for (h = all_frags; h < &all_frags[ARRAY_SIZE(all_frags)]; h++) {
1742 if (value == h->value && mask == h->mask) {
1743 ds_put_cstr(s, h->name);
1747 ds_put_cstr(s, "<error>");
1750 /* Appends to 's' a string representation of field 'mf' whose value is in
1751 * 'value' and 'mask'. 'mask' may be NULL to indicate an exact match. */
1753 mf_format(const struct mf_field *mf,
1754 const union mf_value *value, const union mf_value *mask,
1758 if (is_all_zeros((const uint8_t *) mask, mf->n_bytes)) {
1759 ds_put_cstr(s, "ANY");
1761 } else if (is_all_ones((const uint8_t *) mask, mf->n_bytes)) {
1766 switch (mf->string) {
1769 ofputil_format_port(ntohs(value->be16), s);
1774 case MFS_HEXADECIMAL:
1775 mf_format_integer_string(mf, (uint8_t *) value, (uint8_t *) mask, s);
1779 ds_put_format(s, ETH_ADDR_FMT, ETH_ADDR_ARGS(value->mac));
1781 ds_put_format(s, "/"ETH_ADDR_FMT, ETH_ADDR_ARGS(mask->mac));
1786 ip_format_masked(value->be32, mask ? mask->be32 : htonl(UINT32_MAX),
1791 print_ipv6_masked(s, &value->ipv6, mask ? &mask->ipv6 : NULL);
1795 mf_format_frag_string(&value->u8, &mask->u8, s);