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