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