]> git.proxmox.com Git - mirror_ovs.git/blob - lib/meta-flow.c
Add connection tracking label support.
[mirror_ovs.git] / lib / meta-flow.c
1 /*
2 * Copyright (c) 2011, 2012, 2013, 2014, 2015 Nicira, Inc.
3 *
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:
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17 #include <config.h>
18
19 #include "meta-flow.h"
20
21 #include <errno.h>
22 #include <limits.h>
23 #include <netinet/icmp6.h>
24 #include <netinet/ip6.h>
25
26 #include "classifier.h"
27 #include "dynamic-string.h"
28 #include "nx-match.h"
29 #include "ofp-errors.h"
30 #include "ofp-util.h"
31 #include "ovs-thread.h"
32 #include "packets.h"
33 #include "random.h"
34 #include "shash.h"
35 #include "socket-util.h"
36 #include "tun-metadata.h"
37 #include "unaligned.h"
38 #include "util.h"
39 #include "openvswitch/vlog.h"
40
41 VLOG_DEFINE_THIS_MODULE(meta_flow);
42
43 #define FLOW_U32OFS(FIELD) \
44 offsetof(struct flow, FIELD) % 4 ? -1 : offsetof(struct flow, FIELD) / 4
45
46 #define MF_FIELD_SIZES(MEMBER) \
47 sizeof ((union mf_value *)0)->MEMBER, \
48 8 * sizeof ((union mf_value *)0)->MEMBER
49
50 extern const struct mf_field mf_fields[MFF_N_IDS]; /* Silence a warning. */
51
52 const struct mf_field mf_fields[MFF_N_IDS] = {
53 #include "meta-flow.inc"
54 };
55
56 /* Maps from an mf_field's 'name' or 'extra_name' to the mf_field. */
57 static struct shash mf_by_name;
58
59 /* Rate limit for parse errors. These always indicate a bug in an OpenFlow
60 * controller and so there's not much point in showing a lot of them. */
61 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
62
63 #define MF_VALUE_EXACT_8 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
64 #define MF_VALUE_EXACT_16 MF_VALUE_EXACT_8, MF_VALUE_EXACT_8
65 #define MF_VALUE_EXACT_32 MF_VALUE_EXACT_16, MF_VALUE_EXACT_16
66 #define MF_VALUE_EXACT_64 MF_VALUE_EXACT_32, MF_VALUE_EXACT_32
67 #define MF_VALUE_EXACT_128 MF_VALUE_EXACT_64, MF_VALUE_EXACT_64
68 #define MF_VALUE_EXACT_INITIALIZER { .tun_metadata = { MF_VALUE_EXACT_128 } }
69
70 const union mf_value exact_match_mask = MF_VALUE_EXACT_INITIALIZER;
71
72 static void nxm_init(void);
73
74 /* Returns the field with the given 'name', or a null pointer if no field has
75 * that name. */
76 const struct mf_field *
77 mf_from_name(const char *name)
78 {
79 nxm_init();
80 return shash_find_data(&mf_by_name, name);
81 }
82
83 static void
84 nxm_do_init(void)
85 {
86 int i;
87
88 shash_init(&mf_by_name);
89 for (i = 0; i < MFF_N_IDS; i++) {
90 const struct mf_field *mf = &mf_fields[i];
91
92 ovs_assert(mf->id == i); /* Fields must be in the enum order. */
93
94 shash_add_once(&mf_by_name, mf->name, mf);
95 if (mf->extra_name) {
96 shash_add_once(&mf_by_name, mf->extra_name, mf);
97 }
98 }
99 }
100
101 static void
102 nxm_init(void)
103 {
104 static pthread_once_t once = PTHREAD_ONCE_INIT;
105 pthread_once(&once, nxm_do_init);
106 }
107
108 /* Consider the two value/mask pairs 'a_value/a_mask' and 'b_value/b_mask' as
109 * restrictions on a field's value. Then, this function initializes
110 * 'dst_value/dst_mask' such that it combines the restrictions of both pairs.
111 * This is not always possible, i.e. if one pair insists on a value of 0 in
112 * some bit and the other pair insists on a value of 1 in that bit. This
113 * function returns false in a case where the combined restriction is
114 * impossible (in which case 'dst_value/dst_mask' is not fully initialized),
115 * true otherwise.
116 *
117 * (As usually true for value/mask pairs in OVS, any 1-bit in a value must have
118 * a corresponding 1-bit in its mask.) */
119 bool
120 mf_subvalue_intersect(const union mf_subvalue *a_value,
121 const union mf_subvalue *a_mask,
122 const union mf_subvalue *b_value,
123 const union mf_subvalue *b_mask,
124 union mf_subvalue *dst_value,
125 union mf_subvalue *dst_mask)
126 {
127 for (int i = 0; i < ARRAY_SIZE(a_value->be64); i++) {
128 ovs_be64 av = a_value->be64[i];
129 ovs_be64 am = a_mask->be64[i];
130 ovs_be64 bv = b_value->be64[i];
131 ovs_be64 bm = b_mask->be64[i];
132 ovs_be64 *dv = &dst_value->be64[i];
133 ovs_be64 *dm = &dst_mask->be64[i];
134
135 if ((av ^ bv) & (am & bm)) {
136 return false;
137 }
138 *dv = av | bv;
139 *dm = am | bm;
140 }
141 return true;
142 }
143
144 /* Returns the "number of bits" in 'v', e.g. 1 if only the lowest-order bit is
145 * set, 2 if the second-lowest-order bit is set, and so on. */
146 int
147 mf_subvalue_width(const union mf_subvalue *v)
148 {
149 return 1 + bitwise_rscan(v, sizeof *v, true, sizeof *v * 8 - 1, -1);
150 }
151
152 /* For positive 'n', shifts the bits in 'value' 'n' bits to the left, and for
153 * negative 'n', shifts the bits '-n' bits to the right. */
154 void
155 mf_subvalue_shift(union mf_subvalue *value, int n)
156 {
157 if (n) {
158 union mf_subvalue tmp;
159 memset(&tmp, 0, sizeof tmp);
160
161 if (n > 0 && n < 8 * sizeof tmp) {
162 bitwise_copy(value, sizeof *value, 0,
163 &tmp, sizeof tmp, n,
164 8 * sizeof tmp - n);
165 } else if (n < 0 && n > -8 * sizeof tmp) {
166 bitwise_copy(value, sizeof *value, -n,
167 &tmp, sizeof tmp, 0,
168 8 * sizeof tmp + n);
169 }
170 *value = tmp;
171 }
172 }
173
174 /* Returns true if 'wc' wildcards all the bits in field 'mf', false if 'wc'
175 * specifies at least one bit in the field.
176 *
177 * The caller is responsible for ensuring that 'wc' corresponds to a flow that
178 * meets 'mf''s prerequisites. */
179 bool
180 mf_is_all_wild(const struct mf_field *mf, const struct flow_wildcards *wc)
181 {
182 switch (mf->id) {
183 case MFF_DP_HASH:
184 return !wc->masks.dp_hash;
185 case MFF_RECIRC_ID:
186 return !wc->masks.recirc_id;
187 case MFF_CONJ_ID:
188 return !wc->masks.conj_id;
189 case MFF_TUN_SRC:
190 return !wc->masks.tunnel.ip_src;
191 case MFF_TUN_DST:
192 return !wc->masks.tunnel.ip_dst;
193 case MFF_TUN_ID:
194 return !wc->masks.tunnel.tun_id;
195 case MFF_TUN_TOS:
196 return !wc->masks.tunnel.ip_tos;
197 case MFF_TUN_TTL:
198 return !wc->masks.tunnel.ip_ttl;
199 case MFF_TUN_FLAGS:
200 return !(wc->masks.tunnel.flags & FLOW_TNL_PUB_F_MASK);
201 case MFF_TUN_GBP_ID:
202 return !wc->masks.tunnel.gbp_id;
203 case MFF_TUN_GBP_FLAGS:
204 return !wc->masks.tunnel.gbp_flags;
205 CASE_MFF_TUN_METADATA:
206 return !ULLONG_GET(wc->masks.tunnel.metadata.present.map,
207 mf->id - MFF_TUN_METADATA0);
208 case MFF_METADATA:
209 return !wc->masks.metadata;
210 case MFF_IN_PORT:
211 case MFF_IN_PORT_OXM:
212 return !wc->masks.in_port.ofp_port;
213 case MFF_SKB_PRIORITY:
214 return !wc->masks.skb_priority;
215 case MFF_PKT_MARK:
216 return !wc->masks.pkt_mark;
217 case MFF_CT_STATE:
218 return !wc->masks.ct_state;
219 case MFF_CT_ZONE:
220 return !wc->masks.ct_zone;
221 case MFF_CT_MARK:
222 return !wc->masks.ct_mark;
223 case MFF_CT_LABEL:
224 return ovs_u128_is_zero(&wc->masks.ct_label);
225 CASE_MFF_REGS:
226 return !wc->masks.regs[mf->id - MFF_REG0];
227 CASE_MFF_XREGS:
228 return !flow_get_xreg(&wc->masks, mf->id - MFF_XREG0);
229 case MFF_ACTSET_OUTPUT:
230 return !wc->masks.actset_output;
231
232 case MFF_ETH_SRC:
233 return eth_addr_is_zero(wc->masks.dl_src);
234 case MFF_ETH_DST:
235 return eth_addr_is_zero(wc->masks.dl_dst);
236 case MFF_ETH_TYPE:
237 return !wc->masks.dl_type;
238
239 case MFF_ARP_SHA:
240 case MFF_ND_SLL:
241 return eth_addr_is_zero(wc->masks.arp_sha);
242
243 case MFF_ARP_THA:
244 case MFF_ND_TLL:
245 return eth_addr_is_zero(wc->masks.arp_tha);
246
247 case MFF_VLAN_TCI:
248 return !wc->masks.vlan_tci;
249 case MFF_DL_VLAN:
250 return !(wc->masks.vlan_tci & htons(VLAN_VID_MASK));
251 case MFF_VLAN_VID:
252 return !(wc->masks.vlan_tci & htons(VLAN_VID_MASK | VLAN_CFI));
253 case MFF_DL_VLAN_PCP:
254 case MFF_VLAN_PCP:
255 return !(wc->masks.vlan_tci & htons(VLAN_PCP_MASK));
256
257 case MFF_MPLS_LABEL:
258 return !(wc->masks.mpls_lse[0] & htonl(MPLS_LABEL_MASK));
259 case MFF_MPLS_TC:
260 return !(wc->masks.mpls_lse[0] & htonl(MPLS_TC_MASK));
261 case MFF_MPLS_BOS:
262 return !(wc->masks.mpls_lse[0] & htonl(MPLS_BOS_MASK));
263
264 case MFF_IPV4_SRC:
265 return !wc->masks.nw_src;
266 case MFF_IPV4_DST:
267 return !wc->masks.nw_dst;
268
269 case MFF_IPV6_SRC:
270 return ipv6_mask_is_any(&wc->masks.ipv6_src);
271 case MFF_IPV6_DST:
272 return ipv6_mask_is_any(&wc->masks.ipv6_dst);
273
274 case MFF_IPV6_LABEL:
275 return !wc->masks.ipv6_label;
276
277 case MFF_IP_PROTO:
278 return !wc->masks.nw_proto;
279 case MFF_IP_DSCP:
280 case MFF_IP_DSCP_SHIFTED:
281 return !(wc->masks.nw_tos & IP_DSCP_MASK);
282 case MFF_IP_ECN:
283 return !(wc->masks.nw_tos & IP_ECN_MASK);
284 case MFF_IP_TTL:
285 return !wc->masks.nw_ttl;
286
287 case MFF_ND_TARGET:
288 return ipv6_mask_is_any(&wc->masks.nd_target);
289
290 case MFF_IP_FRAG:
291 return !(wc->masks.nw_frag & FLOW_NW_FRAG_MASK);
292
293 case MFF_ARP_OP:
294 return !wc->masks.nw_proto;
295 case MFF_ARP_SPA:
296 return !wc->masks.nw_src;
297 case MFF_ARP_TPA:
298 return !wc->masks.nw_dst;
299
300 case MFF_TCP_SRC:
301 case MFF_UDP_SRC:
302 case MFF_SCTP_SRC:
303 case MFF_ICMPV4_TYPE:
304 case MFF_ICMPV6_TYPE:
305 return !wc->masks.tp_src;
306 case MFF_TCP_DST:
307 case MFF_UDP_DST:
308 case MFF_SCTP_DST:
309 case MFF_ICMPV4_CODE:
310 case MFF_ICMPV6_CODE:
311 return !wc->masks.tp_dst;
312 case MFF_TCP_FLAGS:
313 return !wc->masks.tcp_flags;
314
315 case MFF_N_IDS:
316 default:
317 OVS_NOT_REACHED();
318 }
319 }
320
321 /* Initializes 'mask' with the wildcard bit pattern for field 'mf' within 'wc'.
322 * Each bit in 'mask' will be set to 1 if the bit is significant for matching
323 * purposes, or to 0 if it is wildcarded.
324 *
325 * The caller is responsible for ensuring that 'wc' corresponds to a flow that
326 * meets 'mf''s prerequisites. */
327 void
328 mf_get_mask(const struct mf_field *mf, const struct flow_wildcards *wc,
329 union mf_value *mask)
330 {
331 mf_get_value(mf, &wc->masks, mask);
332 }
333
334 /* Tests whether 'mask' is a valid wildcard bit pattern for 'mf'. Returns true
335 * if the mask is valid, false otherwise. */
336 bool
337 mf_is_mask_valid(const struct mf_field *mf, const union mf_value *mask)
338 {
339 switch (mf->maskable) {
340 case MFM_NONE:
341 return (is_all_zeros(mask, mf->n_bytes) ||
342 is_all_ones(mask, mf->n_bytes));
343
344 case MFM_FULLY:
345 return true;
346 }
347
348 OVS_NOT_REACHED();
349 }
350
351 /* Returns true if 'flow' meets the prerequisites for 'mf', false otherwise. */
352 bool
353 mf_are_prereqs_ok(const struct mf_field *mf, const struct flow *flow)
354 {
355 switch (mf->prereqs) {
356 case MFP_NONE:
357 return true;
358
359 case MFP_ARP:
360 return (flow->dl_type == htons(ETH_TYPE_ARP) ||
361 flow->dl_type == htons(ETH_TYPE_RARP));
362 case MFP_IPV4:
363 return flow->dl_type == htons(ETH_TYPE_IP);
364 case MFP_IPV6:
365 return flow->dl_type == htons(ETH_TYPE_IPV6);
366 case MFP_VLAN_VID:
367 return (flow->vlan_tci & htons(VLAN_CFI)) != 0;
368 case MFP_MPLS:
369 return eth_type_mpls(flow->dl_type);
370 case MFP_IP_ANY:
371 return is_ip_any(flow);
372
373 case MFP_TCP:
374 return is_ip_any(flow) && flow->nw_proto == IPPROTO_TCP
375 && !(flow->nw_frag & FLOW_NW_FRAG_LATER);
376 case MFP_UDP:
377 return is_ip_any(flow) && flow->nw_proto == IPPROTO_UDP
378 && !(flow->nw_frag & FLOW_NW_FRAG_LATER);
379 case MFP_SCTP:
380 return is_ip_any(flow) && flow->nw_proto == IPPROTO_SCTP
381 && !(flow->nw_frag & FLOW_NW_FRAG_LATER);
382 case MFP_ICMPV4:
383 return is_icmpv4(flow);
384 case MFP_ICMPV6:
385 return is_icmpv6(flow);
386
387 case MFP_ND:
388 return (is_icmpv6(flow)
389 && flow->tp_dst == htons(0)
390 && (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT) ||
391 flow->tp_src == htons(ND_NEIGHBOR_ADVERT)));
392 case MFP_ND_SOLICIT:
393 return (is_icmpv6(flow)
394 && flow->tp_dst == htons(0)
395 && (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT)));
396 case MFP_ND_ADVERT:
397 return (is_icmpv6(flow)
398 && flow->tp_dst == htons(0)
399 && (flow->tp_src == htons(ND_NEIGHBOR_ADVERT)));
400 }
401
402 OVS_NOT_REACHED();
403 }
404
405 /* Set field and it's prerequisities in the mask.
406 * This is only ever called for writeable 'mf's, but we do not make the
407 * distinction here. */
408 void
409 mf_mask_field_and_prereqs(const struct mf_field *mf, struct flow_wildcards *wc)
410 {
411 mf_set_flow_value(mf, &exact_match_mask, &wc->masks);
412
413 switch (mf->prereqs) {
414 case MFP_ND:
415 case MFP_ND_SOLICIT:
416 case MFP_ND_ADVERT:
417 WC_MASK_FIELD(wc, tp_src);
418 WC_MASK_FIELD(wc, tp_dst);
419 /* Fall through. */
420 case MFP_TCP:
421 case MFP_UDP:
422 case MFP_SCTP:
423 case MFP_ICMPV4:
424 case MFP_ICMPV6:
425 /* nw_frag always unwildcarded. */
426 WC_MASK_FIELD(wc, nw_proto);
427 /* Fall through. */
428 case MFP_ARP:
429 case MFP_IPV4:
430 case MFP_IPV6:
431 case MFP_MPLS:
432 case MFP_IP_ANY:
433 /* dl_type always unwildcarded. */
434 break;
435 case MFP_VLAN_VID:
436 WC_MASK_FIELD_MASK(wc, vlan_tci, htons(VLAN_CFI));
437 break;
438 case MFP_NONE:
439 break;
440 }
441 }
442
443 /* Set bits of 'bm' corresponding to the field 'mf' and it's prerequisities. */
444 void
445 mf_bitmap_set_field_and_prereqs(const struct mf_field *mf, struct mf_bitmap *bm)
446 {
447 bitmap_set1(bm->bm, mf->id);
448
449 switch (mf->prereqs) {
450 case MFP_ND:
451 case MFP_ND_SOLICIT:
452 case MFP_ND_ADVERT:
453 bitmap_set1(bm->bm, MFF_TCP_SRC);
454 bitmap_set1(bm->bm, MFF_TCP_DST);
455 /* Fall through. */
456 case MFP_TCP:
457 case MFP_UDP:
458 case MFP_SCTP:
459 case MFP_ICMPV4:
460 case MFP_ICMPV6:
461 /* nw_frag always unwildcarded. */
462 bitmap_set1(bm->bm, MFF_IP_PROTO);
463 /* Fall through. */
464 case MFP_ARP:
465 case MFP_IPV4:
466 case MFP_IPV6:
467 case MFP_MPLS:
468 case MFP_IP_ANY:
469 bitmap_set1(bm->bm, MFF_ETH_TYPE);
470 break;
471 case MFP_VLAN_VID:
472 bitmap_set1(bm->bm, MFF_VLAN_TCI);
473 break;
474 case MFP_NONE:
475 break;
476 }
477 }
478
479 /* Returns true if 'value' may be a valid value *as part of a masked match*,
480 * false otherwise.
481 *
482 * A value is not rejected just because it is not valid for the field in
483 * question, but only if it doesn't make sense to test the bits in question at
484 * all. For example, the MFF_VLAN_TCI field will never have a nonzero value
485 * without the VLAN_CFI bit being set, but we can't reject those values because
486 * it is still legitimate to test just for those bits (see the documentation
487 * for NXM_OF_VLAN_TCI in nicira-ext.h). On the other hand, there is never a
488 * reason to set the low bit of MFF_IP_DSCP to 1, so we reject that. */
489 bool
490 mf_is_value_valid(const struct mf_field *mf, const union mf_value *value)
491 {
492 switch (mf->id) {
493 case MFF_DP_HASH:
494 case MFF_RECIRC_ID:
495 case MFF_CONJ_ID:
496 case MFF_TUN_ID:
497 case MFF_TUN_SRC:
498 case MFF_TUN_DST:
499 case MFF_TUN_TOS:
500 case MFF_TUN_TTL:
501 case MFF_TUN_GBP_ID:
502 case MFF_TUN_GBP_FLAGS:
503 CASE_MFF_TUN_METADATA:
504 case MFF_METADATA:
505 case MFF_IN_PORT:
506 case MFF_SKB_PRIORITY:
507 case MFF_PKT_MARK:
508 case MFF_CT_ZONE:
509 case MFF_CT_MARK:
510 case MFF_CT_LABEL:
511 CASE_MFF_REGS:
512 CASE_MFF_XREGS:
513 case MFF_ETH_SRC:
514 case MFF_ETH_DST:
515 case MFF_ETH_TYPE:
516 case MFF_VLAN_TCI:
517 case MFF_IPV4_SRC:
518 case MFF_IPV4_DST:
519 case MFF_IPV6_SRC:
520 case MFF_IPV6_DST:
521 case MFF_IP_PROTO:
522 case MFF_IP_TTL:
523 case MFF_ARP_SPA:
524 case MFF_ARP_TPA:
525 case MFF_ARP_SHA:
526 case MFF_ARP_THA:
527 case MFF_TCP_SRC:
528 case MFF_TCP_DST:
529 case MFF_UDP_SRC:
530 case MFF_UDP_DST:
531 case MFF_SCTP_SRC:
532 case MFF_SCTP_DST:
533 case MFF_ICMPV4_TYPE:
534 case MFF_ICMPV4_CODE:
535 case MFF_ICMPV6_TYPE:
536 case MFF_ICMPV6_CODE:
537 case MFF_ND_TARGET:
538 case MFF_ND_SLL:
539 case MFF_ND_TLL:
540 return true;
541
542 case MFF_IN_PORT_OXM:
543 case MFF_ACTSET_OUTPUT: {
544 ofp_port_t port;
545 return !ofputil_port_from_ofp11(value->be32, &port);
546 }
547
548 case MFF_IP_DSCP:
549 return !(value->u8 & ~IP_DSCP_MASK);
550 case MFF_IP_DSCP_SHIFTED:
551 return !(value->u8 & (~IP_DSCP_MASK >> 2));
552 case MFF_IP_ECN:
553 return !(value->u8 & ~IP_ECN_MASK);
554 case MFF_IP_FRAG:
555 return !(value->u8 & ~FLOW_NW_FRAG_MASK);
556 case MFF_TCP_FLAGS:
557 return !(value->be16 & ~htons(0x0fff));
558
559 case MFF_ARP_OP:
560 return !(value->be16 & htons(0xff00));
561
562 case MFF_DL_VLAN:
563 return !(value->be16 & htons(VLAN_CFI | VLAN_PCP_MASK));
564 case MFF_VLAN_VID:
565 return !(value->be16 & htons(VLAN_PCP_MASK));
566
567 case MFF_DL_VLAN_PCP:
568 case MFF_VLAN_PCP:
569 return !(value->u8 & ~(VLAN_PCP_MASK >> VLAN_PCP_SHIFT));
570
571 case MFF_IPV6_LABEL:
572 return !(value->be32 & ~htonl(IPV6_LABEL_MASK));
573
574 case MFF_MPLS_LABEL:
575 return !(value->be32 & ~htonl(MPLS_LABEL_MASK >> MPLS_LABEL_SHIFT));
576
577 case MFF_MPLS_TC:
578 return !(value->u8 & ~(MPLS_TC_MASK >> MPLS_TC_SHIFT));
579
580 case MFF_MPLS_BOS:
581 return !(value->u8 & ~(MPLS_BOS_MASK >> MPLS_BOS_SHIFT));
582
583 case MFF_TUN_FLAGS:
584 return !(value->be16 & ~htons(FLOW_TNL_PUB_F_MASK));
585
586 case MFF_CT_STATE:
587 return !(value->be32 & ~htonl(CS_SUPPORTED_MASK));
588
589 case MFF_N_IDS:
590 default:
591 OVS_NOT_REACHED();
592 }
593 }
594
595 /* Copies the value of field 'mf' from 'flow' into 'value'. The caller is
596 * responsible for ensuring that 'flow' meets 'mf''s prerequisites. */
597 void
598 mf_get_value(const struct mf_field *mf, const struct flow *flow,
599 union mf_value *value)
600 {
601 switch (mf->id) {
602 case MFF_DP_HASH:
603 value->be32 = htonl(flow->dp_hash);
604 break;
605 case MFF_RECIRC_ID:
606 value->be32 = htonl(flow->recirc_id);
607 break;
608 case MFF_CONJ_ID:
609 value->be32 = htonl(flow->conj_id);
610 break;
611 case MFF_TUN_ID:
612 value->be64 = flow->tunnel.tun_id;
613 break;
614 case MFF_TUN_SRC:
615 value->be32 = flow->tunnel.ip_src;
616 break;
617 case MFF_TUN_DST:
618 value->be32 = flow->tunnel.ip_dst;
619 break;
620 case MFF_TUN_FLAGS:
621 value->be16 = htons(flow->tunnel.flags & FLOW_TNL_PUB_F_MASK);
622 break;
623 case MFF_TUN_GBP_ID:
624 value->be16 = flow->tunnel.gbp_id;
625 break;
626 case MFF_TUN_GBP_FLAGS:
627 value->u8 = flow->tunnel.gbp_flags;
628 break;
629 case MFF_TUN_TTL:
630 value->u8 = flow->tunnel.ip_ttl;
631 break;
632 case MFF_TUN_TOS:
633 value->u8 = flow->tunnel.ip_tos;
634 break;
635 CASE_MFF_TUN_METADATA:
636 tun_metadata_read(&flow->tunnel, mf, value);
637 break;
638
639 case MFF_METADATA:
640 value->be64 = flow->metadata;
641 break;
642
643 case MFF_IN_PORT:
644 value->be16 = htons(ofp_to_u16(flow->in_port.ofp_port));
645 break;
646 case MFF_IN_PORT_OXM:
647 value->be32 = ofputil_port_to_ofp11(flow->in_port.ofp_port);
648 break;
649 case MFF_ACTSET_OUTPUT:
650 value->be32 = ofputil_port_to_ofp11(flow->actset_output);
651 break;
652
653 case MFF_SKB_PRIORITY:
654 value->be32 = htonl(flow->skb_priority);
655 break;
656
657 case MFF_PKT_MARK:
658 value->be32 = htonl(flow->pkt_mark);
659 break;
660
661 case MFF_CT_STATE:
662 value->be32 = htonl(flow->ct_state);
663 break;
664
665 case MFF_CT_ZONE:
666 value->be16 = htons(flow->ct_zone);
667 break;
668
669 case MFF_CT_MARK:
670 value->be32 = htonl(flow->ct_mark);
671 break;
672
673 case MFF_CT_LABEL:
674 hton128(&flow->ct_label, &value->be128);
675 break;
676
677 CASE_MFF_REGS:
678 value->be32 = htonl(flow->regs[mf->id - MFF_REG0]);
679 break;
680
681 CASE_MFF_XREGS:
682 value->be64 = htonll(flow_get_xreg(flow, mf->id - MFF_XREG0));
683 break;
684
685 case MFF_ETH_SRC:
686 value->mac = flow->dl_src;
687 break;
688
689 case MFF_ETH_DST:
690 value->mac = flow->dl_dst;
691 break;
692
693 case MFF_ETH_TYPE:
694 value->be16 = flow->dl_type;
695 break;
696
697 case MFF_VLAN_TCI:
698 value->be16 = flow->vlan_tci;
699 break;
700
701 case MFF_DL_VLAN:
702 value->be16 = flow->vlan_tci & htons(VLAN_VID_MASK);
703 break;
704 case MFF_VLAN_VID:
705 value->be16 = flow->vlan_tci & htons(VLAN_VID_MASK | VLAN_CFI);
706 break;
707
708 case MFF_DL_VLAN_PCP:
709 case MFF_VLAN_PCP:
710 value->u8 = vlan_tci_to_pcp(flow->vlan_tci);
711 break;
712
713 case MFF_MPLS_LABEL:
714 value->be32 = htonl(mpls_lse_to_label(flow->mpls_lse[0]));
715 break;
716
717 case MFF_MPLS_TC:
718 value->u8 = mpls_lse_to_tc(flow->mpls_lse[0]);
719 break;
720
721 case MFF_MPLS_BOS:
722 value->u8 = mpls_lse_to_bos(flow->mpls_lse[0]);
723 break;
724
725 case MFF_IPV4_SRC:
726 value->be32 = flow->nw_src;
727 break;
728
729 case MFF_IPV4_DST:
730 value->be32 = flow->nw_dst;
731 break;
732
733 case MFF_IPV6_SRC:
734 value->ipv6 = flow->ipv6_src;
735 break;
736
737 case MFF_IPV6_DST:
738 value->ipv6 = flow->ipv6_dst;
739 break;
740
741 case MFF_IPV6_LABEL:
742 value->be32 = flow->ipv6_label;
743 break;
744
745 case MFF_IP_PROTO:
746 value->u8 = flow->nw_proto;
747 break;
748
749 case MFF_IP_DSCP:
750 value->u8 = flow->nw_tos & IP_DSCP_MASK;
751 break;
752
753 case MFF_IP_DSCP_SHIFTED:
754 value->u8 = flow->nw_tos >> 2;
755 break;
756
757 case MFF_IP_ECN:
758 value->u8 = flow->nw_tos & IP_ECN_MASK;
759 break;
760
761 case MFF_IP_TTL:
762 value->u8 = flow->nw_ttl;
763 break;
764
765 case MFF_IP_FRAG:
766 value->u8 = flow->nw_frag;
767 break;
768
769 case MFF_ARP_OP:
770 value->be16 = htons(flow->nw_proto);
771 break;
772
773 case MFF_ARP_SPA:
774 value->be32 = flow->nw_src;
775 break;
776
777 case MFF_ARP_TPA:
778 value->be32 = flow->nw_dst;
779 break;
780
781 case MFF_ARP_SHA:
782 case MFF_ND_SLL:
783 value->mac = flow->arp_sha;
784 break;
785
786 case MFF_ARP_THA:
787 case MFF_ND_TLL:
788 value->mac = flow->arp_tha;
789 break;
790
791 case MFF_TCP_SRC:
792 case MFF_UDP_SRC:
793 case MFF_SCTP_SRC:
794 value->be16 = flow->tp_src;
795 break;
796
797 case MFF_TCP_DST:
798 case MFF_UDP_DST:
799 case MFF_SCTP_DST:
800 value->be16 = flow->tp_dst;
801 break;
802
803 case MFF_TCP_FLAGS:
804 value->be16 = flow->tcp_flags;
805 break;
806
807 case MFF_ICMPV4_TYPE:
808 case MFF_ICMPV6_TYPE:
809 value->u8 = ntohs(flow->tp_src);
810 break;
811
812 case MFF_ICMPV4_CODE:
813 case MFF_ICMPV6_CODE:
814 value->u8 = ntohs(flow->tp_dst);
815 break;
816
817 case MFF_ND_TARGET:
818 value->ipv6 = flow->nd_target;
819 break;
820
821 case MFF_N_IDS:
822 default:
823 OVS_NOT_REACHED();
824 }
825 }
826
827 /* Makes 'match' match field 'mf' exactly, with the value matched taken from
828 * 'value'. The caller is responsible for ensuring that 'match' meets 'mf''s
829 * prerequisites.
830 *
831 * If non-NULL, 'err_str' returns a malloc'ed string describing any errors
832 * with the request or NULL if there is no error. The caller is reponsible
833 * for freeing the string. */
834 void
835 mf_set_value(const struct mf_field *mf,
836 const union mf_value *value, struct match *match, char **err_str)
837 {
838 if (err_str) {
839 *err_str = NULL;
840 }
841
842 switch (mf->id) {
843 case MFF_DP_HASH:
844 match_set_dp_hash(match, ntohl(value->be32));
845 break;
846 case MFF_RECIRC_ID:
847 match_set_recirc_id(match, ntohl(value->be32));
848 break;
849 case MFF_CONJ_ID:
850 match_set_conj_id(match, ntohl(value->be32));
851 break;
852 case MFF_TUN_ID:
853 match_set_tun_id(match, value->be64);
854 break;
855 case MFF_TUN_SRC:
856 match_set_tun_src(match, value->be32);
857 break;
858 case MFF_TUN_DST:
859 match_set_tun_dst(match, value->be32);
860 break;
861 case MFF_TUN_FLAGS:
862 match_set_tun_flags(match, ntohs(value->be16));
863 break;
864 case MFF_TUN_GBP_ID:
865 match_set_tun_gbp_id(match, value->be16);
866 break;
867 case MFF_TUN_GBP_FLAGS:
868 match_set_tun_gbp_flags(match, value->u8);
869 break;
870 case MFF_TUN_TOS:
871 match_set_tun_tos(match, value->u8);
872 break;
873 case MFF_TUN_TTL:
874 match_set_tun_ttl(match, value->u8);
875 break;
876 CASE_MFF_TUN_METADATA:
877 tun_metadata_set_match(mf, value, NULL, match, err_str);
878 break;
879
880 case MFF_METADATA:
881 match_set_metadata(match, value->be64);
882 break;
883
884 case MFF_IN_PORT:
885 match_set_in_port(match, u16_to_ofp(ntohs(value->be16)));
886 break;
887
888 case MFF_IN_PORT_OXM: {
889 ofp_port_t port;
890 ofputil_port_from_ofp11(value->be32, &port);
891 match_set_in_port(match, port);
892 break;
893 }
894 case MFF_ACTSET_OUTPUT: {
895 ofp_port_t port;
896 ofputil_port_from_ofp11(value->be32, &port);
897 match_set_actset_output(match, port);
898 break;
899 }
900
901 case MFF_SKB_PRIORITY:
902 match_set_skb_priority(match, ntohl(value->be32));
903 break;
904
905 case MFF_PKT_MARK:
906 match_set_pkt_mark(match, ntohl(value->be32));
907 break;
908
909 case MFF_CT_STATE:
910 match_set_ct_state(match, ntohl(value->be32));
911 break;
912
913 case MFF_CT_ZONE:
914 match_set_ct_zone(match, ntohs(value->be16));
915 break;
916
917 case MFF_CT_MARK:
918 match_set_ct_mark(match, ntohl(value->be32));
919 break;
920
921 case MFF_CT_LABEL: {
922 ovs_u128 label;
923
924 ntoh128(&value->be128, &label);
925 match_set_ct_label(match, label);
926 break;
927 }
928
929 CASE_MFF_REGS:
930 match_set_reg(match, mf->id - MFF_REG0, ntohl(value->be32));
931 break;
932
933 CASE_MFF_XREGS:
934 match_set_xreg(match, mf->id - MFF_XREG0, ntohll(value->be64));
935 break;
936
937 case MFF_ETH_SRC:
938 match_set_dl_src(match, value->mac);
939 break;
940
941 case MFF_ETH_DST:
942 match_set_dl_dst(match, value->mac);
943 break;
944
945 case MFF_ETH_TYPE:
946 match_set_dl_type(match, value->be16);
947 break;
948
949 case MFF_VLAN_TCI:
950 match_set_dl_tci(match, value->be16);
951 break;
952
953 case MFF_DL_VLAN:
954 match_set_dl_vlan(match, value->be16);
955 break;
956 case MFF_VLAN_VID:
957 match_set_vlan_vid(match, value->be16);
958 break;
959
960 case MFF_DL_VLAN_PCP:
961 case MFF_VLAN_PCP:
962 match_set_dl_vlan_pcp(match, value->u8);
963 break;
964
965 case MFF_MPLS_LABEL:
966 match_set_mpls_label(match, 0, value->be32);
967 break;
968
969 case MFF_MPLS_TC:
970 match_set_mpls_tc(match, 0, value->u8);
971 break;
972
973 case MFF_MPLS_BOS:
974 match_set_mpls_bos(match, 0, value->u8);
975 break;
976
977 case MFF_IPV4_SRC:
978 match_set_nw_src(match, value->be32);
979 break;
980
981 case MFF_IPV4_DST:
982 match_set_nw_dst(match, value->be32);
983 break;
984
985 case MFF_IPV6_SRC:
986 match_set_ipv6_src(match, &value->ipv6);
987 break;
988
989 case MFF_IPV6_DST:
990 match_set_ipv6_dst(match, &value->ipv6);
991 break;
992
993 case MFF_IPV6_LABEL:
994 match_set_ipv6_label(match, value->be32);
995 break;
996
997 case MFF_IP_PROTO:
998 match_set_nw_proto(match, value->u8);
999 break;
1000
1001 case MFF_IP_DSCP:
1002 match_set_nw_dscp(match, value->u8);
1003 break;
1004
1005 case MFF_IP_DSCP_SHIFTED:
1006 match_set_nw_dscp(match, value->u8 << 2);
1007 break;
1008
1009 case MFF_IP_ECN:
1010 match_set_nw_ecn(match, value->u8);
1011 break;
1012
1013 case MFF_IP_TTL:
1014 match_set_nw_ttl(match, value->u8);
1015 break;
1016
1017 case MFF_IP_FRAG:
1018 match_set_nw_frag(match, value->u8);
1019 break;
1020
1021 case MFF_ARP_OP:
1022 match_set_nw_proto(match, ntohs(value->be16));
1023 break;
1024
1025 case MFF_ARP_SPA:
1026 match_set_nw_src(match, value->be32);
1027 break;
1028
1029 case MFF_ARP_TPA:
1030 match_set_nw_dst(match, value->be32);
1031 break;
1032
1033 case MFF_ARP_SHA:
1034 case MFF_ND_SLL:
1035 match_set_arp_sha(match, value->mac);
1036 break;
1037
1038 case MFF_ARP_THA:
1039 case MFF_ND_TLL:
1040 match_set_arp_tha(match, value->mac);
1041 break;
1042
1043 case MFF_TCP_SRC:
1044 case MFF_UDP_SRC:
1045 case MFF_SCTP_SRC:
1046 match_set_tp_src(match, value->be16);
1047 break;
1048
1049 case MFF_TCP_DST:
1050 case MFF_UDP_DST:
1051 case MFF_SCTP_DST:
1052 match_set_tp_dst(match, value->be16);
1053 break;
1054
1055 case MFF_TCP_FLAGS:
1056 match_set_tcp_flags(match, value->be16);
1057 break;
1058
1059 case MFF_ICMPV4_TYPE:
1060 case MFF_ICMPV6_TYPE:
1061 match_set_icmp_type(match, value->u8);
1062 break;
1063
1064 case MFF_ICMPV4_CODE:
1065 case MFF_ICMPV6_CODE:
1066 match_set_icmp_code(match, value->u8);
1067 break;
1068
1069 case MFF_ND_TARGET:
1070 match_set_nd_target(match, &value->ipv6);
1071 break;
1072
1073 case MFF_N_IDS:
1074 default:
1075 OVS_NOT_REACHED();
1076 }
1077 }
1078
1079 /* Unwildcard 'mask' member field described by 'mf'. The caller is
1080 * responsible for ensuring that 'mask' meets 'mf''s prerequisites. */
1081 void
1082 mf_mask_field(const struct mf_field *mf, struct flow *mask)
1083 {
1084 /* For MFF_DL_VLAN, we cannot send a all 1's to flow_set_dl_vlan()
1085 * as that will be considered as OFP10_VLAN_NONE. So consider it as a
1086 * special case. For the rest, calling mf_set_flow_value() is good
1087 * enough. */
1088 if (mf->id == MFF_DL_VLAN) {
1089 flow_set_dl_vlan(mask, htons(VLAN_VID_MASK));
1090 } else {
1091 mf_set_flow_value(mf, &exact_match_mask, mask);
1092 }
1093 }
1094
1095 static int
1096 field_len(const struct mf_field *mf, const union mf_value *value_)
1097 {
1098 const uint8_t *value = &value_->u8;
1099 int i;
1100
1101 if (!mf->variable_len) {
1102 return mf->n_bytes;
1103 }
1104
1105 if (!value) {
1106 return 0;
1107 }
1108
1109 for (i = 0; i < mf->n_bytes; i++) {
1110 if (value[i] != 0) {
1111 break;
1112 }
1113 }
1114
1115 return mf->n_bytes - i;
1116 }
1117
1118 /* Returns the effective length of the field. For fixed length fields,
1119 * this is just the defined length. For variable length fields, it is
1120 * the minimum size encoding that retains the same meaning (i.e.
1121 * discarding leading zeros).
1122 *
1123 * 'is_masked' returns (if non-NULL) whether the original contained
1124 * a mask. Otherwise, a mask that is the same length as the value
1125 * might be misinterpreted as an exact match. */
1126 int
1127 mf_field_len(const struct mf_field *mf, const union mf_value *value,
1128 const union mf_value *mask, bool *is_masked_)
1129 {
1130 int len, mask_len;
1131 bool is_masked = mask && !is_all_ones(mask, mf->n_bytes);
1132
1133 len = field_len(mf, value);
1134 if (is_masked) {
1135 mask_len = field_len(mf, mask);
1136 len = MAX(len, mask_len);
1137 }
1138
1139 if (is_masked_) {
1140 *is_masked_ = is_masked;
1141 }
1142
1143 return len;
1144 }
1145
1146 /* Sets 'flow' member field described by 'mf' to 'value'. The caller is
1147 * responsible for ensuring that 'flow' meets 'mf''s prerequisites.*/
1148 void
1149 mf_set_flow_value(const struct mf_field *mf,
1150 const union mf_value *value, struct flow *flow)
1151 {
1152 switch (mf->id) {
1153 case MFF_DP_HASH:
1154 flow->dp_hash = ntohl(value->be32);
1155 break;
1156 case MFF_RECIRC_ID:
1157 flow->recirc_id = ntohl(value->be32);
1158 break;
1159 case MFF_CONJ_ID:
1160 flow->conj_id = ntohl(value->be32);
1161 break;
1162 case MFF_TUN_ID:
1163 flow->tunnel.tun_id = value->be64;
1164 break;
1165 case MFF_TUN_SRC:
1166 flow->tunnel.ip_src = value->be32;
1167 break;
1168 case MFF_TUN_DST:
1169 flow->tunnel.ip_dst = value->be32;
1170 break;
1171 case MFF_TUN_FLAGS:
1172 flow->tunnel.flags = (flow->tunnel.flags & ~FLOW_TNL_PUB_F_MASK) |
1173 ntohs(value->be16);
1174 break;
1175 case MFF_TUN_GBP_ID:
1176 flow->tunnel.gbp_id = value->be16;
1177 break;
1178 case MFF_TUN_GBP_FLAGS:
1179 flow->tunnel.gbp_flags = value->u8;
1180 break;
1181 case MFF_TUN_TOS:
1182 flow->tunnel.ip_tos = value->u8;
1183 break;
1184 case MFF_TUN_TTL:
1185 flow->tunnel.ip_ttl = value->u8;
1186 break;
1187 CASE_MFF_TUN_METADATA:
1188 tun_metadata_write(&flow->tunnel, mf, value);
1189 break;
1190 case MFF_METADATA:
1191 flow->metadata = value->be64;
1192 break;
1193
1194 case MFF_IN_PORT:
1195 flow->in_port.ofp_port = u16_to_ofp(ntohs(value->be16));
1196 break;
1197
1198 case MFF_IN_PORT_OXM:
1199 ofputil_port_from_ofp11(value->be32, &flow->in_port.ofp_port);
1200 break;
1201 case MFF_ACTSET_OUTPUT:
1202 ofputil_port_from_ofp11(value->be32, &flow->actset_output);
1203 break;
1204
1205 case MFF_SKB_PRIORITY:
1206 flow->skb_priority = ntohl(value->be32);
1207 break;
1208
1209 case MFF_PKT_MARK:
1210 flow->pkt_mark = ntohl(value->be32);
1211 break;
1212
1213 case MFF_CT_STATE:
1214 flow->ct_state = ntohl(value->be32);
1215 break;
1216
1217 case MFF_CT_ZONE:
1218 flow->ct_zone = ntohs(value->be16);
1219 break;
1220
1221 case MFF_CT_MARK:
1222 flow->ct_mark = ntohl(value->be32);
1223 break;
1224
1225 case MFF_CT_LABEL:
1226 ntoh128(&value->be128, &flow->ct_label);
1227 break;
1228
1229 CASE_MFF_REGS:
1230 flow->regs[mf->id - MFF_REG0] = ntohl(value->be32);
1231 break;
1232
1233 CASE_MFF_XREGS:
1234 flow_set_xreg(flow, mf->id - MFF_XREG0, ntohll(value->be64));
1235 break;
1236
1237 case MFF_ETH_SRC:
1238 flow->dl_src = value->mac;
1239 break;
1240
1241 case MFF_ETH_DST:
1242 flow->dl_dst = value->mac;
1243 break;
1244
1245 case MFF_ETH_TYPE:
1246 flow->dl_type = value->be16;
1247 break;
1248
1249 case MFF_VLAN_TCI:
1250 flow->vlan_tci = value->be16;
1251 break;
1252
1253 case MFF_DL_VLAN:
1254 flow_set_dl_vlan(flow, value->be16);
1255 break;
1256 case MFF_VLAN_VID:
1257 flow_set_vlan_vid(flow, value->be16);
1258 break;
1259
1260 case MFF_DL_VLAN_PCP:
1261 case MFF_VLAN_PCP:
1262 flow_set_vlan_pcp(flow, value->u8);
1263 break;
1264
1265 case MFF_MPLS_LABEL:
1266 flow_set_mpls_label(flow, 0, value->be32);
1267 break;
1268
1269 case MFF_MPLS_TC:
1270 flow_set_mpls_tc(flow, 0, value->u8);
1271 break;
1272
1273 case MFF_MPLS_BOS:
1274 flow_set_mpls_bos(flow, 0, value->u8);
1275 break;
1276
1277 case MFF_IPV4_SRC:
1278 flow->nw_src = value->be32;
1279 break;
1280
1281 case MFF_IPV4_DST:
1282 flow->nw_dst = value->be32;
1283 break;
1284
1285 case MFF_IPV6_SRC:
1286 flow->ipv6_src = value->ipv6;
1287 break;
1288
1289 case MFF_IPV6_DST:
1290 flow->ipv6_dst = value->ipv6;
1291 break;
1292
1293 case MFF_IPV6_LABEL:
1294 flow->ipv6_label = value->be32 & htonl(IPV6_LABEL_MASK);
1295 break;
1296
1297 case MFF_IP_PROTO:
1298 flow->nw_proto = value->u8;
1299 break;
1300
1301 case MFF_IP_DSCP:
1302 flow->nw_tos &= ~IP_DSCP_MASK;
1303 flow->nw_tos |= value->u8 & IP_DSCP_MASK;
1304 break;
1305
1306 case MFF_IP_DSCP_SHIFTED:
1307 flow->nw_tos &= ~IP_DSCP_MASK;
1308 flow->nw_tos |= value->u8 << 2;
1309 break;
1310
1311 case MFF_IP_ECN:
1312 flow->nw_tos &= ~IP_ECN_MASK;
1313 flow->nw_tos |= value->u8 & IP_ECN_MASK;
1314 break;
1315
1316 case MFF_IP_TTL:
1317 flow->nw_ttl = value->u8;
1318 break;
1319
1320 case MFF_IP_FRAG:
1321 flow->nw_frag = value->u8 & FLOW_NW_FRAG_MASK;
1322 break;
1323
1324 case MFF_ARP_OP:
1325 flow->nw_proto = ntohs(value->be16);
1326 break;
1327
1328 case MFF_ARP_SPA:
1329 flow->nw_src = value->be32;
1330 break;
1331
1332 case MFF_ARP_TPA:
1333 flow->nw_dst = value->be32;
1334 break;
1335
1336 case MFF_ARP_SHA:
1337 case MFF_ND_SLL:
1338 flow->arp_sha = value->mac;
1339 break;
1340
1341 case MFF_ARP_THA:
1342 case MFF_ND_TLL:
1343 flow->arp_tha = value->mac;
1344 break;
1345
1346 case MFF_TCP_SRC:
1347 case MFF_UDP_SRC:
1348 case MFF_SCTP_SRC:
1349 flow->tp_src = value->be16;
1350 break;
1351
1352 case MFF_TCP_DST:
1353 case MFF_UDP_DST:
1354 case MFF_SCTP_DST:
1355 flow->tp_dst = value->be16;
1356 break;
1357
1358 case MFF_TCP_FLAGS:
1359 flow->tcp_flags = value->be16;
1360 break;
1361
1362 case MFF_ICMPV4_TYPE:
1363 case MFF_ICMPV6_TYPE:
1364 flow->tp_src = htons(value->u8);
1365 break;
1366
1367 case MFF_ICMPV4_CODE:
1368 case MFF_ICMPV6_CODE:
1369 flow->tp_dst = htons(value->u8);
1370 break;
1371
1372 case MFF_ND_TARGET:
1373 flow->nd_target = value->ipv6;
1374 break;
1375
1376 case MFF_N_IDS:
1377 default:
1378 OVS_NOT_REACHED();
1379 }
1380 }
1381
1382 /* Consider each of 'src', 'mask', and 'dst' as if they were arrays of 8*n
1383 * bits. Then, for each 0 <= i < 8 * n such that mask[i] == 1, sets dst[i] =
1384 * src[i]. */
1385 static void
1386 apply_mask(const uint8_t *src, const uint8_t *mask, uint8_t *dst, size_t n)
1387 {
1388 size_t i;
1389
1390 for (i = 0; i < n; i++) {
1391 dst[i] = (src[i] & mask[i]) | (dst[i] & ~mask[i]);
1392 }
1393 }
1394
1395 /* Sets 'flow' member field described by 'field' to 'value', except that bits
1396 * for which 'mask' has a 0-bit keep their existing values. The caller is
1397 * responsible for ensuring that 'flow' meets 'field''s prerequisites.*/
1398 void
1399 mf_set_flow_value_masked(const struct mf_field *field,
1400 const union mf_value *value,
1401 const union mf_value *mask,
1402 struct flow *flow)
1403 {
1404 union mf_value tmp;
1405
1406 mf_get_value(field, flow, &tmp);
1407 apply_mask((const uint8_t *) value, (const uint8_t *) mask,
1408 (uint8_t *) &tmp, field->n_bytes);
1409 mf_set_flow_value(field, &tmp, flow);
1410 }
1411
1412 bool
1413 mf_is_tun_metadata(const struct mf_field *mf)
1414 {
1415 return mf->id >= MFF_TUN_METADATA0 &&
1416 mf->id < MFF_TUN_METADATA0 + TUN_METADATA_NUM_OPTS;
1417 }
1418
1419 /* Returns true if 'mf' has previously been set in 'flow', false if
1420 * it contains a non-default value.
1421 *
1422 * The caller is responsible for ensuring that 'flow' meets 'mf''s
1423 * prerequisites. */
1424 bool
1425 mf_is_set(const struct mf_field *mf, const struct flow *flow)
1426 {
1427 if (!mf_is_tun_metadata(mf)) {
1428 union mf_value value;
1429
1430 mf_get_value(mf, flow, &value);
1431 return !is_all_zeros(&value, mf->n_bytes);
1432 } else {
1433 return ULLONG_GET(flow->tunnel.metadata.present.map,
1434 mf->id - MFF_TUN_METADATA0);
1435 }
1436 }
1437
1438 /* Makes 'match' wildcard field 'mf'.
1439 *
1440 * The caller is responsible for ensuring that 'match' meets 'mf''s
1441 * prerequisites.
1442 *
1443 * If non-NULL, 'err_str' returns a malloc'ed string describing any errors
1444 * with the request or NULL if there is no error. The caller is reponsible
1445 * for freeing the string. */
1446 void
1447 mf_set_wild(const struct mf_field *mf, struct match *match, char **err_str)
1448 {
1449 if (err_str) {
1450 *err_str = NULL;
1451 }
1452
1453 switch (mf->id) {
1454 case MFF_DP_HASH:
1455 match->flow.dp_hash = 0;
1456 match->wc.masks.dp_hash = 0;
1457 break;
1458 case MFF_RECIRC_ID:
1459 match->flow.recirc_id = 0;
1460 match->wc.masks.recirc_id = 0;
1461 break;
1462 case MFF_CONJ_ID:
1463 match->flow.conj_id = 0;
1464 match->wc.masks.conj_id = 0;
1465 break;
1466 case MFF_TUN_ID:
1467 match_set_tun_id_masked(match, htonll(0), htonll(0));
1468 break;
1469 case MFF_TUN_SRC:
1470 match_set_tun_src_masked(match, htonl(0), htonl(0));
1471 break;
1472 case MFF_TUN_DST:
1473 match_set_tun_dst_masked(match, htonl(0), htonl(0));
1474 break;
1475 case MFF_TUN_FLAGS:
1476 match_set_tun_flags_masked(match, 0, 0);
1477 break;
1478 case MFF_TUN_GBP_ID:
1479 match_set_tun_gbp_id_masked(match, 0, 0);
1480 break;
1481 case MFF_TUN_GBP_FLAGS:
1482 match_set_tun_gbp_flags_masked(match, 0, 0);
1483 break;
1484 case MFF_TUN_TOS:
1485 match_set_tun_tos_masked(match, 0, 0);
1486 break;
1487 case MFF_TUN_TTL:
1488 match_set_tun_ttl_masked(match, 0, 0);
1489 break;
1490 CASE_MFF_TUN_METADATA:
1491 tun_metadata_set_match(mf, NULL, NULL, match, err_str);
1492 break;
1493
1494 case MFF_METADATA:
1495 match_set_metadata_masked(match, htonll(0), htonll(0));
1496 break;
1497
1498 case MFF_IN_PORT:
1499 case MFF_IN_PORT_OXM:
1500 match->flow.in_port.ofp_port = 0;
1501 match->wc.masks.in_port.ofp_port = 0;
1502 break;
1503 case MFF_ACTSET_OUTPUT:
1504 match->flow.actset_output = 0;
1505 match->wc.masks.actset_output = 0;
1506 break;
1507
1508 case MFF_SKB_PRIORITY:
1509 match->flow.skb_priority = 0;
1510 match->wc.masks.skb_priority = 0;
1511 break;
1512
1513 case MFF_PKT_MARK:
1514 match->flow.pkt_mark = 0;
1515 match->wc.masks.pkt_mark = 0;
1516 break;
1517
1518 case MFF_CT_STATE:
1519 match->flow.ct_state = 0;
1520 match->wc.masks.ct_state = 0;
1521 break;
1522
1523 case MFF_CT_ZONE:
1524 match->flow.ct_zone = 0;
1525 match->wc.masks.ct_zone = 0;
1526 break;
1527
1528 case MFF_CT_MARK:
1529 match->flow.ct_mark = 0;
1530 match->wc.masks.ct_mark = 0;
1531 break;
1532
1533 case MFF_CT_LABEL:
1534 memset(&match->flow.ct_label, 0, sizeof(match->flow.ct_label));
1535 memset(&match->wc.masks.ct_label, 0, sizeof(match->wc.masks.ct_label));
1536 break;
1537
1538 CASE_MFF_REGS:
1539 match_set_reg_masked(match, mf->id - MFF_REG0, 0, 0);
1540 break;
1541
1542 CASE_MFF_XREGS:
1543 match_set_xreg_masked(match, mf->id - MFF_XREG0, 0, 0);
1544 break;
1545
1546 case MFF_ETH_SRC:
1547 match->flow.dl_src = eth_addr_zero;
1548 match->wc.masks.dl_src = eth_addr_zero;
1549 break;
1550
1551 case MFF_ETH_DST:
1552 match->flow.dl_dst = eth_addr_zero;
1553 match->wc.masks.dl_dst = eth_addr_zero;
1554 break;
1555
1556 case MFF_ETH_TYPE:
1557 match->flow.dl_type = htons(0);
1558 match->wc.masks.dl_type = htons(0);
1559 break;
1560
1561 case MFF_VLAN_TCI:
1562 match_set_dl_tci_masked(match, htons(0), htons(0));
1563 break;
1564
1565 case MFF_DL_VLAN:
1566 case MFF_VLAN_VID:
1567 match_set_any_vid(match);
1568 break;
1569
1570 case MFF_DL_VLAN_PCP:
1571 case MFF_VLAN_PCP:
1572 match_set_any_pcp(match);
1573 break;
1574
1575 case MFF_MPLS_LABEL:
1576 match_set_any_mpls_label(match, 0);
1577 break;
1578
1579 case MFF_MPLS_TC:
1580 match_set_any_mpls_tc(match, 0);
1581 break;
1582
1583 case MFF_MPLS_BOS:
1584 match_set_any_mpls_bos(match, 0);
1585 break;
1586
1587 case MFF_IPV4_SRC:
1588 case MFF_ARP_SPA:
1589 match_set_nw_src_masked(match, htonl(0), htonl(0));
1590 break;
1591
1592 case MFF_IPV4_DST:
1593 case MFF_ARP_TPA:
1594 match_set_nw_dst_masked(match, htonl(0), htonl(0));
1595 break;
1596
1597 case MFF_IPV6_SRC:
1598 memset(&match->wc.masks.ipv6_src, 0, sizeof match->wc.masks.ipv6_src);
1599 memset(&match->flow.ipv6_src, 0, sizeof match->flow.ipv6_src);
1600 break;
1601
1602 case MFF_IPV6_DST:
1603 memset(&match->wc.masks.ipv6_dst, 0, sizeof match->wc.masks.ipv6_dst);
1604 memset(&match->flow.ipv6_dst, 0, sizeof match->flow.ipv6_dst);
1605 break;
1606
1607 case MFF_IPV6_LABEL:
1608 match->wc.masks.ipv6_label = htonl(0);
1609 match->flow.ipv6_label = htonl(0);
1610 break;
1611
1612 case MFF_IP_PROTO:
1613 match->wc.masks.nw_proto = 0;
1614 match->flow.nw_proto = 0;
1615 break;
1616
1617 case MFF_IP_DSCP:
1618 case MFF_IP_DSCP_SHIFTED:
1619 match->wc.masks.nw_tos &= ~IP_DSCP_MASK;
1620 match->flow.nw_tos &= ~IP_DSCP_MASK;
1621 break;
1622
1623 case MFF_IP_ECN:
1624 match->wc.masks.nw_tos &= ~IP_ECN_MASK;
1625 match->flow.nw_tos &= ~IP_ECN_MASK;
1626 break;
1627
1628 case MFF_IP_TTL:
1629 match->wc.masks.nw_ttl = 0;
1630 match->flow.nw_ttl = 0;
1631 break;
1632
1633 case MFF_IP_FRAG:
1634 match->wc.masks.nw_frag &= ~FLOW_NW_FRAG_MASK;
1635 match->flow.nw_frag &= ~FLOW_NW_FRAG_MASK;
1636 break;
1637
1638 case MFF_ARP_OP:
1639 match->wc.masks.nw_proto = 0;
1640 match->flow.nw_proto = 0;
1641 break;
1642
1643 case MFF_ARP_SHA:
1644 case MFF_ND_SLL:
1645 match->flow.arp_sha = eth_addr_zero;
1646 match->wc.masks.arp_sha = eth_addr_zero;
1647 break;
1648
1649 case MFF_ARP_THA:
1650 case MFF_ND_TLL:
1651 match->flow.arp_tha = eth_addr_zero;
1652 match->wc.masks.arp_tha = eth_addr_zero;
1653 break;
1654
1655 case MFF_TCP_SRC:
1656 case MFF_UDP_SRC:
1657 case MFF_SCTP_SRC:
1658 case MFF_ICMPV4_TYPE:
1659 case MFF_ICMPV6_TYPE:
1660 match->wc.masks.tp_src = htons(0);
1661 match->flow.tp_src = htons(0);
1662 break;
1663
1664 case MFF_TCP_DST:
1665 case MFF_UDP_DST:
1666 case MFF_SCTP_DST:
1667 case MFF_ICMPV4_CODE:
1668 case MFF_ICMPV6_CODE:
1669 match->wc.masks.tp_dst = htons(0);
1670 match->flow.tp_dst = htons(0);
1671 break;
1672
1673 case MFF_TCP_FLAGS:
1674 match->wc.masks.tcp_flags = htons(0);
1675 match->flow.tcp_flags = htons(0);
1676 break;
1677
1678 case MFF_ND_TARGET:
1679 memset(&match->wc.masks.nd_target, 0,
1680 sizeof match->wc.masks.nd_target);
1681 memset(&match->flow.nd_target, 0, sizeof match->flow.nd_target);
1682 break;
1683
1684 case MFF_N_IDS:
1685 default:
1686 OVS_NOT_REACHED();
1687 }
1688 }
1689
1690 /* Makes 'match' match field 'mf' with the specified 'value' and 'mask'.
1691 * 'value' specifies a value to match and 'mask' specifies a wildcard pattern,
1692 * with a 1-bit indicating that the corresponding value bit must match and a
1693 * 0-bit indicating a don't-care.
1694 *
1695 * If 'mask' is NULL or points to all-1-bits, then this call is equivalent to
1696 * mf_set_value(mf, value, match). If 'mask' points to all-0-bits, then this
1697 * call is equivalent to mf_set_wild(mf, match).
1698 *
1699 * 'mask' must be a valid mask for 'mf' (see mf_is_mask_valid()). The caller
1700 * is responsible for ensuring that 'match' meets 'mf''s prerequisites.
1701 *
1702 * If non-NULL, 'err_str' returns a malloc'ed string describing any errors
1703 * with the request or NULL if there is no error. The caller is reponsible
1704 * for freeing the string.
1705 *
1706 * Return a set of enum ofputil_protocol bits (as an uint32_t to avoid circular
1707 * dependency on enum ofputil_protocol definition) indicating which OpenFlow
1708 * protocol versions can support this functionality. */
1709 uint32_t
1710 mf_set(const struct mf_field *mf,
1711 const union mf_value *value, const union mf_value *mask,
1712 struct match *match, char **err_str)
1713 {
1714 if (!mask || is_all_ones(mask, mf->n_bytes)) {
1715 mf_set_value(mf, value, match, err_str);
1716 return mf->usable_protocols_exact;
1717 } else if (is_all_zeros(mask, mf->n_bytes) && !mf_is_tun_metadata(mf)) {
1718 /* Tunnel metadata matches on the existence of the field itself, so
1719 * it still needs to be encoded even if the value is wildcarded. */
1720 mf_set_wild(mf, match, err_str);
1721 return OFPUTIL_P_ANY;
1722 }
1723
1724 if (err_str) {
1725 *err_str = NULL;
1726 }
1727
1728 switch (mf->id) {
1729 case MFF_CT_ZONE:
1730 case MFF_RECIRC_ID:
1731 case MFF_CONJ_ID:
1732 case MFF_IN_PORT:
1733 case MFF_IN_PORT_OXM:
1734 case MFF_ACTSET_OUTPUT:
1735 case MFF_SKB_PRIORITY:
1736 case MFF_ETH_TYPE:
1737 case MFF_DL_VLAN:
1738 case MFF_DL_VLAN_PCP:
1739 case MFF_VLAN_PCP:
1740 case MFF_MPLS_LABEL:
1741 case MFF_MPLS_TC:
1742 case MFF_MPLS_BOS:
1743 case MFF_IP_PROTO:
1744 case MFF_IP_TTL:
1745 case MFF_IP_DSCP:
1746 case MFF_IP_DSCP_SHIFTED:
1747 case MFF_IP_ECN:
1748 case MFF_ARP_OP:
1749 case MFF_ICMPV4_TYPE:
1750 case MFF_ICMPV4_CODE:
1751 case MFF_ICMPV6_TYPE:
1752 case MFF_ICMPV6_CODE:
1753 return OFPUTIL_P_NONE;
1754
1755 case MFF_DP_HASH:
1756 match_set_dp_hash_masked(match, ntohl(value->be32), ntohl(mask->be32));
1757 break;
1758 case MFF_TUN_ID:
1759 match_set_tun_id_masked(match, value->be64, mask->be64);
1760 break;
1761 case MFF_TUN_SRC:
1762 match_set_tun_src_masked(match, value->be32, mask->be32);
1763 break;
1764 case MFF_TUN_DST:
1765 match_set_tun_dst_masked(match, value->be32, mask->be32);
1766 break;
1767 case MFF_TUN_FLAGS:
1768 match_set_tun_flags_masked(match, ntohs(value->be16), ntohs(mask->be16));
1769 break;
1770 case MFF_TUN_GBP_ID:
1771 match_set_tun_gbp_id_masked(match, value->be16, mask->be16);
1772 break;
1773 case MFF_TUN_GBP_FLAGS:
1774 match_set_tun_gbp_flags_masked(match, value->u8, mask->u8);
1775 break;
1776 case MFF_TUN_TTL:
1777 match_set_tun_ttl_masked(match, value->u8, mask->u8);
1778 break;
1779 case MFF_TUN_TOS:
1780 match_set_tun_tos_masked(match, value->u8, mask->u8);
1781 break;
1782 CASE_MFF_TUN_METADATA:
1783 tun_metadata_set_match(mf, value, mask, match, err_str);
1784 break;
1785
1786 case MFF_METADATA:
1787 match_set_metadata_masked(match, value->be64, mask->be64);
1788 break;
1789
1790 CASE_MFF_REGS:
1791 match_set_reg_masked(match, mf->id - MFF_REG0,
1792 ntohl(value->be32), ntohl(mask->be32));
1793 break;
1794
1795 CASE_MFF_XREGS:
1796 match_set_xreg_masked(match, mf->id - MFF_XREG0,
1797 ntohll(value->be64), ntohll(mask->be64));
1798 break;
1799
1800 case MFF_PKT_MARK:
1801 match_set_pkt_mark_masked(match, ntohl(value->be32),
1802 ntohl(mask->be32));
1803 break;
1804
1805 case MFF_CT_STATE:
1806 match_set_ct_state_masked(match, ntohl(value->be32), ntohl(mask->be32));
1807 break;
1808
1809 case MFF_CT_MARK:
1810 match_set_ct_mark_masked(match, ntohl(value->be32), ntohl(mask->be32));
1811 break;
1812
1813 case MFF_CT_LABEL: {
1814 ovs_u128 hlabel, hmask;
1815
1816 ntoh128(&value->be128, &hlabel);
1817 if (mask) {
1818 ntoh128(&mask->be128, &hmask);
1819 } else {
1820 hmask.u64.lo = hmask.u64.hi = UINT64_MAX;
1821 }
1822 match_set_ct_label_masked(match, hlabel, hmask);
1823 break;
1824 }
1825
1826 case MFF_ETH_DST:
1827 match_set_dl_dst_masked(match, value->mac, mask->mac);
1828 break;
1829
1830 case MFF_ETH_SRC:
1831 match_set_dl_src_masked(match, value->mac, mask->mac);
1832 break;
1833
1834 case MFF_ARP_SHA:
1835 case MFF_ND_SLL:
1836 match_set_arp_sha_masked(match, value->mac, mask->mac);
1837 break;
1838
1839 case MFF_ARP_THA:
1840 case MFF_ND_TLL:
1841 match_set_arp_tha_masked(match, value->mac, mask->mac);
1842 break;
1843
1844 case MFF_VLAN_TCI:
1845 match_set_dl_tci_masked(match, value->be16, mask->be16);
1846 break;
1847
1848 case MFF_VLAN_VID:
1849 match_set_vlan_vid_masked(match, value->be16, mask->be16);
1850 break;
1851
1852 case MFF_IPV4_SRC:
1853 match_set_nw_src_masked(match, value->be32, mask->be32);
1854 break;
1855
1856 case MFF_IPV4_DST:
1857 match_set_nw_dst_masked(match, value->be32, mask->be32);
1858 break;
1859
1860 case MFF_IPV6_SRC:
1861 match_set_ipv6_src_masked(match, &value->ipv6, &mask->ipv6);
1862 break;
1863
1864 case MFF_IPV6_DST:
1865 match_set_ipv6_dst_masked(match, &value->ipv6, &mask->ipv6);
1866 break;
1867
1868 case MFF_IPV6_LABEL:
1869 if ((mask->be32 & htonl(IPV6_LABEL_MASK)) == htonl(IPV6_LABEL_MASK)) {
1870 mf_set_value(mf, value, match, err_str);
1871 } else {
1872 match_set_ipv6_label_masked(match, value->be32, mask->be32);
1873 }
1874 break;
1875
1876 case MFF_ND_TARGET:
1877 match_set_nd_target_masked(match, &value->ipv6, &mask->ipv6);
1878 break;
1879
1880 case MFF_IP_FRAG:
1881 match_set_nw_frag_masked(match, value->u8, mask->u8);
1882 break;
1883
1884 case MFF_ARP_SPA:
1885 match_set_nw_src_masked(match, value->be32, mask->be32);
1886 break;
1887
1888 case MFF_ARP_TPA:
1889 match_set_nw_dst_masked(match, value->be32, mask->be32);
1890 break;
1891
1892 case MFF_TCP_SRC:
1893 case MFF_UDP_SRC:
1894 case MFF_SCTP_SRC:
1895 match_set_tp_src_masked(match, value->be16, mask->be16);
1896 break;
1897
1898 case MFF_TCP_DST:
1899 case MFF_UDP_DST:
1900 case MFF_SCTP_DST:
1901 match_set_tp_dst_masked(match, value->be16, mask->be16);
1902 break;
1903
1904 case MFF_TCP_FLAGS:
1905 match_set_tcp_flags_masked(match, value->be16, mask->be16);
1906 break;
1907
1908 case MFF_N_IDS:
1909 default:
1910 OVS_NOT_REACHED();
1911 }
1912
1913 return ((mf->usable_protocols_bitwise == mf->usable_protocols_cidr
1914 || ip_is_cidr(mask->be32))
1915 ? mf->usable_protocols_cidr
1916 : mf->usable_protocols_bitwise);
1917 }
1918
1919 static enum ofperr
1920 mf_check__(const struct mf_subfield *sf, const struct flow *flow,
1921 const char *type)
1922 {
1923 if (!sf->field) {
1924 VLOG_WARN_RL(&rl, "unknown %s field", type);
1925 return OFPERR_OFPBAC_BAD_SET_TYPE;
1926 } else if (!sf->n_bits) {
1927 VLOG_WARN_RL(&rl, "zero bit %s field %s", type, sf->field->name);
1928 return OFPERR_OFPBAC_BAD_SET_LEN;
1929 } else if (sf->ofs >= sf->field->n_bits) {
1930 VLOG_WARN_RL(&rl, "bit offset %d exceeds %d-bit width of %s field %s",
1931 sf->ofs, sf->field->n_bits, type, sf->field->name);
1932 return OFPERR_OFPBAC_BAD_SET_LEN;
1933 } else if (sf->ofs + sf->n_bits > sf->field->n_bits) {
1934 VLOG_WARN_RL(&rl, "bit offset %d and width %d exceeds %d-bit width "
1935 "of %s field %s", sf->ofs, sf->n_bits,
1936 sf->field->n_bits, type, sf->field->name);
1937 return OFPERR_OFPBAC_BAD_SET_LEN;
1938 } else if (flow && !mf_are_prereqs_ok(sf->field, flow)) {
1939 VLOG_WARN_RL(&rl, "%s field %s lacks correct prerequisites",
1940 type, sf->field->name);
1941 return OFPERR_OFPBAC_MATCH_INCONSISTENT;
1942 } else {
1943 return 0;
1944 }
1945 }
1946
1947 /* Checks whether 'sf' is valid for reading a subfield out of 'flow'. Returns
1948 * 0 if so, otherwise an OpenFlow error code (e.g. as returned by
1949 * ofp_mkerr()). */
1950 enum ofperr
1951 mf_check_src(const struct mf_subfield *sf, const struct flow *flow)
1952 {
1953 return mf_check__(sf, flow, "source");
1954 }
1955
1956 /* Checks whether 'sf' is valid for writing a subfield into 'flow'. Returns 0
1957 * if so, otherwise an OpenFlow error code (e.g. as returned by
1958 * ofp_mkerr()). */
1959 enum ofperr
1960 mf_check_dst(const struct mf_subfield *sf, const struct flow *flow)
1961 {
1962 int error = mf_check__(sf, flow, "destination");
1963 if (!error && !sf->field->writable) {
1964 VLOG_WARN_RL(&rl, "destination field %s is not writable",
1965 sf->field->name);
1966 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
1967 }
1968 return error;
1969 }
1970
1971 /* Copies the value and wildcard bit pattern for 'mf' from 'match' into the
1972 * 'value' and 'mask', respectively. */
1973 void
1974 mf_get(const struct mf_field *mf, const struct match *match,
1975 union mf_value *value, union mf_value *mask)
1976 {
1977 mf_get_value(mf, &match->flow, value);
1978 mf_get_mask(mf, &match->wc, mask);
1979 }
1980
1981 static char *
1982 mf_from_integer_string(const struct mf_field *mf, const char *s,
1983 uint8_t *valuep, uint8_t *maskp)
1984 {
1985 char *tail;
1986 const char *err_str = "";
1987 int err;
1988
1989 err = parse_int_string(s, valuep, mf->n_bytes, &tail);
1990 if (err || (*tail != '\0' && *tail != '/')) {
1991 err_str = "value";
1992 goto syntax_error;
1993 }
1994
1995 if (*tail == '/') {
1996 err = parse_int_string(tail + 1, maskp, mf->n_bytes, &tail);
1997 if (err || *tail != '\0') {
1998 err_str = "mask";
1999 goto syntax_error;
2000 }
2001 } else {
2002 memset(maskp, 0xff, mf->n_bytes);
2003 }
2004
2005 return NULL;
2006
2007 syntax_error:
2008 if (err == ERANGE) {
2009 return xasprintf("%s: %s too large for %u-byte field %s",
2010 s, err_str, mf->n_bytes, mf->name);
2011 } else {
2012 return xasprintf("%s: bad syntax for %s %s", s, mf->name, err_str);
2013 }
2014 }
2015
2016 static char *
2017 mf_from_ethernet_string(const struct mf_field *mf, const char *s,
2018 struct eth_addr *mac, struct eth_addr *mask)
2019 {
2020 int n;
2021
2022 ovs_assert(mf->n_bytes == ETH_ADDR_LEN);
2023
2024 n = -1;
2025 if (ovs_scan(s, ETH_ADDR_SCAN_FMT"%n", ETH_ADDR_SCAN_ARGS(*mac), &n)
2026 && n == strlen(s)) {
2027 *mask = eth_addr_exact;
2028 return NULL;
2029 }
2030
2031 n = -1;
2032 if (ovs_scan(s, ETH_ADDR_SCAN_FMT"/"ETH_ADDR_SCAN_FMT"%n",
2033 ETH_ADDR_SCAN_ARGS(*mac), ETH_ADDR_SCAN_ARGS(*mask), &n)
2034 && n == strlen(s)) {
2035 return NULL;
2036 }
2037
2038 return xasprintf("%s: invalid Ethernet address", s);
2039 }
2040
2041 static char *
2042 mf_from_ipv4_string(const struct mf_field *mf, const char *s,
2043 ovs_be32 *ip, ovs_be32 *mask)
2044 {
2045 int prefix;
2046
2047 ovs_assert(mf->n_bytes == sizeof *ip);
2048
2049 if (ovs_scan(s, IP_SCAN_FMT"/"IP_SCAN_FMT,
2050 IP_SCAN_ARGS(ip), IP_SCAN_ARGS(mask))) {
2051 /* OK. */
2052 } else if (ovs_scan(s, IP_SCAN_FMT"/%d", IP_SCAN_ARGS(ip), &prefix)) {
2053 if (prefix <= 0 || prefix > 32) {
2054 return xasprintf("%s: network prefix bits not between 0 and "
2055 "32", s);
2056 }
2057 *mask = be32_prefix_mask(prefix);
2058 } else if (ovs_scan(s, IP_SCAN_FMT, IP_SCAN_ARGS(ip))) {
2059 *mask = OVS_BE32_MAX;
2060 } else {
2061 return xasprintf("%s: invalid IP address", s);
2062 }
2063 return NULL;
2064 }
2065
2066 static char *
2067 mf_from_ipv6_string(const struct mf_field *mf, const char *s,
2068 struct in6_addr *value, struct in6_addr *mask)
2069 {
2070 char *str = xstrdup(s);
2071 char *save_ptr = NULL;
2072 const char *name, *netmask;
2073 int retval;
2074
2075 ovs_assert(mf->n_bytes == sizeof *value);
2076
2077 name = strtok_r(str, "/", &save_ptr);
2078 retval = name ? lookup_ipv6(name, value) : EINVAL;
2079 if (retval) {
2080 char *err;
2081
2082 err = xasprintf("%s: could not convert to IPv6 address", str);
2083 free(str);
2084
2085 return err;
2086 }
2087
2088 netmask = strtok_r(NULL, "/", &save_ptr);
2089 if (netmask) {
2090 if (inet_pton(AF_INET6, netmask, mask) != 1) {
2091 int prefix = atoi(netmask);
2092 if (prefix <= 0 || prefix > 128) {
2093 free(str);
2094 return xasprintf("%s: prefix bits not between 1 and 128", s);
2095 } else {
2096 *mask = ipv6_create_mask(prefix);
2097 }
2098 }
2099 } else {
2100 *mask = in6addr_exact;
2101 }
2102 free(str);
2103
2104 return NULL;
2105 }
2106
2107 static char *
2108 mf_from_ofp_port_string(const struct mf_field *mf, const char *s,
2109 ovs_be16 *valuep, ovs_be16 *maskp)
2110 {
2111 ofp_port_t port;
2112
2113 ovs_assert(mf->n_bytes == sizeof(ovs_be16));
2114
2115 if (ofputil_port_from_string(s, &port)) {
2116 *valuep = htons(ofp_to_u16(port));
2117 *maskp = OVS_BE16_MAX;
2118 return NULL;
2119 }
2120 return xasprintf("%s: port value out of range for %s", s, mf->name);
2121 }
2122
2123 static char *
2124 mf_from_ofp_port_string32(const struct mf_field *mf, const char *s,
2125 ovs_be32 *valuep, ovs_be32 *maskp)
2126 {
2127 ofp_port_t port;
2128
2129 ovs_assert(mf->n_bytes == sizeof(ovs_be32));
2130 if (ofputil_port_from_string(s, &port)) {
2131 *valuep = ofputil_port_to_ofp11(port);
2132 *maskp = OVS_BE32_MAX;
2133 return NULL;
2134 }
2135 return xasprintf("%s: port value out of range for %s", s, mf->name);
2136 }
2137
2138 struct frag_handling {
2139 const char *name;
2140 uint8_t mask;
2141 uint8_t value;
2142 };
2143
2144 static const struct frag_handling all_frags[] = {
2145 #define A FLOW_NW_FRAG_ANY
2146 #define L FLOW_NW_FRAG_LATER
2147 /* name mask value */
2148
2149 { "no", A|L, 0 },
2150 { "first", A|L, A },
2151 { "later", A|L, A|L },
2152
2153 { "no", A, 0 },
2154 { "yes", A, A },
2155
2156 { "not_later", L, 0 },
2157 { "later", L, L },
2158 #undef A
2159 #undef L
2160 };
2161
2162 static char *
2163 mf_from_frag_string(const char *s, uint8_t *valuep, uint8_t *maskp)
2164 {
2165 const struct frag_handling *h;
2166
2167 for (h = all_frags; h < &all_frags[ARRAY_SIZE(all_frags)]; h++) {
2168 if (!strcasecmp(s, h->name)) {
2169 /* We force the upper bits of the mask on to make mf_parse_value()
2170 * happy (otherwise it will never think it's an exact match.) */
2171 *maskp = h->mask | ~FLOW_NW_FRAG_MASK;
2172 *valuep = h->value;
2173 return NULL;
2174 }
2175 }
2176
2177 return xasprintf("%s: unknown fragment type (valid types are \"no\", "
2178 "\"yes\", \"first\", \"later\", \"not_first\"", s);
2179 }
2180
2181 static char *
2182 parse_mf_flags(const char *s, const char *(*bit_to_string)(uint32_t),
2183 const char *field_name, ovs_be16 *flagsp, ovs_be16 allowed,
2184 ovs_be16 *maskp)
2185 {
2186 int err;
2187 char *err_str;
2188 uint32_t flags, mask;
2189
2190 err = parse_flags(s, bit_to_string, '\0', field_name, &err_str,
2191 &flags, ntohs(allowed), maskp ? &mask : NULL);
2192 if (err < 0) {
2193 return err_str;
2194 }
2195
2196 *flagsp = htons(flags);
2197 if (maskp) {
2198 *maskp = htons(mask);
2199 }
2200
2201 return NULL;
2202 }
2203
2204 static char *
2205 mf_from_tcp_flags_string(const char *s, ovs_be16 *flagsp, ovs_be16 *maskp)
2206 {
2207 return parse_mf_flags(s, packet_tcp_flag_to_string, "TCP", flagsp,
2208 TCP_FLAGS_BE16(OVS_BE16_MAX), maskp);
2209 }
2210
2211 static char *
2212 mf_from_tun_flags_string(const char *s, ovs_be16 *flagsp, ovs_be16 *maskp)
2213 {
2214 return parse_mf_flags(s, flow_tun_flag_to_string, "tunnel", flagsp,
2215 htons(FLOW_TNL_PUB_F_MASK), maskp);
2216 }
2217
2218 static char *
2219 mf_from_ct_state_string(const char *s, ovs_be32 *flagsp, ovs_be32 *maskp)
2220 {
2221 int err;
2222 char *err_str;
2223 uint32_t flags, mask;
2224
2225 err = parse_flags(s, ct_state_to_string, '\0', "ct_state", &err_str,
2226 &flags, CS_SUPPORTED_MASK, maskp ? &mask : NULL);
2227 if (err < 0) {
2228 return err_str;
2229 }
2230
2231 *flagsp = htonl(flags);
2232 if (maskp) {
2233 *maskp = htonl(mask);
2234 }
2235
2236 return NULL;
2237 }
2238
2239 /* Parses 's', a string value for field 'mf', into 'value' and 'mask'. Returns
2240 * NULL if successful, otherwise a malloc()'d string describing the error. */
2241 char *
2242 mf_parse(const struct mf_field *mf, const char *s,
2243 union mf_value *value, union mf_value *mask)
2244 {
2245 char *error;
2246
2247 if (!strcmp(s, "*")) {
2248 memset(value, 0, mf->n_bytes);
2249 memset(mask, 0, mf->n_bytes);
2250 return NULL;
2251 }
2252
2253 switch (mf->string) {
2254 case MFS_DECIMAL:
2255 case MFS_HEXADECIMAL:
2256 error = mf_from_integer_string(mf, s,
2257 (uint8_t *) value, (uint8_t *) mask);
2258 break;
2259
2260 case MFS_CT_STATE:
2261 ovs_assert(mf->n_bytes == sizeof(ovs_be32));
2262 error = mf_from_ct_state_string(s, &value->be32, &mask->be32);
2263 break;
2264
2265 case MFS_ETHERNET:
2266 error = mf_from_ethernet_string(mf, s, &value->mac, &mask->mac);
2267 break;
2268
2269 case MFS_IPV4:
2270 error = mf_from_ipv4_string(mf, s, &value->be32, &mask->be32);
2271 break;
2272
2273 case MFS_IPV6:
2274 error = mf_from_ipv6_string(mf, s, &value->ipv6, &mask->ipv6);
2275 break;
2276
2277 case MFS_OFP_PORT:
2278 error = mf_from_ofp_port_string(mf, s, &value->be16, &mask->be16);
2279 break;
2280
2281 case MFS_OFP_PORT_OXM:
2282 error = mf_from_ofp_port_string32(mf, s, &value->be32, &mask->be32);
2283 break;
2284
2285 case MFS_FRAG:
2286 error = mf_from_frag_string(s, &value->u8, &mask->u8);
2287 break;
2288
2289 case MFS_TNL_FLAGS:
2290 ovs_assert(mf->n_bytes == sizeof(ovs_be16));
2291 error = mf_from_tun_flags_string(s, &value->be16, &mask->be16);
2292 break;
2293
2294 case MFS_TCP_FLAGS:
2295 ovs_assert(mf->n_bytes == sizeof(ovs_be16));
2296 error = mf_from_tcp_flags_string(s, &value->be16, &mask->be16);
2297 break;
2298
2299 default:
2300 OVS_NOT_REACHED();
2301 }
2302
2303 if (!error && !mf_is_mask_valid(mf, mask)) {
2304 error = xasprintf("%s: invalid mask for field %s", s, mf->name);
2305 }
2306 return error;
2307 }
2308
2309 /* Parses 's', a string value for field 'mf', into 'value'. Returns NULL if
2310 * successful, otherwise a malloc()'d string describing the error. */
2311 char *
2312 mf_parse_value(const struct mf_field *mf, const char *s, union mf_value *value)
2313 {
2314 union mf_value mask;
2315 char *error;
2316
2317 error = mf_parse(mf, s, value, &mask);
2318 if (error) {
2319 return error;
2320 }
2321
2322 if (!is_all_ones((const uint8_t *) &mask, mf->n_bytes)) {
2323 return xasprintf("%s: wildcards not allowed here", s);
2324 }
2325 return NULL;
2326 }
2327
2328 static void
2329 mf_format_integer_string(const struct mf_field *mf, const uint8_t *valuep,
2330 const uint8_t *maskp, struct ds *s)
2331 {
2332 if (mf->string == MFS_HEXADECIMAL) {
2333 ds_put_hex(s, valuep, mf->n_bytes);
2334 } else {
2335 unsigned long long int integer = 0;
2336 int i;
2337
2338 ovs_assert(mf->n_bytes <= 8);
2339 for (i = 0; i < mf->n_bytes; i++) {
2340 integer = (integer << 8) | valuep[i];
2341 }
2342 ds_put_format(s, "%lld", integer);
2343 }
2344
2345 if (maskp) {
2346 /* I guess we could write the mask in decimal for MFS_DECIMAL but I'm
2347 * not sure that that a bit-mask written in decimal is ever easier to
2348 * understand than the same bit-mask written in hexadecimal. */
2349 ds_put_char(s, '/');
2350 ds_put_hex(s, maskp, mf->n_bytes);
2351 }
2352 }
2353
2354 static void
2355 mf_format_frag_string(uint8_t value, uint8_t mask, struct ds *s)
2356 {
2357 const struct frag_handling *h;
2358
2359 mask &= FLOW_NW_FRAG_MASK;
2360 value &= mask;
2361
2362 for (h = all_frags; h < &all_frags[ARRAY_SIZE(all_frags)]; h++) {
2363 if (value == h->value && mask == h->mask) {
2364 ds_put_cstr(s, h->name);
2365 return;
2366 }
2367 }
2368 ds_put_cstr(s, "<error>");
2369 }
2370
2371 static void
2372 mf_format_tnl_flags_string(ovs_be16 value, ovs_be16 mask, struct ds *s)
2373 {
2374 format_flags_masked(s, NULL, flow_tun_flag_to_string, ntohs(value),
2375 ntohs(mask) & FLOW_TNL_PUB_F_MASK, FLOW_TNL_PUB_F_MASK);
2376 }
2377
2378 static void
2379 mf_format_tcp_flags_string(ovs_be16 value, ovs_be16 mask, struct ds *s)
2380 {
2381 format_flags_masked(s, NULL, packet_tcp_flag_to_string, ntohs(value),
2382 TCP_FLAGS(mask), TCP_FLAGS(OVS_BE16_MAX));
2383 }
2384
2385 static void
2386 mf_format_ct_state_string(ovs_be32 value, ovs_be32 mask, struct ds *s)
2387 {
2388 format_flags_masked(s, NULL, ct_state_to_string, ntohl(value),
2389 ntohl(mask), UINT16_MAX);
2390 }
2391
2392 /* Appends to 's' a string representation of field 'mf' whose value is in
2393 * 'value' and 'mask'. 'mask' may be NULL to indicate an exact match. */
2394 void
2395 mf_format(const struct mf_field *mf,
2396 const union mf_value *value, const union mf_value *mask,
2397 struct ds *s)
2398 {
2399 if (mask) {
2400 if (is_all_zeros(mask, mf->n_bytes)) {
2401 ds_put_cstr(s, "ANY");
2402 return;
2403 } else if (is_all_ones(mask, mf->n_bytes)) {
2404 mask = NULL;
2405 }
2406 }
2407
2408 switch (mf->string) {
2409 case MFS_OFP_PORT_OXM:
2410 if (!mask) {
2411 ofp_port_t port;
2412 ofputil_port_from_ofp11(value->be32, &port);
2413 ofputil_format_port(port, s);
2414 break;
2415 }
2416 /* fall through */
2417 case MFS_OFP_PORT:
2418 if (!mask) {
2419 ofputil_format_port(u16_to_ofp(ntohs(value->be16)), s);
2420 break;
2421 }
2422 /* fall through */
2423 case MFS_DECIMAL:
2424 case MFS_HEXADECIMAL:
2425 mf_format_integer_string(mf, (uint8_t *) value, (uint8_t *) mask, s);
2426 break;
2427
2428 case MFS_CT_STATE:
2429 mf_format_ct_state_string(value->be32,
2430 mask ? mask->be32 : OVS_BE32_MAX, s);
2431 break;
2432
2433 case MFS_ETHERNET:
2434 eth_format_masked(value->mac, mask ? &mask->mac : NULL, s);
2435 break;
2436
2437 case MFS_IPV4:
2438 ip_format_masked(value->be32, mask ? mask->be32 : OVS_BE32_MAX, s);
2439 break;
2440
2441 case MFS_IPV6:
2442 print_ipv6_masked(s, &value->ipv6, mask ? &mask->ipv6 : NULL);
2443 break;
2444
2445 case MFS_FRAG:
2446 mf_format_frag_string(value->u8, mask ? mask->u8 : UINT8_MAX, s);
2447 break;
2448
2449 case MFS_TNL_FLAGS:
2450 mf_format_tnl_flags_string(value->be16,
2451 mask ? mask->be16 : OVS_BE16_MAX, s);
2452 break;
2453
2454 case MFS_TCP_FLAGS:
2455 mf_format_tcp_flags_string(value->be16,
2456 mask ? mask->be16 : OVS_BE16_MAX, s);
2457 break;
2458
2459 default:
2460 OVS_NOT_REACHED();
2461 }
2462 }
2463 \f
2464 /* Makes subfield 'sf' within 'flow' exactly match the 'sf->n_bits'
2465 * least-significant bits in 'x'.
2466 */
2467 void
2468 mf_write_subfield_flow(const struct mf_subfield *sf,
2469 const union mf_subvalue *x, struct flow *flow)
2470 {
2471 const struct mf_field *field = sf->field;
2472 union mf_value value;
2473
2474 mf_get_value(field, flow, &value);
2475 bitwise_copy(x, sizeof *x, 0, &value, field->n_bytes,
2476 sf->ofs, sf->n_bits);
2477 mf_set_flow_value(field, &value, flow);
2478 }
2479
2480 /* Makes subfield 'sf' within 'match' exactly match the 'sf->n_bits'
2481 * least-significant bits in 'x'.
2482 */
2483 void
2484 mf_write_subfield(const struct mf_subfield *sf, const union mf_subvalue *x,
2485 struct match *match)
2486 {
2487 const struct mf_field *field = sf->field;
2488 union mf_value value, mask;
2489
2490 mf_get(field, match, &value, &mask);
2491 bitwise_copy(x, sizeof *x, 0, &value, field->n_bytes, sf->ofs, sf->n_bits);
2492 bitwise_one ( &mask, field->n_bytes, sf->ofs, sf->n_bits);
2493 mf_set(field, &value, &mask, match, NULL);
2494 }
2495
2496 /* 'v' and 'm' correspond to values of 'field'. This function copies them into
2497 * 'match' in the correspond positions. */
2498 void
2499 mf_mask_subfield(const struct mf_field *field,
2500 const union mf_subvalue *v,
2501 const union mf_subvalue *m,
2502 struct match *match)
2503 {
2504 union mf_value value, mask;
2505
2506 mf_get(field, match, &value, &mask);
2507 bitwise_copy(v, sizeof *v, 0, &value, field->n_bytes, 0, field->n_bits);
2508 bitwise_copy(m, sizeof *m, 0, &mask, field->n_bytes, 0, field->n_bits);
2509 mf_set(field, &value, &mask, match, NULL);
2510 }
2511
2512 /* Initializes 'x' to the value of 'sf' within 'flow'. 'sf' must be valid for
2513 * reading 'flow', e.g. as checked by mf_check_src(). */
2514 void
2515 mf_read_subfield(const struct mf_subfield *sf, const struct flow *flow,
2516 union mf_subvalue *x)
2517 {
2518 union mf_value value;
2519
2520 mf_get_value(sf->field, flow, &value);
2521
2522 memset(x, 0, sizeof *x);
2523 bitwise_copy(&value, sf->field->n_bytes, sf->ofs,
2524 x, sizeof *x, 0,
2525 sf->n_bits);
2526 }
2527
2528 /* Returns the value of 'sf' within 'flow'. 'sf' must be valid for reading
2529 * 'flow', e.g. as checked by mf_check_src() and sf->n_bits must be 64 or
2530 * less. */
2531 uint64_t
2532 mf_get_subfield(const struct mf_subfield *sf, const struct flow *flow)
2533 {
2534 union mf_value value;
2535
2536 mf_get_value(sf->field, flow, &value);
2537 return bitwise_get(&value, sf->field->n_bytes, sf->ofs, sf->n_bits);
2538 }
2539
2540 void
2541 mf_format_subvalue(const union mf_subvalue *subvalue, struct ds *s)
2542 {
2543 ds_put_hex(s, subvalue->u8, sizeof subvalue->u8);
2544 }
2545
2546 void
2547 field_array_set(enum mf_field_id id, const union mf_value *value,
2548 struct field_array *fa)
2549 {
2550 ovs_assert(id < MFF_N_IDS);
2551 bitmap_set1(fa->used.bm, id);
2552 fa->value[id] = *value;
2553 }