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