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),
176 MFF_IP_PROTO, "nw_proto", NULL,
178 MFM_NONE, FWW_NW_PROTO,
183 MFF_IP_TOS, "nw_tos", NULL,
190 MFF_IP_FRAG, "ip_frag", NULL,
199 MFF_ARP_OP, "arp_op", NULL,
200 MF_FIELD_SIZES(be16),
201 MFM_NONE, FWW_NW_PROTO,
206 MFF_ARP_SPA, "arp_spa", NULL,
207 MF_FIELD_SIZES(be32),
213 MFF_ARP_TPA, "arp_tpa", NULL,
214 MF_FIELD_SIZES(be32),
220 MFF_ARP_SHA, "arp_sha", NULL,
222 MFM_NONE, FWW_ARP_SHA,
227 MFF_ARP_THA, "arp_tha", NULL,
229 MFM_NONE, FWW_ARP_THA,
240 MFF_TCP_SRC, "tcp_src", "tp_src",
241 MF_FIELD_SIZES(be16),
242 MFM_NONE, FWW_TP_SRC,
247 MFF_TCP_DST, "tcp_dst", "tp_dst",
248 MF_FIELD_SIZES(be16),
249 MFM_NONE, FWW_TP_DST,
256 MFF_UDP_SRC, "udp_src", NULL,
257 MF_FIELD_SIZES(be16),
258 MFM_NONE, FWW_TP_SRC,
263 MFF_UDP_DST, "udp_dst", NULL,
264 MF_FIELD_SIZES(be16),
265 MFM_NONE, FWW_TP_DST,
272 MFF_ICMP_TYPE, "icmp_type", NULL,
274 MFM_NONE, FWW_TP_SRC,
279 MFF_ICMP_CODE, "icmp_code", NULL,
281 MFM_NONE, FWW_TP_DST,
292 MFF_ND_TARGET, "nd_target", NULL,
293 MF_FIELD_SIZES(ipv6),
294 MFM_NONE, FWW_ND_TARGET,
299 MFF_ND_SLL, "nd_sll", NULL,
301 MFM_NONE, FWW_ARP_SHA,
306 MFF_ND_TLL, "nd_tll", NULL,
308 MFM_NONE, FWW_ARP_THA,
315 /* Returns the field with the given 'id'. */
316 const struct mf_field *
317 mf_from_id(enum mf_field_id id)
319 assert((unsigned int) id < MFF_N_IDS);
320 return &mf_fields[id];
323 /* Returns the field with the given 'name', or a null pointer if no field has
325 const struct mf_field *
326 mf_from_name(const char *name)
328 static struct shash mf_by_name = SHASH_INITIALIZER(&mf_by_name);
330 if (shash_is_empty(&mf_by_name)) {
331 const struct mf_field *mf;
333 for (mf = mf_fields; mf < &mf_fields[MFF_N_IDS]; mf++) {
334 shash_add_once(&mf_by_name, mf->name, mf);
335 if (mf->extra_name) {
336 shash_add_once(&mf_by_name, mf->extra_name, mf);
341 return shash_find_data(&mf_by_name, name);
344 /* Returns true if 'wc' wildcards all the bits in field 'mf', false if 'wc'
345 * specifies at least one bit in the field.
347 * The caller is responsible for ensuring that 'wc' corresponds to a flow that
348 * meets 'mf''s prerequisites. */
350 mf_is_all_wild(const struct mf_field *mf, const struct flow_wildcards *wc)
369 assert(mf->fww_bit != 0);
370 return (wc->wildcards & mf->fww_bit) != 0;
373 return !wc->tun_id_mask;
393 return !wc->reg_masks[mf->id - MFF_REG0];
396 return ((wc->wildcards & (FWW_ETH_MCAST | FWW_DL_DST))
397 == (FWW_ETH_MCAST | FWW_DL_DST));
400 return !wc->vlan_tci_mask;
402 return !(wc->vlan_tci_mask & htons(VLAN_VID_MASK));
404 return !(wc->vlan_tci_mask & htons(VLAN_PCP_MASK));
407 return !wc->nw_src_mask;
409 return !wc->nw_dst_mask;
412 return ipv6_mask_is_any(&wc->ipv6_src_mask);
414 return ipv6_mask_is_any(&wc->ipv6_dst_mask);
417 return !(wc->tos_frag_mask & IP_DSCP_MASK);
419 return !(wc->tos_frag_mask & FLOW_FRAG_MASK);
422 return !wc->nw_src_mask;
424 return !wc->nw_dst_mask;
432 /* Initializes 'mask' with the wildcard bit pattern for field 'mf' within 'wc'.
433 * Each bit in 'mask' will be set to 1 if the bit is significant for matching
434 * purposes, or to 0 if it is wildcarded.
436 * The caller is responsible for ensuring that 'wc' corresponds to a flow that
437 * meets 'mf''s prerequisites. */
439 mf_get_mask(const struct mf_field *mf, const struct flow_wildcards *wc,
440 union mf_value *mask)
459 assert(mf->fww_bit != 0);
460 memset(mask, wc->wildcards & mf->fww_bit ? 0x00 : 0xff, mf->n_bytes);
464 mask->be64 = wc->tun_id_mask;
485 mask->be32 = htonl(wc->reg_masks[mf->id - MFF_REG0]);
489 memcpy(mask->mac, flow_wildcards_to_dl_dst_mask(wc->wildcards),
494 mask->be16 = wc->vlan_tci_mask;
497 mask->be16 = wc->vlan_tci_mask & htons(VLAN_VID_MASK);
500 mask->u8 = vlan_tci_to_pcp(wc->vlan_tci_mask);
504 mask->be32 = wc->nw_src_mask;
507 mask->be32 = wc->nw_dst_mask;
511 mask->ipv6 = wc->ipv6_src_mask;
514 mask->ipv6 = wc->ipv6_dst_mask;
518 mask->u8 = wc->tos_frag_mask & IP_DSCP_MASK;
521 mask->u8 = wc->tos_frag_mask & FLOW_FRAG_MASK;
525 mask->be32 = wc->nw_src_mask;
528 mask->be32 = wc->nw_dst_mask;
537 /* Tests whether 'mask' is a valid wildcard bit pattern for 'mf'. Returns true
538 * if the mask is valid, false otherwise. */
540 mf_is_mask_valid(const struct mf_field *mf, const union mf_value *mask)
542 switch (mf->maskable) {
544 return (is_all_zeros((const uint8_t *) mask, mf->n_bytes) ||
545 is_all_ones((const uint8_t *) mask, mf->n_bytes));
551 return (mf->n_bytes == 4
552 ? ip_is_cidr(mask->be32)
553 : ipv6_is_cidr(&mask->ipv6));
556 return flow_wildcards_is_dl_dst_mask_valid(mask->mac);
563 is_ip_any(const struct flow *flow)
565 return (flow->dl_type == htons(ETH_TYPE_IP) ||
566 flow->dl_type == htons(ETH_TYPE_IPV6));
570 is_icmpv4(const struct flow *flow)
572 return (flow->dl_type == htons(ETH_TYPE_IP)
573 && flow->nw_proto == IPPROTO_ICMP);
577 is_icmpv6(const struct flow *flow)
579 return (flow->dl_type == htons(ETH_TYPE_IPV6)
580 && flow->nw_proto == IPPROTO_ICMPV6);
583 /* Returns true if 'flow' meets the prerequisites for 'mf', false otherwise. */
585 mf_are_prereqs_ok(const struct mf_field *mf, const struct flow *flow)
587 switch (mf->prereqs) {
592 return flow->dl_type == htons(ETH_TYPE_ARP);
594 return flow->dl_type == htons(ETH_TYPE_IP);
596 return flow->dl_type == htons(ETH_TYPE_IPV6);
598 return is_ip_any(flow);
601 return is_ip_any(flow) && flow->nw_proto == IPPROTO_TCP;
603 return is_ip_any(flow) && flow->nw_proto == IPPROTO_UDP;
605 return is_icmpv6(flow);
607 return is_icmpv4(flow) || is_icmpv6(flow);
610 return (is_icmpv6(flow)
611 && flow->tp_dst == htons(0)
612 && (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT) ||
613 flow->tp_src == htons(ND_NEIGHBOR_ADVERT)));
615 return (is_icmpv6(flow)
616 && flow->tp_dst == htons(0)
617 && (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT)));
619 return (is_icmpv6(flow)
620 && flow->tp_dst == htons(0)
621 && (flow->tp_src == htons(ND_NEIGHBOR_ADVERT)));
627 /* Returns true if 'value' may be a valid value *as part of a masked match*,
630 * A value is not rejected just because it is not valid for the field in
631 * question, but only if it doesn't make sense to test the bits in question at
632 * all. For example, the MFF_VLAN_TCI field will never have a nonzero value
633 * without the VLAN_CFI bit being set, but we can't reject those values because
634 * it is still legitimate to test just for those bits (see the documentation
635 * for NXM_OF_VLAN_TCI in nicira-ext.h). On the other hand, there is never a
636 * reason to set the low bit of MFF_IP_TOS to 1, so we reject that. */
638 mf_is_value_valid(const struct mf_field *mf, const union mf_value *value)
686 return !(value->u8 & ~IP_DSCP_MASK);
688 return !(value->u8 & ~FLOW_FRAG_MASK);
691 return !(value->be16 & htons(0xff00));
694 return !(value->be16 & htons(VLAN_CFI | VLAN_PCP_MASK));
697 return !(value->u8 & ~7);
705 /* Copies the value of field 'mf' from 'flow' into 'value'. The caller is
706 * responsible for ensuring that 'flow' meets 'mf''s prerequisites. */
708 mf_get_value(const struct mf_field *mf, const struct flow *flow,
709 union mf_value *value)
713 value->be64 = flow->tun_id;
717 value->be16 = htons(flow->in_port);
738 value->be32 = htonl(flow->regs[mf->id - MFF_REG0]);
742 memcpy(value->mac, flow->dl_src, ETH_ADDR_LEN);
746 memcpy(value->mac, flow->dl_dst, ETH_ADDR_LEN);
750 value->be16 = flow->dl_type;
754 value->be16 = flow->vlan_tci;
758 value->be16 = flow->vlan_tci & htons(VLAN_VID_MASK);
762 value->u8 = vlan_tci_to_pcp(flow->vlan_tci);
766 value->be32 = flow->nw_src;
770 value->be32 = flow->nw_dst;
774 value->ipv6 = flow->ipv6_src;
778 value->ipv6 = flow->ipv6_dst;
782 value->u8 = flow->nw_proto;
786 value->u8 = flow->tos_frag & IP_DSCP_MASK;
790 value->u8 = flow->tos_frag & FLOW_FRAG_MASK;
794 value->be16 = htons(flow->nw_proto);
798 value->be32 = flow->nw_src;
802 value->be32 = flow->nw_dst;
807 memcpy(value->mac, flow->arp_sha, ETH_ADDR_LEN);
812 memcpy(value->mac, flow->arp_tha, ETH_ADDR_LEN);
816 value->be16 = flow->tp_src;
820 value->be16 = flow->tp_dst;
824 value->be16 = flow->tp_src;
828 value->be16 = flow->tp_dst;
832 value->u8 = ntohs(flow->tp_src);
836 value->u8 = ntohs(flow->tp_dst);
840 value->ipv6 = flow->nd_target;
849 /* Makes 'rule' match field 'mf' exactly, with the value matched taken from
850 * 'value'. The caller is responsible for ensuring that 'rule' meets 'mf''s
853 mf_set_value(const struct mf_field *mf,
854 const union mf_value *value, struct cls_rule *rule)
858 cls_rule_set_tun_id(rule, value->be64);
862 cls_rule_set_in_port(rule, ntohs(value->be16));
884 cls_rule_set_reg(rule, mf->id - MFF_REG0, ntohl(value->be32));
889 cls_rule_set_dl_src(rule, value->mac);
893 cls_rule_set_dl_dst(rule, value->mac);
897 cls_rule_set_dl_type(rule, value->be16);
901 cls_rule_set_dl_tci(rule, value->be16);
905 cls_rule_set_dl_vlan(rule, value->be16);
909 cls_rule_set_dl_vlan_pcp(rule, value->u8);
913 cls_rule_set_nw_src(rule, value->be32);
917 cls_rule_set_nw_dst(rule, value->be32);
921 cls_rule_set_ipv6_src(rule, &value->ipv6);
925 cls_rule_set_ipv6_dst(rule, &value->ipv6);
929 cls_rule_set_nw_proto(rule, value->u8);
933 cls_rule_set_nw_tos(rule, value->u8);
937 cls_rule_set_frag(rule, value->u8);
941 cls_rule_set_nw_proto(rule, ntohs(value->be16));
945 cls_rule_set_nw_src(rule, value->be32);
949 cls_rule_set_nw_dst(rule, value->be32);
954 cls_rule_set_arp_sha(rule, value->mac);
959 cls_rule_set_arp_tha(rule, value->mac);
963 cls_rule_set_tp_src(rule, value->be16);
967 cls_rule_set_tp_dst(rule, value->be16);
971 cls_rule_set_tp_src(rule, value->be16);
975 cls_rule_set_tp_dst(rule, value->be16);
979 cls_rule_set_icmp_type(rule, value->u8);
983 cls_rule_set_icmp_code(rule, value->u8);
987 cls_rule_set_nd_target(rule, &value->ipv6);
996 /* Makes 'rule' wildcard field 'mf'.
998 * The caller is responsible for ensuring that 'rule' meets 'mf''s
1001 mf_set_wild(const struct mf_field *mf, struct cls_rule *rule)
1005 cls_rule_set_tun_id_masked(rule, htonll(0), htonll(0));
1009 rule->wc.wildcards |= FWW_IN_PORT;
1010 rule->flow.in_port = 0;
1015 cls_rule_set_reg_masked(rule, 0, 0, 0);
1020 cls_rule_set_reg_masked(rule, 1, 0, 0);
1025 cls_rule_set_reg_masked(rule, 2, 0, 0);
1030 cls_rule_set_reg_masked(rule, 3, 0, 0);
1035 cls_rule_set_reg_masked(rule, 4, 0, 0);
1043 rule->wc.wildcards |= FWW_DL_SRC;
1044 memset(rule->flow.dl_src, 0, sizeof rule->flow.dl_src);
1048 rule->wc.wildcards |= FWW_DL_DST | FWW_ETH_MCAST;
1049 memset(rule->flow.dl_dst, 0, sizeof rule->flow.dl_dst);
1053 rule->wc.wildcards |= FWW_DL_TYPE;
1054 rule->flow.dl_type = htons(0);
1058 cls_rule_set_dl_tci_masked(rule, htons(0), htons(0));
1062 cls_rule_set_any_vid(rule);
1066 cls_rule_set_any_pcp(rule);
1071 cls_rule_set_nw_src_masked(rule, htonl(0), htonl(0));
1076 cls_rule_set_nw_dst_masked(rule, htonl(0), htonl(0));
1080 memset(&rule->wc.ipv6_src_mask, 0, sizeof rule->wc.ipv6_src_mask);
1081 memset(&rule->flow.ipv6_src, 0, sizeof rule->flow.ipv6_src);
1085 memset(&rule->wc.ipv6_dst_mask, 0, sizeof rule->wc.ipv6_dst_mask);
1086 memset(&rule->flow.ipv6_dst, 0, sizeof rule->flow.ipv6_dst);
1090 rule->wc.wildcards |= FWW_NW_PROTO;
1091 rule->flow.nw_proto = 0;
1095 rule->wc.tos_frag_mask |= IP_DSCP_MASK;
1096 rule->flow.tos_frag &= ~IP_DSCP_MASK;
1100 rule->wc.tos_frag_mask |= FLOW_FRAG_MASK;
1101 rule->flow.tos_frag &= ~FLOW_FRAG_MASK;
1105 rule->wc.wildcards |= FWW_NW_PROTO;
1106 rule->flow.nw_proto = 0;
1111 rule->wc.wildcards |= FWW_ARP_SHA;
1112 memset(rule->flow.arp_sha, 0, sizeof rule->flow.arp_sha);
1117 rule->wc.wildcards |= FWW_ARP_THA;
1118 memset(rule->flow.arp_tha, 0, sizeof rule->flow.arp_tha);
1124 rule->wc.wildcards |= FWW_TP_SRC;
1125 rule->flow.tp_src = htons(0);
1131 rule->wc.wildcards |= FWW_TP_DST;
1132 rule->flow.tp_dst = htons(0);
1136 rule->wc.wildcards |= FWW_ND_TARGET;
1137 memset(&rule->flow.nd_target, 0, sizeof rule->flow.nd_target);
1146 /* Makes 'rule' match field 'mf' with the specified 'value' and 'mask'.
1147 * 'value' specifies a value to match and 'mask' specifies a wildcard pattern,
1148 * with a 1-bit indicating that the corresponding value bit must match and a
1149 * 0-bit indicating a don't-care.
1151 * If 'mask' is NULL or points to all-1-bits, then this call is equivalent to
1152 * mf_set_value(mf, value, rule). If 'mask' points to all-0-bits, then this
1153 * call is equivalent to mf_set_wild(mf, rule).
1155 * 'mask' must be a valid mask for 'mf' (see mf_is_mask_valid()). The caller
1156 * is responsible for ensuring that 'rule' meets 'mf''s prerequisites. */
1158 mf_set(const struct mf_field *mf,
1159 const union mf_value *value, const union mf_value *mask,
1160 struct cls_rule *rule)
1162 if (!mask || is_all_ones((const uint8_t *) mask, mf->n_bytes)) {
1163 mf_set_value(mf, value, rule);
1165 } else if (is_all_zeros((const uint8_t *) mask, mf->n_bytes)) {
1166 mf_set_wild(mf, rule);
1193 cls_rule_set_tun_id_masked(rule, value->be64, mask->be64);
1214 cls_rule_set_reg_masked(rule, mf->id - MFF_REG0,
1215 ntohl(value->be32), ntohl(mask->be32));
1219 if (flow_wildcards_is_dl_dst_mask_valid(mask->mac)) {
1220 cls_rule_set_dl_dst_masked(rule, value->mac, mask->mac);
1225 cls_rule_set_dl_tci_masked(rule, value->be16, mask->be16);
1229 cls_rule_set_nw_src_masked(rule, value->be32, mask->be32);
1233 cls_rule_set_nw_dst_masked(rule, value->be32, mask->be32);
1237 cls_rule_set_ipv6_src_masked(rule, &value->ipv6, &mask->ipv6);
1241 cls_rule_set_ipv6_dst_masked(rule, &value->ipv6, &mask->ipv6);
1245 cls_rule_set_frag_masked(rule, value->u8, mask->u8);
1249 cls_rule_set_nw_src_masked(rule, value->be32, mask->be32);
1253 cls_rule_set_nw_dst_masked(rule, value->be32, mask->be32);
1262 /* Makes a subfield starting at bit offset 'ofs' and continuing for 'n_bits' in
1263 * 'rule''s field 'mf' exactly match the 'n_bits' least-significant bits of
1266 * Example: suppose that 'mf' is originally the following 2-byte field in
1269 * value == 0xe00a == 2#1110000000001010
1270 * mask == 0xfc3f == 2#1111110000111111
1272 * The call mf_set_subfield(mf, 0x55, 8, 7, rule) would have the following
1273 * effect (note that 0x55 is 2#1010101):
1275 * value == 0xd50a == 2#1101010100001010
1276 * mask == 0xff3f == 2#1111111100111111
1278 * The caller is responsible for ensuring that the result will be a valid
1279 * wildcard pattern for 'mf'. The caller is responsible for ensuring that
1280 * 'rule' meets 'mf''s prerequisites. */
1282 mf_set_subfield(const struct mf_field *mf, uint64_t x, unsigned int ofs,
1283 unsigned int n_bits, struct cls_rule *rule)
1285 if (ofs == 0 && mf->n_bytes * 8 == n_bits) {
1286 union mf_value value;
1289 for (i = mf->n_bytes - 1; i >= 0; i--) {
1290 ((uint8_t *) &value)[i] = x;
1293 mf_set_value(mf, &value, rule);
1295 union mf_value value, mask;
1297 unsigned int byte_ofs;
1299 mf_get(mf, rule, &value, &mask);
1301 byte_ofs = mf->n_bytes - ofs / 8;
1302 vp = &((uint8_t *) &value)[byte_ofs];
1303 mp = &((uint8_t *) &mask)[byte_ofs];
1305 unsigned int chunk = MIN(8 - ofs % 8, n_bits);
1306 uint8_t chunk_mask = ((1 << chunk) - 1) << (ofs % 8);
1308 *--vp &= ~chunk_mask;
1309 *vp |= chunk_mask & (x << (ofs % 8));
1310 *--mp |= chunk_mask;
1316 while (n_bits >= 8) {
1324 uint8_t chunk_mask = (1 << n_bits) - 1;
1326 *--vp &= ~chunk_mask;
1327 *vp |= chunk_mask & x;
1328 *--mp |= chunk_mask;
1331 mf_set(mf, &value, &mask, rule);
1335 /* Copies the value and wildcard bit pattern for 'mf' from 'rule' into the
1336 * 'value' and 'mask', respectively. */
1338 mf_get(const struct mf_field *mf, const struct cls_rule *rule,
1339 union mf_value *value, union mf_value *mask)
1341 mf_get_value(mf, &rule->flow, value);
1342 mf_get_mask(mf, &rule->wc, mask);
1345 /* Assigns a random value for field 'mf' to 'value'. */
1347 mf_random_value(const struct mf_field *mf, union mf_value *value)
1349 random_bytes(value, mf->n_bytes);
1401 value->u8 &= FLOW_FRAG_MASK;
1405 value->be16 &= htons(0xff);
1409 value->be16 &= htons(VLAN_VID_MASK);
1423 mf_from_integer_string(const struct mf_field *mf, const char *s,
1424 uint8_t *valuep, uint8_t *maskp)
1426 unsigned long long int integer, mask;
1431 integer = strtoull(s, &tail, 0);
1432 if (errno || (*tail != '\0' && *tail != '/')) {
1437 mask = strtoull(tail + 1, &tail, 0);
1438 if (errno || *tail != '\0') {
1445 for (i = mf->n_bytes - 1; i >= 0; i--) {
1446 valuep[i] = integer;
1452 return xasprintf("%s: value too large for %u-byte field %s",
1453 s, mf->n_bytes, mf->name);
1458 return xasprintf("%s: bad syntax for %s", s, mf->name);
1462 mf_from_ethernet_string(const struct mf_field *mf, const char *s,
1463 uint8_t mac[ETH_ADDR_LEN],
1464 uint8_t mask[ETH_ADDR_LEN])
1466 assert(mf->n_bytes == ETH_ADDR_LEN);
1468 switch (sscanf(s, ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT,
1469 ETH_ADDR_SCAN_ARGS(mac), ETH_ADDR_SCAN_ARGS(mask))){
1470 case ETH_ADDR_SCAN_COUNT * 2:
1473 case ETH_ADDR_SCAN_COUNT:
1474 memset(mask, 0xff, ETH_ADDR_LEN);
1478 return xasprintf("%s: invalid Ethernet address", s);
1483 mf_from_ipv4_string(const struct mf_field *mf, const char *s,
1484 ovs_be32 *ip, ovs_be32 *mask)
1488 assert(mf->n_bytes == sizeof *ip);
1490 if (sscanf(s, IP_SCAN_FMT"/"IP_SCAN_FMT,
1491 IP_SCAN_ARGS(ip), IP_SCAN_ARGS(mask)) == IP_SCAN_COUNT * 2) {
1493 } else if (sscanf(s, IP_SCAN_FMT"/%d",
1494 IP_SCAN_ARGS(ip), &prefix) == IP_SCAN_COUNT + 1) {
1495 if (prefix <= 0 || prefix > 32) {
1496 return xasprintf("%s: network prefix bits not between 1 and "
1498 } else if (prefix == 32) {
1499 *mask = htonl(UINT32_MAX);
1501 *mask = htonl(((1u << prefix) - 1) << (32 - prefix));
1503 } else if (sscanf(s, IP_SCAN_FMT, IP_SCAN_ARGS(ip)) == IP_SCAN_COUNT) {
1504 *mask = htonl(UINT32_MAX);
1506 return xasprintf("%s: invalid IP address", s);
1512 mf_from_ipv6_string(const struct mf_field *mf, const char *s,
1513 struct in6_addr *value, struct in6_addr *mask)
1515 char *str = xstrdup(s);
1516 char *save_ptr = NULL;
1517 const char *name, *netmask;
1520 assert(mf->n_bytes == sizeof *value);
1522 name = strtok_r(str, "/", &save_ptr);
1523 retval = name ? lookup_ipv6(name, value) : EINVAL;
1527 err = xasprintf("%s: could not convert to IPv6 address", str);
1533 netmask = strtok_r(NULL, "/", &save_ptr);
1535 int prefix = atoi(netmask);
1536 if (prefix <= 0 || prefix > 128) {
1538 return xasprintf("%s: prefix bits not between 1 and 128", s);
1540 *mask = ipv6_create_mask(prefix);
1543 *mask = in6addr_exact;
1551 mf_from_ofp_port_string(const struct mf_field *mf, const char *s,
1552 ovs_be16 *valuep, ovs_be16 *maskp)
1556 assert(mf->n_bytes == sizeof(ovs_be16));
1557 if (ofputil_port_from_string(s, &port)) {
1558 *valuep = htons(port);
1559 *maskp = htons(UINT16_MAX);
1562 return mf_from_integer_string(mf, s,
1563 (uint8_t *) valuep, (uint8_t *) maskp);
1567 struct frag_handling {
1573 static const struct frag_handling all_frags[] = {
1574 #define A FLOW_FRAG_ANY
1575 #define L FLOW_FRAG_LATER
1576 /* name mask value */
1579 { "first", A|L, A },
1580 { "later", A|L, A|L },
1585 { "not_later", L, 0 },
1592 mf_from_frag_string(const char *s, uint8_t *valuep, uint8_t *maskp)
1594 const struct frag_handling *h;
1596 for (h = all_frags; h < &all_frags[ARRAY_SIZE(all_frags)]; h++) {
1597 if (!strcasecmp(s, h->name)) {
1598 /* We force the upper bits of the mask on to make mf_parse_value()
1599 * happy (otherwise it will never think it's an exact match.) */
1600 *maskp = h->mask | ~FLOW_FRAG_MASK;
1606 return xasprintf("%s: unknown fragment type (valid types are \"no\", "
1607 "\"yes\", \"first\", \"later\", \"not_first\"", s);
1610 /* Parses 's', a string value for field 'mf', into 'value' and 'mask'. Returns
1611 * NULL if successful, otherwise a malloc()'d string describing the error. */
1613 mf_parse(const struct mf_field *mf, const char *s,
1614 union mf_value *value, union mf_value *mask)
1616 if (!strcasecmp(s, "any") || !strcmp(s, "*")) {
1617 memset(value, 0, mf->n_bytes);
1618 memset(mask, 0, mf->n_bytes);
1622 switch (mf->string) {
1624 case MFS_HEXADECIMAL:
1625 return mf_from_integer_string(mf, s,
1626 (uint8_t *) value, (uint8_t *) mask);
1629 return mf_from_ethernet_string(mf, s, value->mac, mask->mac);
1632 return mf_from_ipv4_string(mf, s, &value->be32, &mask->be32);
1635 return mf_from_ipv6_string(mf, s, &value->ipv6, &mask->ipv6);
1638 return mf_from_ofp_port_string(mf, s, &value->be16, &mask->be16);
1641 return mf_from_frag_string(s, &value->u8, &mask->u8);
1646 /* Parses 's', a string value for field 'mf', into 'value'. Returns NULL if
1647 * successful, otherwise a malloc()'d string describing the error. */
1649 mf_parse_value(const struct mf_field *mf, const char *s, union mf_value *value)
1651 union mf_value mask;
1654 error = mf_parse(mf, s, value, &mask);
1659 if (!is_all_ones((const uint8_t *) &mask, mf->n_bytes)) {
1660 return xasprintf("%s: wildcards not allowed here", s);
1666 mf_format_integer_string(const struct mf_field *mf, const uint8_t *valuep,
1667 const uint8_t *maskp, struct ds *s)
1669 unsigned long long int integer;
1672 assert(mf->n_bytes <= 8);
1675 for (i = 0; i < mf->n_bytes; i++) {
1676 integer = (integer << 8) | valuep[i];
1678 if (mf->string == MFS_HEXADECIMAL) {
1679 ds_put_format(s, "%#llx", integer);
1681 ds_put_format(s, "%lld", integer);
1685 unsigned long long int mask;
1688 for (i = 0; i < mf->n_bytes; i++) {
1689 mask = (mask << 8) | maskp[i];
1692 /* I guess we could write the mask in decimal for MFS_DECIMAL but I'm
1693 * not sure that that a bit-mask written in decimal is ever easier to
1694 * understand than the same bit-mask written in hexadecimal. */
1695 ds_put_format(s, "/%#llx", mask);
1700 mf_format_frag_string(const uint8_t *valuep, const uint8_t *maskp,
1703 const struct frag_handling *h;
1704 uint8_t value = *valuep;
1705 uint8_t mask = *maskp;
1708 mask &= FLOW_FRAG_MASK;
1710 for (h = all_frags; h < &all_frags[ARRAY_SIZE(all_frags)]; h++) {
1711 if (value == h->value && mask == h->mask) {
1712 ds_put_cstr(s, h->name);
1716 ds_put_cstr(s, "<error>");
1719 /* Appends to 's' a string representation of field 'mf' whose value is in
1720 * 'value' and 'mask'. 'mask' may be NULL to indicate an exact match. */
1722 mf_format(const struct mf_field *mf,
1723 const union mf_value *value, const union mf_value *mask,
1727 if (is_all_zeros((const uint8_t *) mask, mf->n_bytes)) {
1728 ds_put_cstr(s, "ANY");
1730 } else if (is_all_ones((const uint8_t *) mask, mf->n_bytes)) {
1735 switch (mf->string) {
1738 ofputil_format_port(ntohs(value->be16), s);
1743 case MFS_HEXADECIMAL:
1744 mf_format_integer_string(mf, (uint8_t *) value, (uint8_t *) mask, s);
1748 ds_put_format(s, ETH_ADDR_FMT, ETH_ADDR_ARGS(value->mac));
1750 ds_put_format(s, "/"ETH_ADDR_FMT, ETH_ADDR_ARGS(mask->mac));
1755 ip_format_masked(value->be32, mask ? mask->be32 : htonl(UINT32_MAX),
1760 print_ipv6_masked(s, &value->ipv6, mask ? &mask->ipv6 : NULL);
1764 mf_format_frag_string(&value->u8, &mask->u8, s);