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,
306 is_all_zeros(const uint8_t *field, size_t length)
310 for (i = 0; i < length; i++) {
311 if (field[i] != 0x00) {
319 is_all_ones(const uint8_t *field, size_t length)
323 for (i = 0; i < length; i++) {
324 if (field[i] != 0xff) {
331 /* Returns the field with the given 'id'. */
332 const struct mf_field *
333 mf_from_id(enum mf_field_id id)
335 assert((unsigned int) id < MFF_N_IDS);
336 return &mf_fields[id];
339 /* Returns the field with the given 'name', or a null pointer if no field has
341 const struct mf_field *
342 mf_from_name(const char *name)
344 static struct shash mf_by_name = SHASH_INITIALIZER(&mf_by_name);
346 if (shash_is_empty(&mf_by_name)) {
347 const struct mf_field *mf;
349 for (mf = mf_fields; mf < &mf_fields[MFF_N_IDS]; mf++) {
350 shash_add_once(&mf_by_name, mf->name, mf);
351 if (mf->extra_name) {
352 shash_add_once(&mf_by_name, mf->extra_name, mf);
357 return shash_find_data(&mf_by_name, name);
360 /* Returns true if 'wc' wildcards all the bits in field 'mf', false if 'wc'
361 * specifies at least one bit in the field.
363 * The caller is responsible for ensuring that 'wc' corresponds to a flow that
364 * meets 'mf''s prerequisites. */
366 mf_is_all_wild(const struct mf_field *mf, const struct flow_wildcards *wc)
386 assert(mf->fww_bit != 0);
387 return (wc->wildcards & mf->fww_bit) != 0;
390 return !wc->tun_id_mask;
407 return !wc->reg_masks[mf->id - MFF_REG0];
410 return ((wc->wildcards & (FWW_ETH_MCAST | FWW_DL_DST))
411 == (FWW_ETH_MCAST | FWW_DL_DST));
414 return !wc->vlan_tci_mask;
416 return !(wc->vlan_tci_mask & htons(VLAN_VID_MASK));
418 return !(wc->vlan_tci_mask & htons(VLAN_PCP_MASK));
421 return !wc->nw_src_mask;
423 return !wc->nw_dst_mask;
426 return ipv6_mask_is_any(&wc->ipv6_src_mask);
428 return ipv6_mask_is_any(&wc->ipv6_dst_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;
492 mask->be32 = htonl(wc->reg_masks[mf->id - MFF_REG0]);
496 memcpy(mask->mac, flow_wildcards_to_dl_dst_mask(wc->wildcards),
501 mask->be16 = wc->vlan_tci_mask;
504 mask->be16 = wc->vlan_tci_mask & htons(VLAN_VID_MASK);
507 mask->u8 = vlan_tci_to_pcp(wc->vlan_tci_mask);
511 mask->be32 = wc->nw_src_mask;
514 mask->be32 = wc->nw_dst_mask;
518 mask->ipv6 = wc->ipv6_src_mask;
521 mask->ipv6 = wc->ipv6_dst_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->icmp_code == htons(0)
612 && (flow->icmp_type == htons(ND_NEIGHBOR_SOLICIT) ||
613 flow->icmp_type == htons(ND_NEIGHBOR_ADVERT)));
615 return (is_icmpv6(flow)
616 && flow->icmp_code == htons(0)
617 && (flow->icmp_type == htons(ND_NEIGHBOR_SOLICIT)));
619 return (is_icmpv6(flow)
620 && flow->icmp_code == htons(0)
621 && (flow->icmp_type == 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)
683 return !(value->u8 & 0x03);
686 return !(value->be16 & htons(0xff00));
689 return !(value->be16 & htons(VLAN_CFI | VLAN_PCP_MASK));
692 return !(value->u8 & ~7);
700 /* Copies the value of field 'mf' from 'flow' into 'value'. The caller is
701 * responsible for ensuring that 'flow' meets 'mf''s prerequisites. */
703 mf_get_value(const struct mf_field *mf, const struct flow *flow,
704 union mf_value *value)
708 value->be64 = flow->tun_id;
712 value->be16 = htons(flow->in_port);
730 value->be32 = htonl(flow->regs[0]);
734 memcpy(value->mac, flow->dl_src, ETH_ADDR_LEN);
738 memcpy(value->mac, flow->dl_dst, ETH_ADDR_LEN);
742 value->be16 = flow->dl_type;
746 value->be16 = flow->vlan_tci;
750 value->be16 = flow->vlan_tci & htons(VLAN_VID_MASK);
754 value->u8 = vlan_tci_to_pcp(flow->vlan_tci);
758 value->be32 = flow->nw_src;
762 value->be32 = flow->nw_dst;
766 value->ipv6 = flow->ipv6_src;
770 value->ipv6 = flow->ipv6_dst;
774 value->u8 = flow->nw_proto;
778 value->u8 = flow->nw_tos;
782 value->be16 = htons(flow->nw_proto);
786 value->be32 = flow->nw_src;
790 value->be32 = flow->nw_dst;
795 memcpy(value->mac, flow->arp_sha, ETH_ADDR_LEN);
800 memcpy(value->mac, flow->arp_tha, ETH_ADDR_LEN);
804 value->be16 = flow->tp_src;
808 value->be16 = flow->tp_dst;
812 value->be16 = flow->tp_src;
816 value->be16 = flow->tp_dst;
820 value->u8 = ntohs(flow->tp_src);
824 value->u8 = ntohs(flow->tp_dst);
828 value->ipv6 = flow->nd_target;
837 /* Makes 'rule' match field 'mf' exactly, with the value matched taken from
838 * 'value'. The caller is responsible for ensuring that 'rule' meets 'mf''s
841 mf_set_value(const struct mf_field *mf,
842 const union mf_value *value, struct cls_rule *rule)
846 cls_rule_set_tun_id(rule, value->be64);
850 cls_rule_set_in_port(rule, ntohs(value->be16));
869 cls_rule_set_reg(rule, mf->id - MFF_REG0, ntohl(value->be32));
874 cls_rule_set_dl_src(rule, value->mac);
878 cls_rule_set_dl_dst(rule, value->mac);
882 cls_rule_set_dl_type(rule, value->be16);
886 cls_rule_set_dl_tci(rule, value->be16);
890 cls_rule_set_dl_vlan(rule, value->be16);
894 cls_rule_set_dl_vlan_pcp(rule, value->u8);
898 cls_rule_set_nw_src(rule, value->be32);
902 cls_rule_set_nw_dst(rule, value->be32);
906 cls_rule_set_ipv6_src(rule, &value->ipv6);
910 cls_rule_set_ipv6_dst(rule, &value->ipv6);
914 cls_rule_set_nw_proto(rule, value->u8);
918 cls_rule_set_nw_tos(rule, value->u8);
922 cls_rule_set_nw_proto(rule, ntohs(value->be16));
926 cls_rule_set_nw_src(rule, value->be32);
930 cls_rule_set_nw_dst(rule, value->be32);
935 cls_rule_set_arp_sha(rule, value->mac);
940 cls_rule_set_arp_tha(rule, value->mac);
944 cls_rule_set_tp_src(rule, value->be16);
948 cls_rule_set_tp_dst(rule, value->be16);
952 cls_rule_set_tp_src(rule, value->be16);
956 cls_rule_set_tp_dst(rule, value->be16);
960 cls_rule_set_icmp_type(rule, value->u8);
964 cls_rule_set_icmp_code(rule, value->u8);
968 cls_rule_set_nd_target(rule, &value->ipv6);
977 /* Makes 'rule' wildcard field 'mf'.
979 * The caller is responsible for ensuring that 'rule' meets 'mf''s
982 mf_set_wild(const struct mf_field *mf, struct cls_rule *rule)
986 cls_rule_set_tun_id_masked(rule, htonll(0), htonll(0));
990 rule->wc.wildcards |= FWW_IN_PORT;
991 rule->flow.in_port = 0;
996 cls_rule_set_reg_masked(rule, 0, 0, 0);
1001 cls_rule_set_reg_masked(rule, 1, 0, 0);
1006 cls_rule_set_reg_masked(rule, 2, 0, 0);
1011 cls_rule_set_reg_masked(rule, 3, 0, 0);
1019 rule->wc.wildcards |= FWW_DL_SRC;
1020 memset(rule->flow.dl_src, 0, sizeof rule->flow.dl_src);
1024 rule->wc.wildcards |= FWW_DL_DST | FWW_ETH_MCAST;
1025 memset(rule->flow.dl_dst, 0, sizeof rule->flow.dl_dst);
1029 rule->wc.wildcards |= FWW_DL_TYPE;
1030 rule->flow.dl_type = htons(0);
1034 cls_rule_set_dl_tci_masked(rule, htons(0), htons(0));
1038 cls_rule_set_any_vid(rule);
1042 cls_rule_set_any_pcp(rule);
1047 cls_rule_set_nw_src_masked(rule, htonl(0), htonl(0));
1052 cls_rule_set_nw_dst_masked(rule, htonl(0), htonl(0));
1056 memset(&rule->wc.ipv6_src_mask, 0, sizeof rule->wc.ipv6_src_mask);
1057 memset(&rule->flow.ipv6_src, 0, sizeof rule->flow.ipv6_src);
1061 memset(&rule->wc.ipv6_dst_mask, 0, sizeof rule->wc.ipv6_dst_mask);
1062 memset(&rule->flow.ipv6_dst, 0, sizeof rule->flow.ipv6_dst);
1066 rule->wc.wildcards |= FWW_NW_PROTO;
1067 rule->flow.nw_proto = 0;
1071 rule->wc.wildcards |= FWW_NW_TOS;
1072 rule->flow.nw_tos = 0;
1076 rule->wc.wildcards |= FWW_NW_PROTO;
1077 rule->flow.nw_proto = 0;
1082 rule->wc.wildcards |= FWW_ARP_SHA;
1083 memset(rule->flow.arp_sha, 0, sizeof rule->flow.arp_sha);
1088 rule->wc.wildcards |= FWW_ARP_THA;
1089 memset(rule->flow.arp_tha, 0, sizeof rule->flow.arp_tha);
1095 rule->wc.wildcards |= FWW_TP_SRC;
1096 rule->flow.tp_src = htons(0);
1102 rule->wc.wildcards |= FWW_TP_DST;
1103 rule->flow.tp_dst = htons(0);
1107 rule->wc.wildcards |= FWW_ND_TARGET;
1108 memset(&rule->flow.nd_target, 0, sizeof rule->flow.nd_target);
1117 /* Makes 'rule' match field 'mf' with the specified 'value' and 'mask'.
1118 * 'value' specifies a value to match and 'mask' specifies a wildcard pattern,
1119 * with a 1-bit indicating that the corresponding value bit must match and a
1120 * 0-bit indicating a don't-care.
1122 * If 'mask' is NULL or points to all-1-bits, then this call is equivalent to
1123 * mf_set_value(mf, value, rule). If 'mask' points to all-0-bits, then this
1124 * call is equivalent to mf_set_wild(mf, rule).
1126 * 'mask' must be a valid mask for 'mf' (see mf_is_mask_valid()). The caller
1127 * is responsible for ensuring that 'rule' meets 'mf''s prerequisites. */
1129 mf_set(const struct mf_field *mf,
1130 const union mf_value *value, const union mf_value *mask,
1131 struct cls_rule *rule)
1133 if (!mask || is_all_ones((const uint8_t *) mask, mf->n_bytes)) {
1134 mf_set_value(mf, value, rule);
1136 } else if (is_all_zeros((const uint8_t *) mask, mf->n_bytes)) {
1137 mf_set_wild(mf, rule);
1164 cls_rule_set_tun_id_masked(rule, value->be64, mask->be64);
1182 cls_rule_set_reg_masked(rule, mf->id - MFF_REG0,
1183 ntohl(value->be32), ntohl(mask->be32));
1187 if (flow_wildcards_is_dl_dst_mask_valid(mask->mac)) {
1188 cls_rule_set_dl_dst_masked(rule, value->mac, mask->mac);
1193 cls_rule_set_dl_tci_masked(rule, value->be16, mask->be16);
1197 cls_rule_set_nw_src_masked(rule, value->be32, mask->be32);
1201 cls_rule_set_nw_dst_masked(rule, value->be32, mask->be32);
1205 cls_rule_set_ipv6_src_masked(rule, &value->ipv6, &mask->ipv6);
1209 cls_rule_set_ipv6_dst_masked(rule, &value->ipv6, &mask->ipv6);
1213 cls_rule_set_nw_src_masked(rule, value->be32, mask->be32);
1217 cls_rule_set_nw_dst_masked(rule, value->be32, mask->be32);
1226 /* Makes a subfield starting at bit offset 'ofs' and continuing for 'n_bits' in
1227 * 'rule''s field 'mf' exactly match the 'n_bits' least-significant bits of
1230 * Example: suppose that 'mf' is originally the following 2-byte field in
1233 * value == 0xe00a == 2#1110000000001010
1234 * mask == 0xfc3f == 2#1111110000111111
1236 * The call mf_set_subfield(mf, 0x55, 8, 7, rule) would have the following
1237 * effect (note that 0x55 is 2#1010101):
1239 * value == 0xd50a == 2#1101010100001010
1240 * mask == 0xff3f == 2#1111111100111111
1242 * The caller is responsible for ensuring that the result will be a valid
1243 * wildcard pattern for 'mf'. The caller is responsible for ensuring that
1244 * 'rule' meets 'mf''s prerequisites. */
1246 mf_set_subfield(const struct mf_field *mf, uint64_t x, unsigned int ofs,
1247 unsigned int n_bits, struct cls_rule *rule)
1249 if (ofs == 0 && mf->n_bytes * 8 == n_bits) {
1250 union mf_value value;
1253 for (i = mf->n_bytes - 1; i >= 0; i--) {
1254 ((uint8_t *) &value)[i] = x;
1257 mf_set_value(mf, &value, rule);
1259 union mf_value value, mask;
1261 unsigned int byte_ofs;
1263 mf_get(mf, rule, &value, &mask);
1265 byte_ofs = mf->n_bytes - ofs / 8;
1266 vp = &((uint8_t *) &value)[byte_ofs];
1267 mp = &((uint8_t *) &mask)[byte_ofs];
1269 unsigned int chunk = MIN(8 - ofs % 8, n_bits);
1270 uint8_t chunk_mask = ((1 << chunk) - 1) << (ofs % 8);
1272 *--vp &= ~chunk_mask;
1273 *vp |= chunk_mask & (x << (ofs % 8));
1274 *--mp |= chunk_mask;
1280 while (n_bits >= 8) {
1288 uint8_t chunk_mask = (1 << n_bits) - 1;
1290 *--vp &= ~chunk_mask;
1291 *vp |= chunk_mask & x;
1292 *--mp |= chunk_mask;
1295 mf_set(mf, &value, &mask, rule);
1299 /* Copies the value and wildcard bit pattern for 'mf' from 'rule' into the
1300 * 'value' and 'mask', respectively. */
1302 mf_get(const struct mf_field *mf, const struct cls_rule *rule,
1303 union mf_value *value, union mf_value *mask)
1305 mf_get_value(mf, &rule->flow, value);
1306 mf_get_mask(mf, &rule->wc, mask);
1309 /* Assigns a random value for field 'mf' to 'value'. */
1311 mf_random_value(const struct mf_field *mf, union mf_value *value)
1313 random_bytes(value, mf->n_bytes);
1362 value->be16 &= htons(0xff);
1366 value->be16 &= htons(VLAN_VID_MASK);
1380 mf_from_integer_string(const struct mf_field *mf, const char *s,
1381 uint8_t *valuep, uint8_t *maskp)
1383 unsigned long long int integer, mask;
1388 integer = strtoull(s, &tail, 0);
1389 if (errno || (*tail != '\0' && *tail != '/')) {
1394 mask = strtoull(tail + 1, &tail, 0);
1395 if (errno || *tail != '\0') {
1402 for (i = mf->n_bytes - 1; i >= 0; i--) {
1403 valuep[i] = integer;
1409 return xasprintf("%s: value too large for %u-byte field %s",
1410 s, mf->n_bytes, mf->name);
1415 return xasprintf("%s: bad syntax for %s", s, mf->name);
1419 mf_from_ethernet_string(const struct mf_field *mf, const char *s,
1420 uint8_t mac[ETH_ADDR_LEN],
1421 uint8_t mask[ETH_ADDR_LEN])
1423 assert(mf->n_bytes == ETH_ADDR_LEN);
1425 switch (sscanf(s, ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT,
1426 ETH_ADDR_SCAN_ARGS(mac), ETH_ADDR_SCAN_ARGS(mask))){
1427 case ETH_ADDR_SCAN_COUNT * 2:
1430 case ETH_ADDR_SCAN_COUNT:
1431 memset(mask, 0xff, ETH_ADDR_LEN);
1435 return xasprintf("%s: invalid Ethernet address", s);
1440 mf_from_ipv4_string(const struct mf_field *mf, const char *s,
1441 ovs_be32 *ip, ovs_be32 *mask)
1445 assert(mf->n_bytes == sizeof *ip);
1447 if (sscanf(s, IP_SCAN_FMT"/"IP_SCAN_FMT,
1448 IP_SCAN_ARGS(ip), IP_SCAN_ARGS(mask)) == IP_SCAN_COUNT * 2) {
1450 } else if (sscanf(s, IP_SCAN_FMT"/%d",
1451 IP_SCAN_ARGS(ip), &prefix) == IP_SCAN_COUNT + 1) {
1452 if (prefix <= 0 || prefix > 32) {
1453 return xasprintf("%s: network prefix bits not between 1 and "
1455 } else if (prefix == 32) {
1456 *mask = htonl(UINT32_MAX);
1458 *mask = htonl(((1u << prefix) - 1) << (32 - prefix));
1460 } else if (sscanf(s, IP_SCAN_FMT, IP_SCAN_ARGS(ip)) == IP_SCAN_COUNT) {
1461 *mask = htonl(UINT32_MAX);
1463 return xasprintf("%s: invalid IP address", s);
1469 mf_from_ipv6_string(const struct mf_field *mf, const char *s,
1470 struct in6_addr *value, struct in6_addr *mask)
1472 char *str = xstrdup(s);
1473 char *save_ptr = NULL;
1474 const char *name, *netmask;
1477 assert(mf->n_bytes == sizeof *value);
1479 name = strtok_r(str, "/", &save_ptr);
1480 retval = name ? lookup_ipv6(name, value) : EINVAL;
1484 err = xasprintf("%s: could not convert to IPv6 address", str);
1490 netmask = strtok_r(NULL, "/", &save_ptr);
1492 int prefix = atoi(netmask);
1493 if (prefix <= 0 || prefix > 128) {
1495 return xasprintf("%s: prefix bits not between 1 and 128", s);
1497 *mask = ipv6_create_mask(prefix);
1500 *mask = in6addr_exact;
1508 mf_from_ofp_port_string(const struct mf_field *mf, const char *s,
1509 ovs_be16 *valuep, ovs_be16 *maskp)
1513 assert(mf->n_bytes == sizeof(ovs_be16));
1514 if (ofputil_port_from_string(s, &port)) {
1515 *valuep = htons(port);
1516 *maskp = htons(UINT16_MAX);
1519 return mf_from_integer_string(mf, s,
1520 (uint8_t *) valuep, (uint8_t *) maskp);
1524 /* Parses 's', a string value for field 'mf', into 'value' and 'mask'. Returns
1525 * NULL if successful, otherwise a malloc()'d string describing the error. */
1527 mf_parse(const struct mf_field *mf, const char *s,
1528 union mf_value *value, union mf_value *mask)
1530 if (!strcasecmp(s, "any") || !strcmp(s, "*")) {
1531 memset(value, 0, mf->n_bytes);
1532 memset(mask, 0, mf->n_bytes);
1536 switch (mf->string) {
1538 case MFS_HEXADECIMAL:
1539 return mf_from_integer_string(mf, s,
1540 (uint8_t *) value, (uint8_t *) mask);
1543 return mf_from_ethernet_string(mf, s, value->mac, mask->mac);
1546 return mf_from_ipv4_string(mf, s, &value->be32, &mask->be32);
1549 return mf_from_ipv6_string(mf, s, &value->ipv6, &mask->ipv6);
1552 return mf_from_ofp_port_string(mf, s, &value->be16, &mask->be16);
1557 /* Parses 's', a string value for field 'mf', into 'value'. Returns NULL if
1558 * successful, otherwise a malloc()'d string describing the error. */
1560 mf_parse_value(const struct mf_field *mf, const char *s, union mf_value *value)
1562 union mf_value mask;
1565 error = mf_parse(mf, s, value, &mask);
1570 if (!is_all_ones((const uint8_t *) &mask, mf->n_bytes)) {
1571 return xasprintf("%s: wildcards not allowed here", s);
1577 mf_format_integer_string(const struct mf_field *mf, const uint8_t *valuep,
1578 const uint8_t *maskp, struct ds *s)
1580 unsigned long long int integer;
1583 assert(mf->n_bytes <= 8);
1586 for (i = 0; i < mf->n_bytes; i++) {
1587 integer = (integer << 8) | valuep[i];
1589 if (mf->string == MFS_HEXADECIMAL) {
1590 ds_put_format(s, "%#llx", integer);
1592 ds_put_format(s, "%lld", integer);
1596 unsigned long long int mask;
1599 for (i = 0; i < mf->n_bytes; i++) {
1600 mask = (mask << 8) | maskp[i];
1603 /* I guess we could write the mask in decimal for MFS_DECIMAL but I'm
1604 * not sure that that a bit-mask written in decimal is ever easier to
1605 * understand than the same bit-mask written in hexadecimal. */
1606 ds_put_format(s, "/%#llx", mask);
1610 /* Appends to 's' a string representation of field 'mf' whose value is in
1611 * 'value' and 'mask'. 'mask' may be NULL to indicate an exact match. */
1613 mf_format(const struct mf_field *mf,
1614 const union mf_value *value, const union mf_value *mask,
1618 if (is_all_zeros((const uint8_t *) mask, mf->n_bytes)) {
1619 ds_put_cstr(s, "ANY");
1621 } else if (is_all_ones((const uint8_t *) mask, mf->n_bytes)) {
1626 switch (mf->string) {
1629 ofputil_format_port(ntohs(value->be16), s);
1634 case MFS_HEXADECIMAL:
1635 mf_format_integer_string(mf, (uint8_t *) value, (uint8_t *) mask, s);
1639 ds_put_format(s, ETH_ADDR_FMT, ETH_ADDR_ARGS(value->mac));
1641 ds_put_format(s, "/"ETH_ADDR_FMT, ETH_ADDR_ARGS(mask->mac));
1646 ip_format_masked(value->be32, mask ? mask->be32 : htonl(UINT32_MAX),
1651 print_ipv6_masked(s, &value->ipv6, mask ? &mask->ipv6 : NULL);